样条曲线图,对角线固定值为0,0,绘制重新映射的

时间:2017-05-31 08:51:10

标签: javascript jquery graph highcharts

我们正在为我们的游戏使用样条曲线图,在这个游戏中,我们需要将x和y轴值设置为0,并保存从最初到结束的值,因为我们需要从开始到结束的所有绘图价值。

Check Live Demo Here

enter image description here

JavaScript代码

<script>
var a = 1;
var b = 1;
var factor = 1.2;
$(document).ready(function () {
    Highcharts.chart('container', {
        chart: {
            type: 'spline',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
                load: function () { 
                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function () {
                        b = b*1.2;
                        console.log(b);
                        var x = a; // current time
                        var y = b;
                            a++;
                        series.addPoint([x, y], true, true);
                    }, 700);
                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'number',
            min: 0,
            tickInterval: 2
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>X: ' + this.x+', Y:'+this.y;
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                var data = [],i;
                for (i = 1; i <= 19; i++) {
                        b = b*factor;
                        data.push({
                            x: a,
                            y: b
                        });
                        a++;            
                }
                return data;
            }())
        }]
    });
});

1 个答案:

答案 0 :(得分:2)

以下代码绘制从原点(0,0)到结束点的曲线,该曲线在间隔上更新。您需要在shift来电中设置false变量addPointHigchart docs

$(document).ready(function () {

    var a = 1;
    var b = 1;
    var factor = 1.2;

    Highcharts.chart('container', {
        chart: {
            type: 'spline',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
                load: function () { 
                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function () {
                        b = b*1.2;
                        var x = a; // current time
                        var y = b;
                        a++;
                        // Add new end point
                        series.addPoint([x, y], true, false);
                    }, 700);
                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'number',
            min: 0,
            tickInterval: 2
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>X: ' + this.x+', Y:'+this.y;
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                // Add point at origin and last point of series
                var data = [{x:0,y:0}],i;
                for (i = 1; i <= 19; i++) {
                    b = b*factor;
                    a++
                    data.push({
                        x: a,
                        y: b
                    });
                }
                return data;
            }())
        }]
    });
});

Updated JsFiddle