英文:
How to plot a line chart given a data table using google chart?
问题
如果我有以下的 Google Charts 数据表定义:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('string','Team');
data.addColumn('number', 'Total Score');
并且该表中的数据如下:
Dec,Team A,68
Dec,Team B,75
Dec,Team C,23
Dec,Team D,158
Dec,Team E,27
Jan,Team A,68
Jan,Team E,27
如何使用 Google Charts 创建如下线性图表:
我尝试使用以下代码:
var lineChart = new google.charts.Line(document.getElementById('LineChart'));
lineChart.draw(data);
但这会生成一个具有重复月份的单一线条沿着横轴的图表:
提前感谢您的帮助。
英文:
If I have the following definition of a data table in google charts
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('string','Team');
data.addColumn('number', 'Total Score');
And the data in that table looks like:
Dec,Team A,68
Dec,Team B,75
Dec,Team C,23
Dec,Team D,158
Dec,Team E,27
Jan,Team A,68
Jan,Team E,27
How can I can produce a line chart as follows using google charts:
I have tried using
var lineChart = new google.charts.Line(document.getElementById('LineChart'));
lineChart.draw(data);
But that produces a single line with repeated months along the h-axis
Thank you in advance
答案1
得分: 1
以下是您提供的内容的翻译:
每个团队需要在数据表中拥有自己的系列或列,如下所示...
月份,A队,B队,C队,D队,E队
十二月,68,75,23,158,27
我们可以使用分组函数和数据视图来转换原始数据表。
首先,按团队对数据进行分组。
// 分组数据表
var groupData = google.visualization.data.group(
data,
[0, 1],
[{
column: 2,
aggregation: google.visualization.data.sum,
type: 'number'
}]
);
然后,我们可以使用数据视图创建单独的列/系列。
// 创建数据视图
var view = new google.visualization.DataView(groupData);
// 总和列数组
var aggColumns = [];
// 使用月份作为第一个视图列
var viewColumns = [0];
// 为每个团队构建视图和总和列
groupData.getDistinctValues(1).forEach(function (team, index) {
// 为每个团队添加视图列
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === team) {
return dt.getValue(row, 2);
}
return null;
},
label: team,
type: 'number'
});
// 为每个团队添加总和列
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: team,
type: 'number'
});
});
// 设置视图列
view.setColumns(viewColumns);
然后,我们可以再次进行聚合,以合并所有行...
// 按日期对视图进行求和
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
查看以下可工作示例...
<!-- 开始代码片段:不隐藏JS,显示控制台,不使用Babel -->
<!-- JavaScript语言 -->
google.charts.load('current', {
packages: ['line']
}).then(function () {
// 创建数据表
var data = new google.visualization.DataTable();
data.addColumn('string', '月份');
data.addColumn('string', '团队');
data.addColumn('number', '总分');
data.addRows([
['十二月', 'A队', 68],
['十二月', 'B队', 75],
['十二月', 'C队', 23],
['十二月', 'D队', 158],
['十二月', 'E队', 27],
['一月', 'A队', 68],
['一月', 'E队', 27]
]);
// 分组数据表
var groupData = google.visualization.data.group(
data,
[0, 1],
[{
column: 2,
aggregation: google.visualization.data.sum,
type: 'number'
}]
);
// 创建数据视图
var view = new google.visualization.DataView(groupData);
// 总和列数组
var aggColumns = [];
// 使用月份作为第一个视图列
var viewColumns = [0];
// 为每个团队构建视图和总和列
groupData.getDistinctValues(1).forEach(function (team, index) {
// 为每个团队添加视图列
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === team) {
return dt.getValue(row, 2);
}
return null;
},
label: team,
type: 'number'
});
// 为每个团队添加总和列
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: team,
type: 'number'
});
});
// 设置视图列
view.setColumns(viewColumns);
// 按日期对视图进行求和
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
// 绘制图表
var lineChart = new google.charts.Line(document.getElementById('LineChart'));
lineChart.draw(aggData);
});
<!-- JavaScript语言结束 -->
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="LineChart"></div>
<!-- 结束代码片段 -->
希望这可以帮助您理解和使用您的代码。
英文:
each team needs to have its own series, or column in the data table,
as follows...
Month, Team A, Team B, Team C, Team D, Team E
Dec, 68, 75, 23, 158, 27
we can use the group function and a data view to transform the original data table.
first, group the data by team.
// group data table
var groupData = google.visualization.data.group(
data,
[0, 1],
[{
column: 2,
aggregation: google.visualization.data.sum,
type: 'number'
}]
);
then we can use a data view to create the separate columns / series
// create data view
var view = new google.visualization.DataView(groupData);
// sum column array
var aggColumns = [];
// use month as first view column
var viewColumns = [0];
// build view & agg columns for each team
groupData.getDistinctValues(1).forEach(function (team, index) {
// add view column for each team
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === team) {
return dt.getValue(row, 2);
}
return null;
},
label: team,
type: 'number'
});
// add sum column for each team
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: team,
type: 'number'
});
});
// set view columns
view.setColumns(viewColumns);
then we can aggregate again, to collapse all the rows...
// sum view by date
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
see following working snippet...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
google.charts.load('current', {
packages: ['line']
}).then(function () {
// create data table
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('string','Team');
data.addColumn('number', 'Total Score');
data.addRows([
['Dec', 'Team A', 68],
['Dec', 'Team B', 75],
['Dec', 'Team C', 23],
['Dec', 'Team D', 158],
['Dec', 'Team E', 27],
['Jan', 'Team A', 68],
['Jan', 'Team E', 27]
]);
// group data table
var groupData = google.visualization.data.group(
data,
[0, 1],
[{
column: 2,
aggregation: google.visualization.data.sum,
type: 'number'
}]
);
// create data view
var view = new google.visualization.DataView(groupData);
// sum column array
var aggColumns = [];
// use month as first view column
var viewColumns = [0];
// build view & agg columns for each team
groupData.getDistinctValues(1).forEach(function (team, index) {
// add view column for each team
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === team) {
return dt.getValue(row, 2);
}
return null;
},
label: team,
type: 'number'
});
// add sum column for each team
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: team,
type: 'number'
});
});
// set view columns
view.setColumns(viewColumns);
// sum view by date
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
// draw chart
var lineChart = new google.charts.Line(document.getElementById('LineChart'));
lineChart.draw(aggData);
});
<!-- language: lang-html -->
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="LineChart"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论