使用<google.charts.bar>和<series>选项对堆叠的Google图表进行注释

时间:2015-04-29 00:09:19

标签: google-visualization

是否有人知道是否可以在使用 google.charts.Bar 功能创建的图表上添加注释

我使用 google.charts.Bar 而不是 google.visualization.ColumnChart 的原因是我需要为每个时段都有多个堆叠列。

enter image description here

google.load("visualization", "1", {
  packages: ["corechart", "bar", "table"]
});
google.setOnLoadCallback(drawChart);

function drawChart() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Period');
  data.addColumn('number', 'AHMA PS');
  data.addColumn('number', 'Others PS');
  data.addColumn('number', 'AHMA AB');
  data.addColumn('number', 'Others AB');
  data.addColumn('number', 'AHMA CC');
  data.addColumn('number', 'Others CC');

  data.addRows([
    ['Apr', 30, 50, 10, 60, 2, 40],
    ['Mar', 30, 2, 10, 60, 2, 40],
    ['Feb', 30, 50, 10, 60, 2, 40],
    ['Jan', 30, 50, 10, 60, 2, 40]
  ]);

  var view = new google.visualization.DataView(data);

  view.setColumns([0, 1, {
      calc: "stringify",
      sourceColumn: 1,
      type: "string",
      role: "annotation"
    },
    2, {
      calc: "stringify",
      sourceColumn: 2,
      type: "string",
      role: "annotation"
    },
    3, {
      calc: "stringify",
      sourceColumn: 3,
      type: "string",
      role: "annotation"
    },
    4, {
      calc: "stringify",
      sourceColumn: 4,
      type: "string",
      role: "annotation"
    },
    5, {
      calc: "stringify",
      sourceColumn: 5,
      type: "string",
      role: "annotation"
    },
    6, {
      calc: "stringify",
      sourceColumn: 6,
      type: "string",
      role: "annotation"
    }
  ]);

  var options = {
    isStacked: true,
    series: {
      2: {
        targetAxisIndex: 1
      },
      3: {
        targetAxisIndex: 1
      },
      4: {
        targetAxisIndex: 2
      },
      5: {
        targetAxisIndex: 2
      }
    },
    vAxis: {
      viewWindow: {
        min: 0,
        max: 100
      }
    }
  };
  var chart = new google.charts.Bar(document.getElementById('google-chart'));
  chart.draw(view, google.charts.Bar.convertOptions(options));
}
<script src="https://www.google.com/jsapi"></script>

<div id="google-chart"></div>

1 个答案:

答案 0 :(得分:1)

答案:

<script type="text/javascript">

    $(document).ready(function () {

        google.charts.load('current', { 'packages': ['bar'] });
        google.charts.setOnLoadCallback(drawChart);
    });

    function drawChart() {
        alert('ok');
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Period');
        data.addColumn('number', 'AHMA PS');
        data.addColumn('number', 'Others PS');
        data.addColumn('number', 'AHMA AB');
        data.addColumn('number', 'Others AB');
        data.addColumn('number', 'AHMA CC');
        data.addColumn('number', 'Others CC');

        data.addRows([
          ['Apr', 30, 50, 10, 60, 2, 40],
          ['Mar', 30, 2, 10, 60, 2, 40],
          ['Feb', 30, 50, 10, 60, 2, 40],
          ['Jan', 30, 50, 10, 60, 2, 40]
        ]);

        var view = new google.visualization.DataView(data);

        view.setColumns([0, 1, {
            calc: "stringify",
            sourceColumn: 1,
            type: "string",
            role: "annotation"
        },
          2, {
              calc: "stringify",
              sourceColumn: 2,
              type: "string",
              role: "annotation"
          },
          3, {
              calc: "stringify",
              sourceColumn: 3,
              type: "string",
              role: "annotation"
          },
          4, {
              calc: "stringify",
              sourceColumn: 4,
              type: "string",
              role: "annotation"
          },
          5, {
              calc: "stringify",
              sourceColumn: 5,
              type: "string",
              role: "annotation"
          },
          6, {
              calc: "stringify",
              sourceColumn: 6,
              type: "string",
              role: "annotation"
          }
        ]);

        var options = {
            isStacked: true,
            series: {
                2: {
                    targetAxisIndex: 1
                },
                3: {
                    targetAxisIndex: 1
                },
                4: {
                    targetAxisIndex: 2
                },
                5: {
                    targetAxisIndex: 2
                }
            },
            vAxis: {
                viewWindow: {
                    min: 0,
                    max: 100
                }
            }
        };
        var chart = new google.charts.Bar(document.getElementById('google-chart'));
        chart.draw(view, google.charts.Bar.convertOptions(options));
    }
</script>