【问题标题】:Highcharts : How to flag the local maxima on a dynamic graph drawn using HighChartHighcharts:如何在使用 HighChart 绘制的动态图上标记局部最大值
【发布时间】:2014-04-25 20:37:56
【问题描述】:

这里是 jsfiddle 链接http://jsfiddle.net/MzQE8/19/

这是我的代码

       $(function () {
       $(document).ready(function() {
       Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });

    var chart;

    $('#container').highcharts({
        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: 450
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOptions:{

            series : {
                lineWidth:1
            }
        },
        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 = -30; i <= 0; i++) {
                    data.push({
                        x: time + i * 1000,
                        y: Math.random()
                    });
                }
                return data;
            })(),
            color:'red'
        }]
     });
  });

});

我想在这里做的是为那些大于其邻近区域的点赋予不同的颜色。

怎么做?

一种似乎可行的方法是将迄今为止的最大值保持在变量中,然后如果出现比旧最大值更大的新值,我们应该更新它,并为图中的对应点赋予不同的颜色。

还有其他更直接的方法吗?

【问题讨论】:

    标签: javascript css highcharts data-visualization


    【解决方案1】:

    在 addPoint 函数中,您可以使用对象(而不是数组)并设置颜色。只有您需要的是添加条件,它将修改默认颜色。

    var x = (new Date()).getTime(), // current time
                                y = Math.random(),
                                color = 'red'; //default color
    
                            //condition 
    
                            series.addPoint({
                                x: x, 
                                y: y,
                                color: color
                            }, true, true);
    

    http://jsfiddle.net/MzQE8/130/

    【讨论】:

    • 我想要做的是在该值大于其附近的其他值时为其添加不同的颜色。为此,我正在考虑采用一个大小为 4 的数组并找到最大值并修改其颜色并从数组中提供数据。这个可以吗?
    • 所以我的情况取决于找到该点及其邻域的值,如果该点较大,则给它一个不同的颜色值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多