使用 google charts 指令在 angular js 中向下钻取图表

Drilldown charts in angular js using google charts directives

提问人:VenkateshKoona 提问时间:10/5/2017 最后编辑:jrd1VenkateshKoona 更新时间:10/11/2017 访问量:1052

问:

我们是 angularjs v4 的新手。我们要求在谷歌图表中向下钻取图表。我们正在使用 ng2-google-charts 指令。我们能够找到 select 事件并更新数据。但图表没有重新加载。任何人都可以帮忙吗?

查看:索引.html

<pre>
   <br/>
   <google-chart #drillchart [data]='pieChartData' type="BarChart" (chartSelect)='select($event)'>
   </google-chart>
</pre>

Component.ts:

pieChartData =  {

    chartType: 'BarChart',
    dataTable: [
      ['Country', 'Poulation'],
      ['Ind', 25],
      ['Rus', 10],
      ['Chi', 30],
      ['USA', 15],
      ['UK', 12],
      ['Aus', 8]
    ],
    options: {'title': 'Population'}

  };

newDataIndia = [

    ['State', 'Poulation'],
    ['AndhraPradesh', 30],
    ['UttarPradesh',      40],
    ['MadyaPradesh',  10],
    ['Karnataka', 10],
    ['Tamilnadu', 10]
    ];

  newDataUS = [

    ['State', 'Poulation'],
    ['Texas', 30],
    ['Florida',      40],
    ['Pennsylvania',  10],
    ['Lousiana', 15],
    ['Colorado', 10]
  ];

public changeData(data):void {

    /*let dataTable = this.drillchart.wrapper.getDataTable();
    for (let i = 0; i < 6; i++) {
      dataTable.setValue(i, 1, Math.round(Math.random() * 1000));
      dataTable.setValue(i, 2, Math.round(Math.random() * 1000));
    }*/
    let dataTable = this.drillchart.wrapper.getDataTable()
    console.log(dataTable);
    dataTable.Sf[0].label = data[0][0];
    dataTable.Sf[0].type = "string";
    dataTable.Sf[1].label = data[0][1];
    dataTable.Sf[1].type = "number";

    for (let i = 0; i < data.length-1; i++) {
      dataTable.Tf[i].c[0].v = data[i+1][0];
      dataTable.Tf[i].c[1].v = data[i+1][1];
    }

    if(dataTable.Tf.length < data.length-1)
    {
      for(var icount = data.length-1; icount != data.length-1; icount--) {
        dataTable = dataTable.Tf.pop();
      }
    }

    this.drillchart.redraw();
  }

提前致谢。

angularjs angularjs-directive google-visualization drilldown using -directives

评论


答:

0赞 Vijay Bhosale 10/11/2017 #1

像 highcharts 一样,谷歌图表中没有任何简单的方法可以进行深入分析。但是通过一些技巧是可能的

 <input type="button" style="width: auto !important;" value="back" id="btnBack" />
<div id="SkillBar"></div>

使用后退按钮和容器来绘制图表。现在加载可视化库条形图并创建条形图

google.charts.load('current', { packages: ['corechart', 'bar'] });
 google.charts.setOnLoadCallback(drawAnnotations);
   function drawAnnotations() {
        google.charts.setOnLoadCallback(drawSkillBar);

        function drawSkillBar() {

            var data = new google.visualization.DataTable();


            data.addColumn('string', 'Category');
            data.addColumn('number', 'Demand');
            data.addColumn({ type: 'number', role: 'annotation' });
            data.addColumn('number', 'Supply Overall');
            data.addColumn({ type: 'number', role: 'annotation' });
            data.addColumn('number', 'Bench and Buffer');
            data.addColumn({ type: 'number', role: 'annotation' });

            data.addRows([
              ['Core', 25, 25, 22, 22, 12, 12],
              ['Invest', 15, 15, 21, 21, 11, 11],
              ['Others', 10, 10, 12, 12, 9, 9]
            ]);

            var options = {
                title: '',
                //hAxis: {
                //    title: 'Time of Day',
                //},
                //vAxis: {
                //    title: 'Rating (scale of 1-10)'
                //},
                is3D: true,
                'height': 300,
                'width': 600,
                colors: ['#3366CC', '#378eb8', '#984ea3'],
                legend: 'true',
                //focusTarget: 'category',
                animation: {
                    startup: true,
                    duration: 500,
                    easing: 'out'
                }
            };


            var chart = new google.visualization.ColumnChart(document.getElementById('SkillBar'));

像这样创建您的选择处理程序

 function selectHandler() {
                var selectedItem = chart.getSelection()[0];

                if (selectedItem && selectedItem.row != null) {
                    var value = data.getValue(selectedItem.row, selectedItem.column);
                    var column = data.getColumnLabel(selectedItem.column)

                    if (column.toLowerCase() == 'demand') {
                        $('#btnBack').removeClass('hidden');
                        drawDemandBar(data.getValue(selectedItem.row, 0), '#3366CC');
                    }
                    if (column.toLowerCase() == 'supply overall') {
                        $('#btnBack').removeClass('hidden');
                        drawSupplyBar(data.getValue(selectedItem.row, 0), '#378eb8');
                    }
                    if (column.toLowerCase() == 'bench and buffer') {
                        $('#btnBack').removeClass('hidden');
                        drawOthersBar(data.getValue(selectedItem.row, 0), '#984ea3');
                    }

                }
            }

现在绘制附有选择处理程序事件的图表

google.visualization.events.addListener(chart, 'select', selectHandler);
            chart.draw(data, options);

就是这样..现在您所要做的就是为 3 根柱声明三个不同的函数

1. drawSupplyBar()  2. drawDemandBar() 3. drawOthersBar()

你可以在这里找到完整的代码和工作小提琴