I’m trying to make a bar chart using Google Charts with dependent controls. The problem I am having concerns inputting the data in a format that is usable for my task.
Here is an example of the data I want to use:
‘Option1heading’, ‘Option2heading’, ‘Option3heading’, ‘val1’, ‘val2’, ‘val3’, ‘val4’, ‘val5’, ‘val6’
‘Row1val1’, ‘Row1val2’, ‘Row1val3’, 1336060, 1538156, 1576579, 1600652, 1968113, 123345
‘Row2val1’, ‘Row2val2’, ‘Row2val3’, 400361, 366849, 440514, 434552, 393032, 234374
‘Row3val1’, ‘Row3val2’, ‘Row3val3’, 1001582, 1119450, 993360, 1004163, 979198, 578236
‘Row4val1’, ‘Row4val2’, ‘Row4val3’, 997974, 941795, 930593, 897127, 108087, 4893
The first row in this example contains the options that I want to filter on ‘Option1heading’, ‘Option2heading’ and ‘Option3heading’. In reality these might be something like ‘country’, ‘region’, ‘state’. The second row onwards then contains the data, ‘Row1val1’, ‘Row1val2’, ‘Row1val3’ being the filter information (e.g. ‘France’, ‘North’, ‘Paris’).
Following that, the 6 numeric values would be individual bars of data for that row. In the legend for this example these would equal ‘val1’ – ‘val6’ (as per the first row). In reality these might be things like ‘population’, ‘Male’, ‘female’, ‘0-10 years’ etc.
Here is the code as it currently stands. It ‘kind of’ works but is not working correctly. Is this even possible and can anyone point me in the right direction in order to do it?
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery.dump.js"></script>
<script type="text/javascript">
// Load the Visualization API and the controls package.
google.load('visualization', '1.1', {'packages':['corechart', 'controls']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawDashboard);
// Callback that creates and populates a data table,
// instantiates a dashboard, a range slider and a pie chart,
// passes in the data and draws it.
function drawDashboard() {
var data = new google.visualization.DataTable();
var raw_data = [['Option1', 'Option2', 'option3', 'val 1', 'val 2', 'val 3', 'val 4', 'val 5', 'val 6'],
['Ford', 's', 'm', 1336060, 1538156, 1576579, 1600652, 1968113, 123345],
['Citroen', 's', 'm', 400361, 366849, 440514, 434552, 393032, 234374],
['BMW', 's', 'm', 1001582, 1119450, 993360, 1004163, 979198, 578236],
['Toyota', 's', 'm', 997974, 941795, 930593, 897127, 108087, 4893]];
var my_rows = ['Row1', 'Row2', 'Row3', 'Row4', 'Row5', 'Row6'];
data.addColumn('string', 'Year');
for (var i = 0; i < raw_data.length; ++i) {
data.addColumn('number', raw_data[i][0]);
}
data.addRows(my_rows.length);
for (var j = 0; j < my_rows.length; ++j) {
data.setValue(j, 0, my_rows[j].toString());
}
for (var i = 1; i < raw_data.length; ++i) {
for (var j = 3; j < raw_data[i].length; ++j) {
data.setValue(j-3, i+1, raw_data[i][j]);
}
}
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
var controlPicker1 = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'control1',
'options': {
'filterColumnLabel': 'Ford',
'ui': {
'labelStacking': 'horizontal',
'allowTyping': false,
'allowMultiple': true
}
}
});
var controlPicker2 = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'control2',
'options': {
'filterColumnLabel': 'Citroen',
'ui': {
'labelStacking': 'horizontal',
'allowTyping': false,
'allowMultiple': true
}
}
});
var controlPicker3 = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'control3',
'options': {
'filterColumnLabel': 'BMW',
'ui': {
'labelStacking': 'horizontal',
'allowTyping': false,
'allowMultiple': true
}
}
});
var barChart = new google.visualization.ChartWrapper({
'chartType': 'BarChart',
'containerId': 'chart_div',
'options': {
'width': '100%',
'height': '100%',
'vAxis': {title: "Year"},
'hAxis': {title: "Cups"},
'fontSize': 14,
'chartArea': {top: 0, right: 0, bottom: 0, height:'100%', width:'70%'}
},
// Configure the barchart to use columns 2 (City) and 3 (Population)
});
google.visualization.events.addListener(dashboard, 'ready', function() {
// Dashboard redraw, have a look at how many rows the barChart is displaying
var numRows = barChart.getDataTable().getNumberOfRows();
var expectedHeight = (numRows * 60)+50;
if (parseInt(barChart.getOption('height'), 10) != expectedHeight) {
// Update the chart options and redraw just it
Div("chart_div", expectedHeight);
barChart.setOption('height', expectedHeight);
barChart.draw();
}
});
// Establish dependencies, declaring that 'filter' drives 'pieChart',
// so that the pie chart will only display entries that are let through
// given the chosen slider range.
dashboard.bind(controlPicker1, controlPicker2);
dashboard.bind(controlPicker2, controlPicker3);
dashboard.bind(controlPicker3, barChart);
// Draw the dashboard.
dashboard.draw(data);
}
function Div(id,h) {
var div=document.getElementById(id);
h = (h) + "px";
var w=parseInt(div.style.width);
if($(this).width() >= 1200){
w = 1200 + "px";
}else{
w = ($(this).width()-30) + "px";
}
$(div).height(h);
$(div).width(w);
}
</script>
</head>
<style>
#chart_div { width: 1200px; height: 30000px; }
</style>
<body>
<!--Div that will hold the dashboard-->
<div id="dashboard_div">
<!--Divs that will hold each control and visualization-->
<div id="control1"></div>
<div id="control2"></div>
<div id="control3"></div>
<div id="chart_div"></div>
</div>
</body>
</html>
Many thanks in advance for any help you can provide.
The JSON data structure should be something like this:
There’s more info in the google.visualization.DataTable reference docs.