0

我想以异步方式向散点图添加新点。为此,散点图“addpoint”中有一个 api,可将新发送的数据添加到图表中,而无需刷新散点图。这里使用的代码是

<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script>
    $(document).ready(function(){
        $('#container').highcharts({
        chart: {
            type: 'scatter',
            zoomType: 'xy'
        },
        title: {
            text: 'Height Versus Weight of 507 Individuals by Gender'
        },
        subtitle: {
            text: 'Source: Heinz  2003'
        },
        xAxis: {
            title: {
                enabled: true,
                text: 'Height (cm)'
            },
            startOnTick: true,
            endOnTick: true,
            showLastLabel: true
        },
        yAxis: {
            title: {
                text: 'Weight (kg)'
            }
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            verticalAlign: 'top',
            x: 100,
            y: 70,
            floating: true,
            backgroundColor: '#FFFFFF',
            borderWidth: 1
        },
        plotOptions: {
            scatter: {
                marker: {
                    radius: 5,
                    states: {
                        hover: {
                            enabled: true,
                            lineColor: 'rgb(100,100,100)'
                        }
                    }
                },
                states: {
                    hover: {
                        marker: {
                            enabled: false
                        }
                    }
                },
                tooltip: {
                    headerFormat: '<b>{series.name}</b><br>',
                    pointFormat: '{point.x} cm, {point.y} kg'
                }
            }
        },
        series: [{
            name: 'Female',
            color: 'rgba(223, 83, 83, .5)',
            data: [[161.2, 51.6], [167.5, 59.0], [159.5, 49.2], [157.0, 63.0], [155.8, 53.6],
                ]

        }, {
            name: 'Male',
            color: 'rgba(119, 152, 191, .5)',
            data: [[174.0, 65.6], [175.3, 71.8], [193.5, 80.7], [186.5, 72.6], [187.2, 78.8],
                ]
        }]
    }); 

    function requestData() {
        var chart = $('#container').highcharts(); 
        var points = [
                {
                    x: Math.random() * 100, 
                    y:Math.random() * 80 
                }
            ]
        var series = chart.series[0];    
        var data;
        chart.series[1].addPoint([Math.random() * 100,Math.random() * 80]);     
        // call it again after one second
        setTimeout(requestData, 1000);    
    }
    requestData();      
    });
</script>
 </head>
 <body>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

  </body>
   </html>

小提琴在这里:http: //jsfiddle.net/anirbankundu/T8GT3/1/

谁能告诉我是否有任何可能的方法来添加点数组而不是逐步添加每个点。每次通话我将获得大约 1000 点,总点数可以超过 100K

谢谢, 阿尼尔班

4

1 回答 1

1

用于chart.series[1].data获取当前的系列数据,然后用于chart.series[1].setData更新它的数据。

function requestData() {
    var chart = $('#container').highcharts(),
        serie = chart.series[1];

    // get serie data
    var data = serie.data;

    // append points to data
    for (var i = 0; i < 1000; i++) {
        data.push([
            Math.random() * 100,
            Math.random() * 80
        ]);
    }

    // update serie data
    serie.setData(data);
}

你可以看到它在这里工作。

更新- 将点附加到当前数据

function requestData() {
    var chart = $('#container').highcharts();

    // append points to data
    for (var i = 0; i < 1000; i++) {
        chart.series[1].addPoint([
            Math.random() * 100,
            Math.random() * 80
        ], false);
    }

    chart.redraw();
}

演示

于 2013-08-27T03:27:28.043 回答