How to set hAxis as date format in Google Chart? - charts

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>

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 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>

Remove decimal values from the axis

I'm using google charts and I have an issue with formatting of the hAxis.
It looks like this when there are only 4 values on it:
I want to remove those decimal numbers between the real values. I don't want it to show 0.5,1.5,2.5,3.5
How can it be done?
there are a few options that could be used, if you were using a classic chart
but those are not supported using material charts
you can use option hAxis.format to change the number format,
but this will only remove the decimal and cause the numbers to repeat
the only method i've found when using a matieral chart,
is to use string values for the x-axis...
['Lesson', 'Average', 'Average best'],
['1', 10, 10],
['2', 10, 10],
['3', 10, 10],
['4', 7, 7.5]
or you can use a DataView to convert the numbers to strings...
var data = google.visualization.arrayToDataTable([
['Lesson', 'Average', 'Average best'],
[1, 10, 10],
[2, 10, 10],
[3, 10, 10],
[4, 7, 7.5]
]);
var view = new google.visualization.DataView(data);
view.setColumns([{
calc: function (dt, row) {
return dt.getValue(row, 0).toFixed(0);
},
label: data.getColumnLabel(0),
type: 'string'
}, 1, 2]);
see following working snippet...
google.charts.load('current', {
packages: ['bar']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Lesson', 'Average', 'Average best'],
[1, 10, 10],
[2, 10, 10],
[3, 10, 10],
[4, 7, 7.5]
]);
var view = new google.visualization.DataView(data);
view.setColumns([{
calc: function (dt, row) {
return dt.getValue(row, 0).toFixed(0);
},
label: data.getColumnLabel(0),
type: 'string'
}, 1, 2]);
var options = {
chart: {
title: 'Results distribution by lesson',
},
height: 280
};
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(view, google.charts.Bar.convertOptions(options));
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

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>

One trendline for multiple series

Hello I have this simple example with 2 data series and 2 trendiness.
http://jsfiddle.net/ttwqazav/
I want to have only 1 trendline for both of them, as an average! Is that possible?
google.load('visualization', '1.1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['X', 'Y', 'Z'],
[0, 0, 2],
[1, 2, 4],
[2, 4, 5],
[3, 6, 8],
[4, 8, 10],
[5, 10, 12],
[6, 12, 14],
[7, 14, 16],
[8, 16, 18],
[9, 18, 20],
[10, 20, 22]
]);
var options = {
height: 500,
legend: 'none',
colors: ['#9575cd', '#33ac71'],
pointShape: 'diamond',
trendlines: {
0: {
color:'red',
type: 'linear',
pointsVisible: false
}
}
};
var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
chart.draw(data, options);
}
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
Just specify data points as below:
var data = google.visualization.arrayToDataTable([
['X', 'Y'],
[0, 0],
[1, 2],
[2, 4],
[3, 6],
[4, 8],
[5, 10],
[6, 12],
[7, 14],
[8, 16],
[9, 18],
[10, 20],
[0, 2],
[1, 4],
[2, 5],
[3, 8],
[4, 10],
[5, 12],
[6, 14],
[7, 16],
[8, 18],
[9, 20],
[10, 22]
]);
But you cannot differentiate between the two sets of data points!
If your aim is to get average best fit line, it'll work. :)