Show horizontal gridlines between series on bar chart - charts

I have a bar chart that compares a budget with the money spent, for diferent categories.
The problem is, budget and progress can be zero, so the groups are hard to figure out, as can be seen in this JSFiddle.
It would be great to have a gridline that separates the groups, more obviously than with just empty space.
I can do it vertically, but it doesn't work horizontally.
hAxis: {
title: '$',
minValue: 0,
gridlines: {color: 'cyan'}
},
vAxis: {
title: 'Food',
gridlines: {color: 'red'}
}
Can anyone help?

gridlines are only supported by a continuous axis (number, date, etc.)
they will not appear for a discrete axis (string)
in the data table, you can use object notation in order to use a continuous axis,
but still display a string value. ({v: 4, f: 'Pizza'})
['Food', 'Budgeted', 'Spent'],
[{v: 4, f: 'Pizza'}, 8175000, 7854695],
[{v: 3, f: 'Hamburger'}, 0, 2792000],
[{v: 2, f: 'Milkshake'}, 2695000, 0],
[{v: 1, f: 'Paella'}, 0, 0]
where v: is the value, and f: is the formatted value.
the value is used to control the order,
the formatted value will be displayed in the tooltip.
then, in order to display the labels on the axis,
we must use object notation in the ticks option.
ticks: [
{v: 0.5, f: ''},
{v: 1, f: 'Paella'},
{v: 1.5, f: ''},
{v: 2, f: 'Milkshake'},
{v: 2.5, f: ''},
{v: 3, f: 'Hamburger'},
{v: 3.5, f: ''},
{v: 4, f: 'Pizza'},
{v: 4.5, f: ''}
],
the ticks with the half value will add a line between.
but you won't be able to add grid lines only between.
because we must add a tick for the label itself.
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Food', 'Budgeted', 'Spent'],
[{v: 4, f: 'Pizza'}, 8175000, 7854695],
[{v: 3, f: 'Hamburger'}, 0, 2792000],
[{v: 2, f: 'Milkshake'}, 2695000, 0],
[{v: 1, f: 'Paella'}, 0, 0]
]);
var options = {
title: 'Food budgeted vs food spent',
bar: {
groupWidth: '75%',
},
chartArea: {width: '50%'},
hAxis: {
title: '$',
minValue: 0,
gridlines: {color: 'red'}
},
vAxis: {
title: 'Food',
ticks: [
{v: 0.5, f: ''},
{v: 1, f: 'Paella'},
{v: 1.5, f: ''},
{v: 2, f: 'Milkshake'},
{v: 2.5, f: ''},
{v: 3, f: 'Hamburger'},
{v: 3.5, f: ''},
{v: 4, f: 'Pizza'},
{v: 4.5, f: ''}
],
gridlines: {color: 'red'}
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

How to makes lines on Google Charts Linechart to start from point 0, 0 (angular-google-charts)

I would like to make the lines of the linechart to start from the left-bottom part of the graph and then go up to the first item (right), instead of just starting from the first item (left).
I tried adding a [null, 0, 0], but that just creates a new column, which adds some space between the beginning of the HAxis and the beginning of the lines.
Demo
in order for the line to begin at zero, at point must exist at 0, 0
but I think you will have better luck using an x-axis that is continuous (numeric) vs. discrete (string)
we can use object notation to provide a numeric value while still displaying a string
where v is the value and f is the formatted value
{v: 1, f: 'Value 1'}
we can use this in both the data table values, as well as the axis ticks.
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Values', 'Line 1', 'Line 2'],
[{v: 0, f: ''}, 0, 0],
[{v: 1, f: 'Value 1'}, 20, 10],
[{v: 2, f: 'Value 2'}, 40, 50],
[{v: 3, f: 'Value 3'}, 50, null]
]);
var options = {
hAxis: {
ticks: [ // ticks to display on the x-axis
{v: 0, f: ''},
{v: 1, f: 'Value 1'},
{v: 2, f: 'Value 2'},
{v: 3, f: 'Value 3'},
{v: 4, f: ''}
],
title: 'HAxis'
},
vAxis: {
title: 'VAxis'
},
curveType: 'function'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
(obviously, the above snippet is not angular, but works the same...)

Google Charts - placing custom formatting axis values

I have some numeric data (e.g. "1, 2, 3, 4, 5..") based on which I want to build a chart. However, instead of raw numbers, I want to show other text as axis values.
For example, instead of number "1" I want to display "one"
Is that possible to do with Google Charts?
using object notation, you can provide the value (v:) and the formatted value (f:) for each tick
{v: 1, f: 'one'}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]
], true);
var options = {
hAxis: {
ticks: [
{v: 1, f: 'one'},
{v: 2, f: 'two'},
{v: 3, f: 'three'},
{v: 4, f: 'four'},
{v: 5, f: 'five'}
]
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

How to set hAxis as date format in Google Chart?

I use Google Chart as DataTable.
I try to make hAxis like as date format:
hAxis: {
title: '',
format: 'date',
viewWindow: {
min: [7, 30, 0],
max: [17, 30, 0]
}
}
And add rows as:
data.addRows([
[{v: [8, 0, 0], f: '03/02/13'}, 1],
[{v: [9, 0, 0], f: '04/02/13'}, 2],
[{v: [10, 0, 0], f:'05/02/13'}, 3],
[{v: [11, 0, 0], f: '06/02/13'}, 4],
[{v: [12, 0, 0], f: '07/02/13'}, 5]
]);
I need to get in horizontal line(hAxis) titles in date format: dd/mm/yy
hAxis.format expects a pattern, or pattern name
so it would be something like...
hAxis: {
format: 'short'
// or
format: 'dd/MM/yy'
}
however, as pointed out in the comments, the column type appears to be timeofday
to avoid changing the column type, you can provide your own custom hAxis.ticks,
using the data provided to the chart
see following, working snippet. uses rawData to populate chart data and ticks...
google.charts.load('current', {
callback: function () {
var rawData = [
[{v: [8, 0, 0], f: '03/02/13'}, 1],
[{v: [9, 0, 0], f: '04/02/13'}, 2],
[{v: [10, 0, 0], f:'05/02/13'}, 3],
[{v: [11, 0, 0], f: '06/02/13'}, 4],
[{v: [12, 0, 0], f: '07/02/13'}, 5]
];
var data = new google.visualization.DataTable();
data.addColumn({label: 'Date', type: 'timeofday'});
data.addColumn({label: 'Count', type: 'number'});
data.addRows(rawData);
var hTicks = [];
for (var i = 0; i < rawData.length; i++) {
hTicks.push(rawData[i][0]);
}
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {
hAxis: {
ticks: hTicks,
title: '',
viewWindow: {
min: [7, 30, 0],
max: [17, 30, 0]
}
}
});
},
packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

How to deal with 3 column recordset in Google Charts?

I have a MySQL recordset like:
Date;Customer;Turnover
2016-03-01;Cust1;120
2016-03-01;Cust2;120
2016-04-01;Cust1;130
2016-04-01;Cust2;125
2016-05-01;Cust1;135
2016-05-01;Cust2;110
My goal is to have a stacked bar chart with Year/Month on the x-axis (type of date so I can use date filters), and customer values stacked on top of each other. In addition I need to filter the customers I want to hide from the display.
I've been trying to group/aggregate the data, also played with dashboard filter controls, but I haven't made a breakthrough yet so I was hoping someone might give me a hint on where to start with this.
in order to display the date on the x-axis, you need a column for each customer
Group the data by Date, Customer
Sort grouped data by Date, Customer
Get unique Customer Id's
Build new table with column for each Customer
see example, taken from the provided fiddle...
google.charts.load('current', {
callback: drawVisualization,
packages:['corechart', 'table', 'controls']
});
function drawVisualization() {
var data = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
{id: 'user-id', label: 'User-Id', type: 'string'},
{id: 'customer-id', label: 'Customer-Id', type: 'string'},
{id: 's_minutes', label: 'minutes', type: 'number'}
],
rows: [
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '67205'}, {v: 1122} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '67205'}, {v: 332} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '228626'}, {v: 334} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '67205'}, {v: 554} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '67205'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '228626'}, {v: 544} ]},
]
});
// group data by date, customer
var grouped_data = google.visualization.data.group(
data,
[0, 2],
[
{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}
]
);
// sort grouped data
grouped_data.sort([{column: 0},{column: 1}]);
// get unique customers
var custGroup = google.visualization.data.group(
data,
[2]
);
// build customer data table
var custData = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
]
});
// add column for each customer
for (var i = 0; i < custGroup.getNumberOfRows(); i++) {
custData.addColumn(
{label: custGroup.getValue(i, 0), type: 'number'}
);
}
// add rows for each date
var rowDate;
var rowIndex;
for (var i = 0; i < grouped_data.getNumberOfRows(); i++) {
// add row when date changes
if (rowDate !== grouped_data.getValue(i, 0).getTime()) {
rowDate = grouped_data.getValue(i, 0).getTime();
rowIndex = custData.addRow();
custData.setValue(rowIndex, 0, new Date(rowDate));
}
// find customer column
for (var x = 1; x < custData.getNumberOfColumns(); x++) {
if (custData.getColumnLabel(x) === grouped_data.getValue(i, 1)) {
custData.setValue(rowIndex, x, grouped_data.getValue(i, 2));
}
}
}
var options = {
hAxis: {title: '', textStyle: { fontSize: '13' }, textPosition: 'in'},
vAxis: {title: '', textStyle: { fontSize: '10' }},
seriesType: 'bars',
legend: {position: 'top', textStyle: {color: 'black', fontSize: 14}},
isStacked: true
};
var tableTE = new google.visualization.Table(document.getElementById('table_div_te'));
tableTE.draw(data, {showRowNumber: true, frozenColumns: 1, width: '100%', height: '300'});
var tableTEAgg = new google.visualization.Table(document.getElementById('table_div_teagg'));
tableTEAgg.draw(custData, {showRowNumber: true, frozenColumns: 1, width: '100%', height: '300'});
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(custData, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table_div_te"></div>
<div id="table_div_teagg"></div>
<div id="chart_div" style="width: 1600px; height: 300px;"></div>

Adding labels to google scatter charts

I am using google charts API to draw scatter, is there a way to label each node in the scatter diagram. I guess it only uses the numeric values.....Any other tools I can use instead.
Example:
Name Age Response
Allen 12 40
Tom 16 45
Sim 17 60
X and y axis will be age and response respectively, but each node on the graph can I label as Allen, Tom, Sim
I was having same issue to plot labels for points on chart itself.
Google chart have solved this problem now. You can add one more property as annotation. By which you can add labels.
See how it looks like. Generally I do annotation in number and then explain what those number are about.
var data = google.visualization.arrayToDataTable([
['Age', 'Weight', {role: 'annotation'}],
[ 8, 12, 'Point 1'],
[ 1, 5.5, 'Point 2'],
[ 11, 14, 'Point 3'],
[ 4, 5, 'Point 4'],
[ 3, 3.5, 'Point 5'],
[ 6.5, 7, 'Point 6']
]);
You can label the points in a Google ScatterChart by adding a tooltip. Tooltips show up when you mouseover a data point.
The code for your data table should look something like this:
var dt = new google.visualization.DataTable(
{
cols: [{id: 'A', label: 'Age', type: 'number'},
{id: 'B', label: 'Response', type: 'number'},
{id: 'C', label: 'Name', type:'tooltip', p:{role:'tooltip'}}
],
rows: [{c:[{v: 12}, {v: 40}, {v:'Allen'}]},
{c:[{v: 16}, {v: 45}, {v:'Tom'}]},
{c:[{v: 17}, {v: 60}, {v:'Sim'}]}
]
},
0.6
)
When you mouseover the points, the name will show up.
Link to Tooltips:
https://developers.google.com/chart/interactive/docs/roles#tooltiprole
Link to DataTable class (for formatting data):
https://developers.google.com/chart/interactive/docs/reference#DataTable
NOTE: if you're trying to plot multiple data series, you have to specify a tooltip for each one. For example, if you add a separate data series for Average Response, the code would change to:
var dt = new google.visualization.DataTable(
{
cols: [{id: 'A', label: 'Age', type: 'number'},
{id: 'B', label: 'Response', type: 'number'},
{id: 'C', label: 'Name', type:'tooltip', p:{role:'tooltip'}},
{id: 'D', label: 'AvgResp', type: 'number'},
{id: 'E', label: 'Category', type:'tooltip', p:{role:'tooltip'}}
],
rows: [{c:[{v: 12}, {v: 40}, {v:'Allen'}, {v:null}, {v:null}]},
{c:[{v: 16}, {v: 45}, {v:'Tom'}, {v:null}, {v:null}]},
{c:[{v: 17}, {v: 60}, {v:'Sim'}, {v:null}, {v:null}]},
{c:[{v: 12}, {v: null}, {v:null}, {v: 35}, {v:'Avg. 12yo'}]},
{c:[{v: 16}, {v: null}, {v:null}, {v: 48}, {v:'Avg. 16yo'}]},
{c:[{v: 17}, {v: null}, {v:null}, {v: 52}, {v:'Avg. 17yo'}]}
]
},
0.6
)
To do it using the visualization API, just use a cell_object (https://google-developers.appspot.com/chart/interactive/docs/reference#cell_object). The google API playground was useful for me, might be for you: https://code.google.com/apis/ajax/playground
Here's an example of some code:
var data = google.visualization.arrayToDataTable([
['Age','Response'],
[ {v:12, f: 'Allen'}, 40],
[ {v:16, f: 'Tom'}, 45],
[ {v:17, f: 'Sim'}, 60]
]);
Hope that helps!
https://developers.google.com/chart/image/docs/gallery/scatter_charts#chart_types
This page documents coloring the plots and adding a legend.
With Google charts you can't, short of using the legend and cross referencing. I don't know of any scatter chart creator that will label the individual plots.