【问题标题】:Creating pivoted DataView from existing google charts DataTable object从现有的谷歌图表 DataTable 对象创建透视数据视图
【发布时间】:2012-11-28 17:24:23
【问题描述】:

我有一个 DataTable,其中包含:

id,day,proj,col1,col2,subtype,time
1,Nov 28,projectA,1050,880,foo,17481
2,Nov 28,projectA,1050,880,bar,16098
3,Nov 28,projectA,1080,40,foo,13509
4,Nov 28,projectA,1080,40,bar,9031

但想创建一个新的透视 DataView,其中包含:

id,day,proj,col1,col2,foo,bar
1,Nov 28,projectA,1050,880,17481,16098
3,Nov 28,projectA,1080,40,13509,9031

然后我想为其创建一个堆叠的柱形图。

查询语言中有一个pivot 子句,但是如何对DataTable 中已有的数据进行透视?

【问题讨论】:

    标签: javascript google-visualization


    【解决方案1】:

    手动。

    你可以在 asgallant 的 jsfiddle 上看到 this example。这使用 dataView 来完成任务。

    google.load('visualization', '1', {packages: ['table']});
    google.setOnLoadCallback(drawChart);
    
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'A');
        data.addColumn('string', 'B');
        data.addColumn('number', 'C');
        data.addRows([
            [1, 'foo', 6],
            [2, 'foo', 2],
            [3, 'foo', 1],
            [4, 'foo', 3],
            [1, 'bar', 7],
            [2, 'bar', 3],
            [1, 'baz', 8],
            [2, 'baz', 4]
        ]);
    
        var table1 = new google.visualization.Table(document.getElementById('table1'));
        table1.draw(data, {});
    
        /* manually pivot the data table
         * set column A as the first column in the view, 
         * then we have to separate out the C values into their own columns
         * according to the value of B, using a DataView with calculated columns
         */
        var view = new google.visualization.DataView(data);
        view.setColumns([0, {
            type: 'number',
            label: 'foo',
            calc: function (dt, row) {
                // return values of C only for the rows where B = "foo"
                return (dt.getValue(row, 1) == 'foo') ? dt.getValue(row, 2) : null;
            }
        }, {
            type: 'number',
            label: 'bar',
            calc: function (dt, row) {
                // return values of C only for the rows where B = "bar"
                return (dt.getValue(row, 1) == 'bar') ? dt.getValue(row, 2) : null;
            }
        }, {
            type: 'number',
            label: 'baz',
            calc: function (dt, row) {
                // return values of C only for the rows where B = "baz"
                return (dt.getValue(row, 1) == 'baz') ? dt.getValue(row, 2) : null;
            }
        }]);
    
        // next, we group the view on column A, which gets us the pivoted data
        var pivotedData = google.visualization.data.group(view, [0], [{
            column: 1,
            type: 'number',
            label: view.getColumnLabel(1),
            aggregation: google.visualization.data.sum
        }, {
            column: 2,
            type: 'number',
            label: view.getColumnLabel(2),
            aggregation: google.visualization.data.sum
        }, {
            column: 3,
            type: 'number',
            label: view.getColumnLabel(3),
            aggregation: google.visualization.data.sum
        }]);
    
        var table2 = new google.visualization.Table(document.getElementById('table2'));
        table2.draw(pivotedData, {});
    }
    

    或者,您可以手动进行。

          var data = new google.visualization.DataTable();
    
          data.addColumn('string', 'First Column Title');
    
          var baseline = chartData.getValue(chartData.getNumberOfRows() - 1, 15);
    
          for (var i = 0; i < chartData.getNumberOfRows(); i++) {
            data.addColumn('number', chartData.getFormattedValue(i, 0));
          };
    
          for (var j = 0; j < chartData.getNumberOfColumns() - 2; j++) {
            data.addRow();
            data.setValue(j, 0, chartData.getColumnLabel(j + 1));
            for (var i = 0; i < chartData.getNumberOfRows(); i++) {
              data.setValue(j, i + 1, chartData.getValue(i, j+1));
            };
          };
    

    【讨论】:

    • 并没有真正帮助我。我的列未命名为 foo、bar 或 baz。很高兴有一些更有活力的东西。不过,您链接的代码看起来很有希望。
    猜你喜欢
    • 2020-10-23
    • 2021-01-19
    • 2017-12-12
    • 2018-10-28
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多