我的数据格式 -
['Group','Count','Month','Year'],
['A',10,'February',2015],
['B',8,'February',2015],
['C',15,'February',2016]
我将使用过滤器显示按组列分隔的每个月的数据。
X轴将拥有群组。 Y轴将为所有年份(2014年,2015年,2016年......)提供计数。
我正在使用Google Dashboard。我的代码 -
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard4_div'));
var slider = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'filter4_div',
'options': {
'filterColumnLabel': 'Month',
'ui': {
'allowTyping': true,
'allowMultiple': false,
'allowNone': false,
'sortValues': false,
'label': 'Choose month',
}
}
});
var ColumnChart = new google.visualization.ChartWrapper({
'chartType': 'ColumnChart',
'containerId': 'chart4_div',
// How to take in two column values
});
dashboard.bind(slider, ColumnChart);
// Draw the dashboard.
dashboard.draw(data);
我想知道如何使用Google Charts在我的图表中添加两个列值
答案 0 :(得分:1)
因为在绘制图表之前数据需要操作,所以 独立绘制类别过滤器和图表
使用slider
的原始数据表
然后,当'ready'
上发生'statechange'
或slider
事件时,
使用selectedValues
过滤行
一旦过滤,请使用data.group
将年份从行转换为列
请参阅以下工作代码段...
google.charts.load('current', {
callback: function () {
var data = google.visualization.arrayToDataTable([
['Group', 'Count', 'Month', 'Year'],
['A', 10, 'February', 2015],
['B', 8, 'February', 2015],
['C', 15, 'February', 2016],
['A', 7, 'February', 2016],
['B', 5, 'February', 2016],
['C', 12, 'February', 2015],
['A', 20, 'March', 2015],
['B', 16, 'March', 2015],
['C', 30, 'March', 2016],
['A', 14, 'March', 2016],
['B', 10, 'March', 2016],
['C', 24, 'March', 2015]
]);
var slider = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'filter_div',
'dataTable': data,
'options': {
'filterColumnLabel': 'Month',
'ui': {
'allowTyping': true,
'allowMultiple': false,
'allowNone': false,
'sortValues': false,
'label': 'Choose month',
}
}
});
google.visualization.events.addListener(slider, 'ready', drawChart);
google.visualization.events.addListener(slider, 'statechange', drawChart);
function drawChart() {
var sliderData = new google.visualization.DataView(slider.getDataTable());
sliderData.setRows(sliderData.getFilteredRows([{
column: 2,
value: slider.getState().selectedValues[0]
}]));
// group by 'Group' / 'Year'
var dataGroup = google.visualization.data.group(
sliderData,
[0, 3],
[{column: 1, aggregation: google.visualization.data.sum, type: 'number', label: 'Count'}]
);
dataGroup.sort([{column: 0},{column: 1}]);
// build final data table
var yearData = new google.visualization.DataTable({
cols: [
{label: 'Group', type: 'string'}
]
});
// add column for each year
var years = dataGroup.getDistinctValues(1);
for (var i = 0; i < years.length; i++) {
yearData.addColumn(
{label: years[i], type: 'number'}
);
}
// add row for each month
var rowMonth = null;
var rowIndex = null;
for (var i = 0; i < dataGroup.getNumberOfRows(); i++) {
if (rowMonth !== dataGroup.getValue(i, 0)) {
rowMonth = dataGroup.getValue(i, 0);
rowIndex = yearData.addRow();
yearData.setValue(rowIndex, 0, rowMonth);
}
for (var x = 1; x < yearData.getNumberOfColumns(); x++) {
if (yearData.getColumnLabel(x) === dataGroup.getValue(i, 1).toString()) {
yearData.setValue(rowIndex, x, dataGroup.getValue(i, 2));
}
}
}
var view = new google.visualization.DataView(yearData);
view.setColumns([0, 1, {
calc: 'stringify',
sourceColumn: 1,
type: 'string',
role: 'annotation'
}, 2, {
calc: 'stringify',
sourceColumn: 2,
type: 'string',
role: 'annotation'
}]);
var ColumnChart = new google.visualization.ChartWrapper({
'chartType': 'ColumnChart',
'containerId': 'chart_div',
'dataTable': view
});
ColumnChart.draw();
}
slider.draw();
},
packages: ['controls', 'corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard_div">
<div id="filter_div"></div>
<div id="chart_div"></div>
</div>