2015-09-07 93 views
1

我只是想通過函數調用來改變highcharts中的數據系列。但似乎我無法訪問它。我知道「系列」是未定義的。我也嘗試使用類似於'按​​鈕單擊'方法(http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-addpoint-append/)失敗的方法。Highchart,使用參數編輯來自函數調用的數據

任何幫助是極大的讚賞 BR克里斯蒂安

var mainchart; 
function addGraphData(a) { 
    mainchart.series[0].addPoint(a); 
} 
$(function() { 
    $('#container').highcharts({ 
    chart: { 
     defaultSeriesType: 'line', 
     events: { 
     load : function() { 
      mainchart = this; // `this` is the reference to the chart 
     } 
     } 
    }, 
    title: { 
     text: 'Kobber (KJ-040)' 
    }, 
    xAxis: { 
     text: 'Dato' 
    }, 
    yAxis: { 
     title: { 
     text: 'Resultater' 
     } 
    }, 
    series: [{ 
     //name: 'Metall data', 
     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] 
    }] 
    }); 
}) 

$(function() { 
 
    $('#container').highcharts({ 
 

 
     series: [{ 
 
      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] 
 
     }] 
 

 
    }); 
 

 

 
    // the button action 
 
    var i = 0; 
 
    $('#button').click(function() { 
 
     var chart = $('#container').highcharts(); 
 
     chart.series[0].addPoint(50 * (i % 3)); 
 
     i += 1; 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<script src="http://code.highcharts.com/highcharts.js"></script> 
 

 
<div id="container" style="height: 400px"></div> 
 
<button id="button" class="autocompare">Add point</button>

+1

如果你打你的'addGraphData()'函數? – Kabulan0lak

+0

Im實際上首先在窗口加載時調用createGraph()函數,然後調用addGraphData()。我想在某些特殊事件中全局調用該函數。我怎樣才能做到這一點?請記住,即時通訊完全neewbie在jQuery中。 – KB87

+0

只需調用createGraph(),然後爲要添加的每個點調用addGraphData()。我真的不明白你的問題。 – Kabulan0lak

回答

0

我覺得你只是不打電話給你定義的功能。我把你的代碼和一個JSFiddle和它的工作。我只是說:

$('#button').click(function() {   
    addGraphData(Math.random()*100); 
}); 

看到一個工作的jsfiddle這裏:http://jsfiddle.net/Lfnbapn7/2/

+0

所以問題是我調用函數的地方?我想全局調用addGraphData()函數,與按鈕或鼠標點擊無關嗎?我怎樣才能做到這一點? – KB87

+0

我不明白。你想添加多少點?例如,如果只有一個,那麼你可以調用'addGraphData(150)'。 – Kabulan0lak

+0

對不起,如果我不清楚。我只是想能夠從使用函數調用的任何地方動態地編輯圖表,就像我有兩個輸入字段在「正確範圍內」獲取它們的值一樣。那麼我想爲圖表添加一個值? – KB87