Google Visualization API图表-使用列或行之类的折线图 [英] Google Visualization API Chart - Line Charts using columns or rows like series

查看:57
本文介绍了Google Visualization API图表-使用列或行之类的折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的数据将格式保留在不同的折线图中.我的意思是,我想对数据表进行矩阵转换.

I would like to use the same data keeping the format in different line graphs. I mean, I would like to do a matrix translation on the data table.

我使用Google代码示例更好地解释了我的问题:

I use the google code example to explain better my question:

var data = google.visualization.arrayToDataTable([
      ['Name', 'Gender', 'Donuts February', 'Donuts January'],
      ['Michael' , 'Male', 12, 5],
      ['Elisa', 'Female', 20, 7],
      ['Robert', 'Male', 7, 3],
    ]);

默认情况下,我可以绘制带有x轴的折线图:Michael,Elisa和Robert以及两个系列.但是我不知道如何在x轴上绘制:甜甜圈1月"和甜甜圈2月"以及三个系列(Michael,Elisa和Robert).

By default, I can draw the line chart with x-axis: Michael, Elisa, and Robert and two series. But I do not know how to draw in the x-axis: 'Donuts January' and 'Donuts February' and three series (Michael, Elisa, and Robert).

有可能吗?

推荐答案

API不支持切换行和列角色,以将行用作您的序列,将列用作轴值.如果要完成此操作,则必须以所需的格式从旧表中手动构造一个新的DataTable.这是您可以做到的一种方法:

The API does not support switching row and column roles to use rows as your series and columns as your axis values. If you want to accomplish that, you have to manually construct a new DataTable from your old one, in the format you want. Here's one way you could do it:

var pivottedData = new google.visualization.DataTable();
pivottedData.addColumn('string', 'Category');
for (var i = 0; i < data.getNumberOfRows(); i++) {
    pivottedData.addColumn('number', data.getValue(i, 0));
}
// skip "Name" and "Gender" columns
for (var i = 2; i < data.getNumberOfColumns(); i++) {
    var row = [data.getColumnLabel(i)];
    for (var j = 0; j < data.getNumberOfRows(); j++) {
        row.push(data.getValue(j, i));
    }
    pivottedData.addRow(row);
}

这篇关于Google Visualization API图表-使用列或行之类的折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