How do I eliminate the redundant labels across the hAxis?
My dataset has 8 first of month dates representing the last 8 quarters. I'm getting a lot of extra hAxis labels that I have not been able to remove. I've tried working with the gridline settings but had no luck cleaning the chart up. My goal is one label per bar.
Can someone please point me in the correct direction?
As always thank you for the assitance!
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Time of Day');
data.addColumn('number', 'Rating');
data.addColumn('number', 'Goal');
data.addRows([
[new Date(2023, 0, 1), 5, 6],
[new Date(2023, 3, 1), 7, 6],
[new Date(2023, 6, 1), 3, 6],
[new Date(2023, 9, 1), 1, 6],
[new Date(2024, 0, 1), 5, 6],
[new Date(2024, 3, 1), 7, 6],
[new Date(2024, 6, 1), 3, 6],
[new Date(2024, 9, 1), 1, 6],
]);
var options = {
title: 'Rate the Day on a Scale of 1 to 10',
height: 400,
hAxis: {
slantedText: true,
slantedTextAngle: 90,
type: "discrete",
format: 'yyyy-QQ',
//gridlines: { count: -1 },
//minorGridlines: {count: 0},
},
vAxis: {
minValue: 0
},
seriesType: 'bars',
series: {
1: {
type: 'line'
}
},
trendlines: {
0: {
type: "linear"
}
},
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
the problem results from the x-axis being a continuous axis (date)
the chart is displaying different dates along the x-axis,
however, the format of the date causes duplicate labels to be displayed.
there are a couple ways to resolve...
1) provide custom ticks for the x-axis, to display one label per bar
2) convert the date column to a string, which results in a Discrete axis
following are examples of both...
1) provide custom ticks for the x-axis, to display one label per bar
the
ticksconfiguration option takes an array of values,which must be of the same data type as the axis (date)
as such, build an array of values from the first data table column
then use those ticks in the
hAxisoptionssee following working snippet...
2) convert the date column to a string, which results in a Discrete axis
first, change the data type of the first data table column to string
next, we'll need a date formatter to convert the date to the desired format
then, we'll hold the data in a separate variable and convert the date to string before adding the rows to the data table
see following working snippet...