通过滑块更改Highchart图形幅度

时间:2017-11-06 19:26:38

标签: javascript highcharts

在这里完成初学者。 从Spline每秒更新,我想通过滑块改变图形的幅度(这意味着,随机生成的数字的范围)。如何实现呢?

我能够改变变量,但是代表幅度的变量y在函数内部,并且我的解决方案都不可行。

这是代码

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>


</head>

<body>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script>
    $(document).ready(function () {
    Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });

    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 () {
                        var x = (new Date()).getTime(), // current time
                            y = Math.random();
                        series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                    Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;

                for (i = -19; i <= 0; i += 1) {
                    data.push({
                        x: time + i * 1000,
                        y: Math.random()
                    });
                }
                return data;
            }())
        }]
    });
});
</script>

</body>

如何添加滑块,可以控制负载中变量y的值?

提前致谢。

1 个答案:

答案 0 :(得分:1)

请参阅此实时演示http://jsfiddle.net/kkulig/4y120nbo/

我将对HTML滑块的引用放在“更高”的位置。范围比load函数。我使用rangeForm.value来操纵生成数字的幅度:

    // set up the updating of the chart each second
    var series = this.series[0];
    setInterval(function() {
      var x = (new Date()).getTime(), // current time
        y = Math.random() * rangeForm.value;
      series.addPoint([x, y], true, true);
    }, 1000);