Highcharts addPlotLine for one set of values

 ̄綄美尐妖づ 提交于 2019-12-12 05:30:39

问题


I have the following code. For a given day it shows the average wait time and the maximum wait time in seconds. The plotline currently gives an average of these two values. What I would like is that the average just take into account the average times so for a particular period as chosen by the user, the line would display the average for that period

var seriesOptionsChatChart = [],
seriesCounterChatChart = 0,
namesChatChart = ['ChatChartAVG','ChatChartMAX'];

function createChartChatChart() {

    Highcharts.stockChart('chart27', {

        rangeSelector: {
            buttons: [{
                type: 'day',
                count: 1,
                text: '1d'
            },{
                type: 'day',
                count: 5,
                text: '5d'
            }, {
                type: 'week',
                count: 1,
                text: '1w'
            }, {
                type: 'month',
                count: 1,
                text: '1m'
            }, {
                type: 'month',
                count: 3,
                text: '3m'
            }, {
                type: 'year',
                count: 1,
                text: '1y'
            }, {
                type: 'all',
                text: 'All'
            }],
            selected: 3 // all
        },
    credits: {
          enabled: false
      },
          chart: {
            zoomType: 'x',
            events: {
                load: computeAvg,
                redraw: computeAvg
            }
        },

        yAxis: {

            plotLines: [{
                value: 0,
                width: 2,
                color: 'silver'
            }],
            title: {
                text: 'Seconds'
            }
        },

        plotOptions: {
            series: {
                showInNavigator: true,
                turboThreshold:0
            }
        },

        tooltip: {
            pointFormat: '<span style="color:{series.color}">Seconds</span>: <b>{point.y}</b><br/>',
            split: true
        },

        series: seriesOptionsChatChart
    });
}

$.each(namesChatChart, function (i, name) {

    $.getJSON('/' + name.toLowerCase(),    function (data) {

        if (name=='ChatChartMAX') {
            seriesColour = '#D50000';
        }
        else {
            seriesColour = '#1DA2B6';
        }

        seriesOptionsChatChart[i] = {
            name: name,
            data: data,
            color: seriesColour,
        };
        seriesCounterChatChart += 1;
        if (seriesCounterChatChart === namesChatChart.length) {
            createChartChatChart();
        }
    });
});


function computeAvg() {
    var chart = this,
        series = chart.series,
        yAxis = chart.yAxis[0],
        xAxis = chart.xAxis[0],
        extremes = xAxis.getExtremes(),
        min = extremes.min,
        max = extremes.max,
        plotLine = chart.get('avgLine'),
        sum = 0,
        count = 0;

    Highcharts.each(series, function (serie, i) {
        if(serie.name !== 'Navigator') {
            Highcharts.each(serie.data, function (point, j) {
                if (point.x >= min && point.x <= max) {
                    sum += point.y;
                    count++;
                }
            });
        }
    });

    yAxis.removePlotLine('avgLine');
    yAxis.addPlotLine({
        value: (sum/count),
        color: '#52B184',
        width: 2,
        id: 'avgLine',
        label: {
          text: (sum / count).toFixed(2),
          verticalAlign: 'middle',
          align: 'right',
          rotation: 0,
        }           
    });

}

回答1:


Changed if(serie.name !== 'Navigator') { to focus on the series I wanted



来源:https://stackoverflow.com/questions/44366741/highcharts-addplotline-for-one-set-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!