Google Chart API Implementation - charts

Am using google API to implement chart in my Project. Am using Scatter chart.
I need to implement chart like below. How can Achieve this.? Is there any other way to Achieve this by using any other Open source chart.?
Sample Google Chart which I need
Additional requirement

you can use a ComboChart to combine scatter and area series
the area series should be stacked
set the color of the first layer to 'transparent'
use null for values where the series do not coincide
see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'area bottom');
data.addColumn('number', 'area top');
data.addColumn('number', 'scatter');
data.addRows([
[1.5, null, null, 1.5],
[3, 3, 3, null],
[6, 3, 3, null]
]);
var options = {
areaOpacity: 1,
colors: ['transparent', '#ff9900', '#3366cc'],
hAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
height: 320,
legend: {
position: 'none'
},
isStacked: true,
seriesType: 'area',
series: {
2: {
type: 'scatter'
}
},
title: 'Final Score',
vAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
width: 320
};
var chart = new google.visualization.ComboChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
UPDATE
just add another area layer for the new requirement...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'area bottom');
data.addColumn('number', 'area middle');
data.addColumn('number', 'area top');
data.addColumn('number', 'scatter');
data.addRows([
[1.5, null, null, null, 1.5],
[3, 3, 3, null, null],
[4.5, 3, 3, null, null],
[4.5, 3, 1.5, 1.5, null],
[6, 3, 1.5, 1.5, null]
]);
var options = {
areaOpacity: 1,
colors: ['transparent', '#ff9900', '#f8bbd0', '#3366cc'],
hAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
height: 320,
legend: {
position: 'none'
},
isStacked: true,
seriesType: 'area',
series: {
3: {
type: 'scatter'
}
},
title: 'Final Score',
vAxis: {
format: '#,##0.0',
ticks: [0, 1.5, 3, 4.5, 6],
title: 'FINAL SCORE'
},
width: 320
};
var chart = new google.visualization.ComboChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

Google Chart Animation not working with Stacked chart

