【问题标题】:Google Charts - Trendline for Stacked Column ChartGoogle Charts - 堆积柱形图的趋势线
【发布时间】:2016-12-19 14:49:19
【问题描述】:

我的问题是关于使用 Google Charts API 的堆积柱形图。

我正试图从中得出一条全球趋势线。

<script type="text/javascript">
 google.charts.load("current", {"packages":["corechart"]});
 google.charts.setOnLoadCallback(drawVisualization);
 function drawVisualization() {
 var data = google.visualization.arrayToDataTable([['Month', 'OK', 'KO', 'Estimation'],[ '2016/08', 1990, 49, null ],[ '2016/09', 6892, 97, null ],[ '2016/10', 6018, 0, null ],[ '2016/11', 7329, 146, null ],[ '2016/12', 3059, 97, 1827 ]]);
 var options = {
  isStacked: true,
  seriesType: "bars",
  legend: "none",
  hAxis:{ textPosition: "none" },
  vAxis: { viewWindow: { min: 0, max: 8000 } },
  trendlines: { 0: {} }
 };
 var chart = new google.visualization.ComboChart(document.getElementById("bar"));
 chart.draw(data, options);
 }
 </script>

当我添加 trendlines: { 0: {} }, 时,我没有得到任何结果。

我没有在参考指南上找到任何内容。可能没有实现,还是我做错了?

【问题讨论】:

    标签: javascript charts google-visualization stacked trendline


    【解决方案1】:

    虽然文档中没有提到,trendlines 仅在 Continuous x 轴上受支持

    这意味着第一列的值应该是日期、数字等...

    字符串值产生一个离散

    discrete vs continuous...

    请参阅以下工作 sn-p...

    使用DataView 将第一列转换为实际日期,从而启用trendlines...

    google.charts.load('current', {
      callback: function () {
        var data = google.visualization.arrayToDataTable([
          ['Month', 'OK', 'KO', 'Estimation'],
          ['2016/08', 1990, 49, null],
          ['2016/09', 6892, 97, null],
          ['2016/10', 6018, 0, null],
          ['2016/11', 7329, 146, null],
          ['2016/12', 3059, 97, 1827]
        ]);
    
        var view = new google.visualization.DataView(data);
        view.setColumns([{
          calc: function (dt, row) {
            var dateParts = dt.getValue(row, 0).split('/');
            return new Date(parseInt(dateParts[0]), parseInt(dateParts[1]) - 1, 1);
          },
          type: 'date',
          label: data.getColumnLabel(0)
        }, 1, 2, 3]);
    
        var options = {
          isStacked: true,
          seriesType: 'bars',
          legend: 'none',
          hAxis: {
            textPosition: 'none'
          },
          vAxis: {
            viewWindow: {
              min: 0,
              max: 8000
            }
          },
          trendlines: {
            0: {}
          }
        };
    
        var chart = new google.visualization.ComboChart(document.getElementById('bar'));
        chart.draw(view, options);
      },
      packages:['corechart']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="bar"></div>

    【讨论】: