How do I align Google Chart gridlines with my data points? - date

While I have read several similar SO posts (most notably this one, which is the reason for the first, commented-out dataArray in my snippet), I still cannot seem to make my gridlines along the horizontal axis align with my actual data points. As you can see from the snippet below, I have specified the hAxis.ticks. While it's no longer reflected in this snippet, I've also played around with hAxis.gridlines.count, hAxis.minorGridlines.count, and several other options, all to no avail.
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

appears to be an issue with the 'current' version of google charts.
if you go back to version '45', it appears to work as expected.
see following working snippet...
google.charts.load('45', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Ratings');
/* const dataArray = [
[new Date('2020-12-22'), 2],
[new Date('2020-01-05'), 6],
[new Date('2020-01-05'), 7],
[new Date('2020-02-02'), 8],
[new Date('2020-02-16'), 10],
[new Date('2020-03-01'), 5],
[new Date('2020-03-15'), 9],
] */
const dataArray = [
[new Date('12/22/2019'), 2],
[new Date('01/05/2020'), 6],
[new Date('01/19/2020'), 7],
[new Date('02/02/2020'), 8],
[new Date('02/16/2020'), 10],
[new Date('03/01/2020'), 5],
[new Date('03/15/2020'), 9],
];
data.addRows(dataArray);
const dataView = new google.visualization.DataView(data);
const chart = new google.visualization.LineChart(document.querySelector('#chart'));
chart.draw(dataView, {
pointSize: 5,
lineWidth: 3,
hAxis: {
format: "MMM d, yyyy",
ticks: dataArray.map(d => d[0])
}
});
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></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>

How to create discontinuous line chart with Google Chart

I want to create a discontinuous line chart like this.
To draw the chart I wrote like this.
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Number');
data.addRows([
[new Date(2017, 1, 20), 460],
[new Date(2017, 1, 27), 579],
[new Date(2017, 1, 27), null],
[new Date(2017, 2, 1), 679],
[new Date(2017, 2, 6), 352]
]);
var linearOptions = {
title: 'Discontinuous Line Chart'
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
}
I had to put a null value data to make it separate.
Is there a way to draw a chart like above from a data like these?
data.addRows([
[new Date(2017, 1, 20), 460, true],
[new Date(2017, 1, 27), 579, false],
[new Date(2017, 2, 1), 679, true],
[new Date(2017, 2, 6), 352, true]
]);
This is a source code in jsfiddle.
https://jsfiddle.net/7jkmovqs/
you can use a 'style' column role...
use stroke-opacity: 0.0; to create a gap in the line
see following working snippet...
here, null results in the default style...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Number');
data.addColumn({role: 'style', type: 'string'});
data.addRows([
[new Date(2017, 1, 20), 460, null],
[new Date(2017, 1, 27), 579, null],
[new Date(2017, 2, 1), 679, 'line {stroke-opacity: 0.0;}'],
[new Date(2017, 2, 6), 352, null]
]);
var linearOptions = {
title: 'Discontinuous Line Chart'
};
var linearChart = new google.visualization.LineChart(document.getElementById('linear_div'));
linearChart.draw(data, linearOptions);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="linear_div"></div>
note: there is a column role for 'certainty' that takes a boolean value...
data.addRows([
[new Date(2017, 1, 20), 460, true],
[new Date(2017, 1, 27), 579, false],
[new Date(2017, 2, 1), 679, true],
[new Date(2017, 2, 6), 352, true]
]);
but this will make the line dashed...

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 to remove Y axis (hAxis) label/title from Google Chart?

I read the API reference from google developer website. For material line chart, there is example from here. Could anyone tell me how to remove the bottom "Month"? I think it should be worked by some code like:
hAxis: {
title: ''
}
In addition, most of the hAxis and vAxis features don't work.
that is correct, the following option will remove the x-axis title...
hAxis: {
title: ''
},
just be sure to use the options conversion method for material charts...
//convert options
materialChart.draw(data, google.charts.Line.convertOptions(materialOptions));
see following working snippet...
google.charts.load('current', {'packages':['line', 'corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var button = document.getElementById('change-chart');
var chartDiv = document.getElementById('chart_div');
var data = new google.visualization.DataTable();
data.addColumn('date', 'Month');
data.addColumn('number', "Average Temperature");
data.addColumn('number', "Average Hours of Daylight");
data.addRows([
[new Date(2014, 0), -.5, 5.7],
[new Date(2014, 1), .4, 8.7],
[new Date(2014, 2), .5, 12],
[new Date(2014, 3), 2.9, 15.3],
[new Date(2014, 4), 6.3, 18.6],
[new Date(2014, 5), 9, 20.9],
[new Date(2014, 6), 10.6, 19.8],
[new Date(2014, 7), 10.3, 16.6],
[new Date(2014, 8), 7.4, 13.3],
[new Date(2014, 9), 4.4, 9.9],
[new Date(2014, 10), 1.1, 6.6],
[new Date(2014, 11), -.2, 4.5]
]);
var materialOptions = {
chart: {
title: 'Average Temperatures and Daylight in Iceland Throughout the Year'
},
width: 900,
height: 500,
series: {
// Gives each series an axis name that matches the Y-axis below.
0: {axis: 'Temps'},
1: {axis: 'Daylight'}
},
hAxis: {
title: ''
},
axes: {
// Adds labels to each axis; they don't have to match the axis names.
y: {
Temps: {label: 'Temps (Celsius)'},
Daylight: {label: 'Daylight'}
}
}
};
var classicOptions = {
title: 'Average Temperatures and Daylight in Iceland Throughout the Year',
width: 900,
height: 500,
// Gives each series an axis that matches the vAxes number below.
series: {
0: {targetAxisIndex: 0},
1: {targetAxisIndex: 1}
},
vAxes: {
// Adds titles to each axis.
0: {title: 'Temps (Celsius)'},
1: {title: 'Daylight'}
},
hAxis: {
ticks: [new Date(2014, 0), new Date(2014, 1), new Date(2014, 2), new Date(2014, 3),
new Date(2014, 4), new Date(2014, 5), new Date(2014, 6), new Date(2014, 7),
new Date(2014, 8), new Date(2014, 9), new Date(2014, 10), new Date(2014, 11)
]
},
vAxis: {
viewWindow: {
max: 30
}
}
};
function drawMaterialChart() {
var materialChart = new google.charts.Line(chartDiv);
materialChart.draw(data, google.charts.Line.convertOptions(materialOptions));
button.innerText = 'Change to Classic';
button.onclick = drawClassicChart;
}
function drawClassicChart() {
var classicChart = new google.visualization.LineChart(chartDiv);
classicChart.draw(data, classicOptions);
button.innerText = 'Change to Material';
button.onclick = drawMaterialChart;
}
drawMaterialChart();
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<button id="change-chart">Change to Classic</button>
<br><br>
<div id="chart_div"></div>
note: the conversion method will not work for all options,
many simply aren't supported...
see --> Tracking Issue for Material Chart Feature Parity
another route would be to use a classic chart with the following option...
theme: 'material'

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>