2016-05-30 72 views
1

例如,我有一個這樣的圖表:HighChart:生成的onclick()由for循環addSeries

$(function() { 
$('#KeywordTrend').highcharts({ 
    chart:{ 
     width:500 
    }, 
    title: { 
     text: false 
     }, 
    xAxis: { 
     categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
    }, 
    series: [{ 
     name:'kit', 
     data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] 
    }] 
}); 

,我必須以允許用戶的願望數據添加到圖表創建三個按鈕像下面

var name = ['john','mary','july']; 

    var executed = false; 
    var executed2 = false; 
    var executed3 = false; 

    $(".test0").click(function(){ 
     console.log(executed); 
       if (!executed) { 
     var chart = $('#KeywordTrend').highcharts(); 
     var data = [194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]; 
     chart.addSeries({ 
       name: name[0], 
       data: data 
     }); 
    } 
    executed = true; 
      console.log(executed); 
    }); 


    $(".test1").click(function(){ 
     console.log(executed2); 
      if (!executed2) { 
      var chart2 = $('#KeywordTrend').highcharts(); 
       chart2.addSeries({ 
        name:name[1], 
        data: [194.1, 35.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4] 

      }) 
      executed2 = true; 
      console.log(executed2); 
      } 
     }); 

    $(".test2").click(function(){ 
     console.log(executed3); 
      if (!executed3) { 
      var chart3 = $('#KeywordTrend').highcharts(); 
       chart3.addSeries({ 
        name:name[2], 
        data: [194.1, 85.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4] 

      }) 
      executed3 = true; 
      console.log(executed3); 
      } 
     }); 

以上的click()只是複製和不好的時候纔來維修,因此我想問一下,如果有把在這些click() for循環的方式。

var executed s只是一個檢查器,只允許用戶執行一次該功能。

+0

你的問題一個更好的網站將codereview.stackexchange.com – TMB

+0

可以請你分享的HTML代碼 – brk

回答

2

請大家看看下面的方法。在這裏,我刪除了boolean標誌executed, executed2 and executed3的使用,因爲那些在通用邏輯中使用會很棘手。而不是我使用$.data()技術來存儲有關特定按鈕是否先前被點擊過的信息。希望這將幫助你:

var name = ['john', 'mary', 'july']; 
 

 
var data_array = []; 
 

 
data_array[0] = [194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]; 
 
data_array[1] = [194.1, 35.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]; 
 
data_array[2] = [194.1, 85.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]; 
 

 

 

 
$(function() { 
 
    $('#KeywordTrend').highcharts({ 
 
    chart: { 
 
     width: 500 
 
    }, 
 
    title: { 
 
     text: false 
 
    }, 
 
    xAxis: { 
 
     categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
 
    }, 
 
    series: [{ 
 
     name: 'kit', 
 
     data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] 
 
    }] 
 
    }); 
 

 

 
    $("[class^='test'").click(function() { 
 
    var current_id = $(this).attr("class"); 
 
    var number = current_id.replace("test", ""); 
 
    
 
    var isExecuted = $(this).data("executed"); 
 

 
    console.log(isExecuted); 
 
    if (!isExecuted) { 
 
     var chart = $('#KeywordTrend').highcharts(); 
 
     var data = data_array[number] 
 
     chart.addSeries({ 
 
     name: name[number], 
 
     data: data 
 
     }); 
 
    } 
 
    $(this).data("executed", true); 
 
    console.log($(this).data("executed")); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<script src="https://code.highcharts.com/highcharts.js"></script> 
 
<script src="https://code.highcharts.com/modules/exporting.js"></script> 
 

 

 
<div id="KeywordTrend"></div> 
 

 
<button class="test0">button 1</button> 
 
<button class="test1">button 2</button> 
 
<button class="test2">button 3</button>

0

你可以做到這一點,在這裏我只是將點擊處理程序附加到查詢所有元素。

$('.clickMe').click(function(event) { 
 
    alert(event.target.value); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='button' class='clickMe' value='give'> 
 
<input type='button' class='clickMe' value='me'> 
 
<input type='button' class='clickMe' value='text'>

1

,我發現你的代碼的最大問題是,你沒有統一的數據和代碼的相關位的方式。現在,您會發現他們的persons地圖下

HTML

<input class='test test0' type=button value='person1'> 
<input class='test test1' type=button value='person2'> 
<input class='test test2' type=button value='person3'> 

JS

$(function() { 
    $('#KeywordTrend').highcharts({ 
    chart: { 
     width: 500 
    }, 
    title: { 
     text: false 
    }, 
    xAxis: { 
     categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
    }, 
    series: [{ 
     name: 'kit', 
     data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] 
    }] 
    }); 

    var data = [194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]; 
    var chart = $('#KeywordTrend').highcharts(); 
    var persons = { 
    person1: { 
     name: 'john', 
     executed: false, 
    }, 
    person2: { 
     name: 'mary', 
     executed: false, 
    }, 
    person3: { 
     name: 'july', 
     executed: false, 
    } 
    } 

    $(".test").click(function(event) { 
    var person = persons[event.target.value] 
    if (!test.executed) { 
     chart.addSeries({ 
     name: person.name, 
     data: data 
     }); 
     person.executed = true; 
    } 
    }); 
}); 
+0

這個答案實際上給我帶來了新的想法,但不幸的是,我實際上使用node.js從後端的API獲取數據,因此我現在更好地堅持舊方法,但是謝謝! – anson920520