I am trying to apply animation on google chart on first load, its working with normal bar chart but when I am making it stacked, Animation not working. Please help me out
Tried almost every option from documentation but nothing is helping out.
there is no error in console.
google.charts.load('current', { 'packages': ['bar'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Months', 'EL', 'CL'],
['Jan', 2, 4],
['Feb', 1, 4],
['Mar', 6, 1],
['Apr', 3, 5],
['May', 1, 4],
['Jun', 3, 4],
['Jul', 2, 5],
['Aug', 2, 4],
['Sep', 1, 4],
['Oct', 6, 1],
['Dec', 1, 4]
]);
var options = {
chart: {
title: 'Leave Info',
subtitle: 'Total EL and CL consumed in a year',
},
bars: 'vertical',
height: 300,
width: 500,
animation:{
startup: 'true',
duration: 1000,
easing: 'out'
},
bar: {groupWidth: "40%"},
isStacked: true,
series: {
0: { color: 'red' },
1: { color: '#999' }
}
};
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
With the great help by White-hat from comments above, I am able to get this result, Thought of posting it here if anyone else need the help
google.charts.load('current', {
callback: function() {
drawChart();
window.addEventListener('resize', drawChart, false);
},
packages: ['corechart']
});
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Months', 'EL', 'CL'],
['Jan', 2, 4],
['Feb', 1, 4],
['Mar', 6, 1],
['Apr', 3, 5],
['May', 1, 4],
['Jun', 3, 4],
['Jul', 2, 5],
['Aug', 2, 4],
['Sep', 1, 4],
['Oct', 6, 1],
['Dec', 1, 4]
]);
var options = {
animation: {
duration: 1000,
easing: 'linear',
startup: true
},
isStacked: true,
height: 600,
theme: 'material',
title: 'Company Performance'
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Google Charts: Animation doesn't work for one chart, pointing annotations in the title area

I'm a bit new with google charts, I was playing around to learn it but I got two problems with my chart:
1) I'm not sure why the second chart (red) displays without animation
2) How can I extend the annotation lines with the values to the top of my graph?
The best case would be that the values are shown under the chart title
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data1 = new google.visualization.DataTable();
data1.addColumn('number', 'x');
data1.addColumn('number', 'green');
data1.addRows([
[0.005, 3],
[0.006, 6],
[0.007, 5],
[0.008, 8],
[0.009, 2],
[0.010, 5],
[0.011, 5],
[0.012, 4],
[0.013, 8]
]);
var data2 = new google.visualization.DataTable();
data2.addColumn('number', 'x');
data2.addColumn('number', 'red');
data2.addRows([
[0.016, 5],
[0.017, 1],
[0.018, 3],
[0.019, 9],
[0.020, 4],
[0.021, 5],
[0.022, 7],
[0.023, 7],
[0.024, 3]
]);
var joinedData = google.visualization.data.join(data1, data2, 'full',
[[0, 0]], [1], [1]);
var options = {
title: 'Playground',
colors: ['#007F01', '#FE0002'],
interpolateNulls: true,
hAxis: {
title: 'Price',
titleTextStyle: {
color: '#333'
},
direction: 1,
format: 'decimal'
},
vAxis: {
direction: 1
},
orientation: 'horizontal',
// customize colum
series: {
0: {type: "area"},
1: {type: "area"},
},
// legend: {position : 'left'},
animation: {
startup: true,
duration: 1000,
easing: 'out',
}
};
var view = new google.visualization.DataView(joinedData);
view.setColumns([0,
1,
{
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2,
{
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
}
]);
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(view, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 100%; height: 500px;"></div>
</body>
</html>
Here you can run it:
jsfiddle
there is bug within google charts when working with data views and animation.
an easy fix is to convert the view back to a data table when drawing the chart...
view.toDataTable()
e.g.
chart.draw(view.toDataTable(), options);
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data1 = new google.visualization.DataTable();
data1.addColumn('number', 'x');
data1.addColumn('number', 'green');
data1.addRows([
[0.005, 3],
[0.006, 6],
[0.007, 5],
[0.008, 8],
[0.009, 2],
[0.010, 5],
[0.011, 5],
[0.012, 4],
[0.013, 8]
]);
var data2 = new google.visualization.DataTable();
data2.addColumn('number', 'x');
data2.addColumn('number', 'red');
data2.addRows([
[0.016, 5],
[0.017, 1],
[0.018, 3],
[0.019, 9],
[0.020, 4],
[0.021, 5],
[0.022, 7],
[0.023, 7],
[0.024, 3]
]);
var joinedData = google.visualization.data.join(data1, data2, 'full',
[[0, 0]], [1], [1]);
var options = {
title: 'Playground',
colors: ['#007F01', '#FE0002'],
interpolateNulls: true,
hAxis: {
title: 'Price',
titleTextStyle: {
color: '#333'
},
direction: 1,
format: 'decimal'
},
vAxis: {
direction: 1
},
orientation: 'horizontal',
// customize colum
series: {
0: {type: "area"},
1: {type: "area"},
},
// legend: {position : 'left'},
animation: {
startup: true,
duration: 1000,
easing: 'out',
}
};
var view = new google.visualization.DataView(joinedData);
view.setColumns([0,
1,
{
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2,
{
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
}
]);
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(view.toDataTable(), options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 100%; height: 500px;"></div>
UPDATE
as for extending the annotation lines,
you can change the length,
but this will just increase the size of each,
and they still will not be aligned at the top
annotations: {
stem: {
length: 10
}
},
the only way would be to modify the chart manually, after it draws
you can move the labels and increase the height of the lines
but this will create two issues
1) the chart will move them back, anytime there is interactivity, such as hovering the labels or data points
to fix, you can use a MutationObserver
this will override the settings, every time the chart tries to move them back
2) if you use chart method getImageURI to get an image of the chart, the changes will not appear
to fix this, use html2canvas to get the image instead of the chart method
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data1 = new google.visualization.DataTable();
data1.addColumn('number', 'x');
data1.addColumn('number', 'green');
data1.addRows([
[0.005, 3],
[0.006, 6],
[0.007, 5],
[0.008, 8],
[0.009, 2],
[0.010, 5],
[0.011, 5],
[0.012, 4],
[0.013, 8]
]);
var data2 = new google.visualization.DataTable();
data2.addColumn('number', 'x');
data2.addColumn('number', 'red');
data2.addRows([
[0.016, 5],
[0.017, 1],
[0.018, 3],
[0.019, 9],
[0.020, 4],
[0.021, 5],
[0.022, 7],
[0.023, 7],
[0.024, 3]
]);
var joinedData = google.visualization.data.join(data1, data2, 'full',
[[0, 0]], [1], [1]);
var options = {
annotations: {
stem: {
length: 10
}
},
title: 'Playground',
colors: ['#007f01', '#fe0002'],
interpolateNulls: true,
hAxis: {
title: 'Price',
titleTextStyle: {
color: '#333'
},
direction: 1,
format: 'decimal'
},
vAxis: {
direction: 1
},
orientation: 'horizontal',
// customize colum
series: {
0: {type: "area"},
1: {type: "area"},
},
// legend: {position : 'left'},
animation: {
startup: true,
duration: 1000,
easing: 'out',
}
};
var view = new google.visualization.DataView(joinedData);
view.setColumns([0,
1,
{
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2,
{
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
}
]);
var container = document.getElementById('chart_div');
var chart = new google.visualization.ComboChart(container);
var observer = new MutationObserver(moveAnnotations);
observer.observe(container, {
childList: true,
subtree: true
});
function moveAnnotations() {
var chartLayout = chart.getChartLayoutInterface();
var chartBounds = chartLayout.getChartAreaBoundingBox();
var labels = container.getElementsByTagName('text');
var labelSize = 0;
Array.prototype.forEach.call(labels, function(label) {
if (label.getAttribute('text-anchor') === 'middle') {
if (options.colors.indexOf(label.getAttribute('fill')) > -1) {
labelSize = (parseFloat(label.getAttribute('font-size')) / 2);
label.setAttribute('y', chartBounds.top + labelSize);
}
}
});
var stems = container.getElementsByTagName('rect');
Array.prototype.forEach.call(stems, function(stem) {
if ((parseInt(stem.getAttribute('height')) === options.annotations.stem.length) && (stem.getAttribute('fill') === '#999999')) {
var height = parseFloat(stem.getAttribute('y')) - chartBounds.top;
stem.setAttribute('height', height);
stem.setAttribute('y', chartBounds.top + labelSize);
}
});
}
chart.draw(view.toDataTable(), options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
notes:
1) the move function (moveAnnotations) uses colors from the options in order to identify the annotation labels from the rest of the chart labels
the chart changes all colors to lowercase, so had to change colors in options to lowercase
2) move function also uses option annotations.stem.length to identify the annotation lines

Google Column Chart display the vert value in the column bar?

How do i display the vertical number in a column bar??
This a simple Google Column Chart, how do i display the numbers in the column bars on top???
var data = new google.visualization.DataTable();
data.addColumn('string', 'Period Incident');
data.addColumn('number', '# of Incidents');
data.addRows([
['Mushrooms', 3],
['Onions', 4],
['Olives', 5],
['Zucchini', 11],
['Pepper', 7],
['Avocado', 4],
['Tomato', 5],
['Pepperoni', 2]
]);
// Set chart options
var options = {
'title':'How Much Pizza I Ate Last Night',
legend: { position: 'top', alignment: 'start' },
'width':570,
'height':420};
var chart = new google.visualization.ColumnChart(
document.getElementById('columnchartIncidents'));
chart.draw(data, options);
}
use the 'annotation' column role...
1) add annotation column to the data table...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Period Incident');
data.addColumn('number', '# of Incidents');
// add annotation column role
data.addColumn({type: 'string', role: 'annotation'});
data.addRows([
['Mushrooms', 3, '3'], // <-- add annotations to the data
['Onions', 4, '4'],
['Olives', 5, '5'],
['Zucchini', 11, '11'],
['Pepper', 7, '7'],
['Avocado', 4, '4'],
['Tomato', 5, '5'],
['Pepperoni', 2, '2']
]);
var options = {
title: 'How Much Pizza I Ate Last Night',
legend: {
position: 'top',
alignment: 'start'
},
width: 570,
height: 420
};
var chart = new google.visualization.ColumnChart(
document.getElementById('columnchartIncidents')
);
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchartIncidents"></div>
2) or use a data view to add a calculated column for the annotation role...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Period Incident');
data.addColumn('number', '# of Incidents');
data.addRows([
['Mushrooms', 3],
['Onions', 4],
['Olives', 5],
['Zucchini', 11],
['Pepper', 7],
['Avocado', 4],
['Tomato', 5],
['Pepperoni', 2]
]);
// create data view
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
calc: 'stringify',
role: 'annotation',
sourceColumn: 1,
type: 'string'
}]);
var options = {
title: 'How Much Pizza I Ate Last Night',
legend: {
position: 'top',
alignment: 'start'
},
width: 570,
height: 420
};
var chart = new google.visualization.ColumnChart(
document.getElementById('columnchartIncidents')
);
chart.draw(view, options); // <-- draw chart with view
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchartIncidents"></div>

