【问题标题】:How to get total of highcharts multiple series如何获得highcharts多个系列的总数
【发布时间】:2016-11-19 00:57:35
【问题描述】:

使用 Highcharts value-in-legend 插件http://www.highcharts.com/plugin-registry/single/10/Value-In-Legend,我已经能够实现一种多系列总计,但我不明白如何获得单击的 y 轴点的总计。

例如,当我单击时,有一天我会得到 3 个单独的序列号,但我也想以某种方式得到一个总数,但我只知道加载时的 y 点和重绘时可见的 y 点。我认为困难在于获得 3 个系列积分的总和而不是获得单个积分的值。

$(function() {
  // Start the standard Highcharts setup
  var seriesOptions = [],
    yAxisOptions = [],
    seriesCounter = 0,
    names = ['MSFT', 'AAPL', 'GOOG'],
    colors = Highcharts.getOptions().colors;
  $.each(names, function(i, name) {
    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?', function(data) {
      seriesOptions[i] = {
        name: name,
        data: data
      };
      // As we're loading the data asynchronously, we don't know what order it will arrive. So
      // we keep a counter and create the chart when all the data is loaded.
      seriesCounter++;
      if(seriesCounter == names.length) {
        createChart();
      }
    });
  });
  // create the chart when all data is loaded
  function createChart() {
    $('#container').highcharts('StockChart', {
      chart: {
        events: {
          load: function(event) {
            console.log('load');
            var total = 0;
            for(var i = 0, len = this.series[0].yData.length; i < len; i++) {
              total += this.series[0].yData[i];
            }
            totalText_posts = this.renderer.text('Total: ' + total, this.plotLeft, this.plotTop - 35).attr({
              zIndex: 5
            }).add()
          },
          redraw: function(chart) {
            console.log('redraw');
            console.log(totalText_posts);
            var total = 0;
            for(var i = 0, len = this.series[0].yData.length; i < len; i++) {
              if(this.series[0].points[i] && this.series[0].points[i].visible) total += this.series[0].yData[i];
            }
            totalText_posts.element.innerHTML = 'Total: ' + total;
          }
        }
      },
      rangeSelector: {
        selected: 4
      },
      yAxis: {
        labels: {
          formatter: function() {
            return(this.value > 0 ? '+' : '') + this.value + '%';
          }
        },
        plotLines: [{
          value: 0,
          width: 2,
          color: 'silver'
        }]
      },
      legend: {
        enabled: true,
        floating: true,
        align: 'left',
        verticalAlign: 'top',
        y: 35,
        labelFormat: '<span style="color:{color}">{name}</span>: <b>{point.y:.2f} USD</b> ({point.change:.2f}%)<br/>',
        borderWidth: 0
      },
      plotOptions: {
        series: {
          compare: 'percent',
          cursor: 'pointer',
                point: {
                    events: {
                        click: function () {
                            alert('Category: ' + this.category + ', value: ' + this.y);
                        }
                    }
                }
        }
      },
      series: seriesOptions
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/stock/highstock.src.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://rawgithub.com/highslide-software/value-in-legend/master/value-in-legend.js"></script>

<div id="container" style="height: 400px; min-width: 500px"></div>

【问题讨论】:

    标签: javascript jquery date highcharts stocks


    【解决方案1】:

    通过阅读 Highcharts value-in-legend 插件 https://rawgithub.com/highslide-software/value-in-legend/master/value-in-legend.js 的源代码,我找到了一种将总结果作为标题放在多系列中的方法。

        $(function () {
            var seriesOptions_likes = [],
                seriesCounter_likes = 0,
                names_likes = ['MSFT', 'AAPL', 'GOOG'],
                totalText_likes = 0;
    
            /**
             * Create the chart when all data is loaded
             * @returns {undefined}
             */
            function createLikesChart() {
    
                Highcharts.stockChart('container_likes', {
                    chart: {
    
                    },
    
                    rangeSelector: {
                        selected: 4
                    },
    
                    title: {
                        text: 'Total Results: '
                    },
    
                    yAxis: {
                        labels: {
                            formatter: function () {
                                return (this.value > 0 ? ' + ' : '') + this.value + '%';
                            }
                        },
                        plotLines: [{
                            value: 0,
                            width: 2,
                            color: 'silver'
                        }]
                    },
    
                    plotOptions: {
                        series: {
                            compare: 'percent',
                            showInNavigator: true
                        }
                    },
    
                    tooltip: {
                        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                        valueDecimals: 2,
                        split: true
                    },
    
                    series: seriesOptions_likes,
    
                    legend: {
                        enabled: true,
                        floating: true,
                        align: 'left',
                        verticalAlign: 'top',
                        y: 65,
                        borderWidth: 0
                    },
                });
            }
    
            $.each(names_likes, function (i, name) {
    
                $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?',    function (data) {
    
                    seriesOptions_likes[i] = {
                        name: name,
                        data: data
                    };
    
                    // As we're loading the data asynchronously, we don't know what order it will arrive. So
                    // we keep a counter and create the chart when all the data is loaded.
                    seriesCounter_likes += 1;
    
                    if (seriesCounter_likes === names_likes.length) {
                        createLikesChart();
                    }
                });
            });
          });
    
        (function (H) {
            H.Series.prototype.point = {}; // The active point
            H.Chart.prototype.callbacks.push(function (chart) {
    
                $(chart.container).bind('mousemove', function () {
                    var legendOptions = chart.legend.options,
                        hoverPoints = chart.hoverPoints,
                        total = 0;
    
                    if (!hoverPoints && chart.hoverPoint) {
                        hoverPoints = [chart.hoverPoint];
                    }
                    if (hoverPoints) {
                        var total = 0,
                            ctr = 0;
                        H.each(hoverPoints, function (point) {
                            point.series.point = point;
                            total += point.y;
    
                        });
                        H.each(chart.legend.allItems, function (item) {
                            item.legendItem.attr({
                                text: legendOptions.labelFormat ?
                                    H.format(legendOptions.labelFormat, item) :
                                    legendOptions.labelFormatter.call(item)
                            });
                        });
    
                        chart.legend.render();
    
                        chart.title.update({ text: 'Total Results: ' + total.toFixed(2) });
                    }
                });
            });
            // Hide the tooltip but allow the crosshair
            H.Tooltip.prototype.defaultFormatter = function () { return false; };
        }(Highcharts));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.highcharts.com/stock/highstock.js"></script>
    <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
    
    <div id="container_likes" style="height: 400px; min-width: 600px"></div>

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      相关资源
      最近更新 更多