Google Chart hide grid zero

Set to show only the 100 and -100 grids ticks: [-100, 100], but the horizontal 0 (zero) grid appears. How to hide?
<table class="columns">
<tr>
<th>Linear Scale</th>
</tr>
<tr>
<td><div id="linear_div"></div></td>
</tr>
</table>
<script>
google.charts.load('current', {'packages':['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Population');
data.addRows([
[new Date(1400, 1, 1), -44],
[new Date(1500, 1, 1), 33],
[new Date(1600, 1, 1), 88],
[new Date(1700, 1, 1), 100],
[new Date(1750, 1, 1), 200],
]);
var linearOptions = {
title: 'World Population Since 1400 A.D. in Linear Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
ticks: [-100, 100]
}
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
}
</script>
https://jsfiddle.net/dnsaudd8/
use following option to hide 0 (baseline)
baselineColor: 'transparent'
see following working snippet...
google.charts.load('current', {'packages':['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Population');
data.addRows([
[new Date(1400, 1, 1), -44],
[new Date(1500, 1, 1), 33],
[new Date(1600, 1, 1), 88],
[new Date(1700, 1, 1), 100],
[new Date(1750, 1, 1), 200]
]);
var linearOptions = {
baselineColor: 'transparent',
title: 'World Population Since 1400 A.D. in Linear Scale',
legend: 'none',
width: 450,
height: 500,
hAxis: {
title: 'Date'
},
vAxis: {
title: 'Population (millions)',
ticks: [-100, 100]
}
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="linear_div"></div>

How do I align date and values to gridlines in Google Chart?

I'm using Google Chart in my application with the following code (JSFiddle):
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'date');
data.addColumn('number', 'view');
data.addRows([
[new Date('2015-08-01'), 5],
[new Date('2015-08-02'), 7],
[new Date('2015-08-03'), 2],
[new Date('2015-08-04'), 16],
[new Date('2015-08-05'), 3],
[new Date('2015-08-06'), 6],
[new Date('2015-08-07'), 1]
]);
var options = {
title: 'view count',
width: 900,
height: 500,
hAxis: {
format: 'MM-dd',
gridlines: {count: 90}
},
vAxis: {
minValue: 0,
gridlines: {
color: '#f3f3f3',
count: 6
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
However, the chart does not match between date and gridlines:
How can I match (synchronize) grid and date?
Changing the date format (mm/dd/yyyy vs. yyyy-mm-dd) seems to get it to align...
google.load('visualization', '1', {
packages: ['corechart']
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'date');
data.addColumn('number', 'view');
data.addRows([
[new Date('08/01/2015'), 5],
[new Date('08/02/2015'), 7],
[new Date('08/03/2015'), 2],
[new Date('08/04/2015'), 16],
[new Date('08/05/2015'), 3],
[new Date('08/06/2015'), 6],
[new Date('08/07/2015'), 1]
]);
var options = {
title: 'view count',
width: 900,
height: 500,
hAxis: {
format: 'MM-dd',
gridlines: {
count: 90
}
},
vAxis: {
minValue: 0,
gridlines: {
color: '#f3f3f3',
count: 6
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>