Reputation: 8369
I've a google line chart with three lines. I'm displaying months on haxis as labels. However as the chart is continuing drawing the lines, the labels are getting disappeared/removed. How to avoid this?
Below is my chart code:
google.charts.load('current', {
packages: ['corechart']
});
google.setOnLoadCallback(prepareChartData);
function prepareChartData(){
var chartData = new google.visualization.DataTable();
chartData.addColumn('date', 'Date');
chartData.addColumn('number', 'Total');
chartData.addColumn('number', 'Dogs');
chartData.addColumn('number', 'Cats');
title = 'My Chart';
var options = {
title: title,
curveType: 'function',
legend: {position: 'bottom', alignment: 'start'},
colors: ['#003f5c', '#ffa600', '#665191', '#f95d6a'],
chartArea: {
bottom: 80
},
annotations: {
alwaysOutside: true,
textStyle: {
color: 'black',
fontSize: 11
},
},
hAxis: {
format: 'MMM yy',
viewWindowMode: "explicit",
},
vAxis: {
minValue: 0,
viewWindowMode: "explicit",
viewWindow: { min: 0 },
title: ''
},
titleTextStyle: {
color:'#3a3a3a',
fontSize:24,
bold:false
// fontName: "Segoe UI"
},
bar: {groupWidth: '95%'},
bars: 'horizontal'
};
var chartDivId = "chart_div";
var chart = new google.visualization.LineChart(document.getElementById(chartDivId));
var rawData =[];
var chart_object = { "Dec 19": {monthLabel: "Dec", chartArray:[{'date': "2019-12-31", 'total': "5", 'cats': "10", 'dogs': "10"}]},"Jan 20": {monthLabel: "Jan", chartArray:[{'date': "2020-1-01", 'total': "5", 'cats': "10", 'dogs': "10"}]},"Feb 20": {monthLabel: "Feb", chartArray:[{'date': "2020-2-29", 'total': "5", 'cats': "10", 'dogs': "10"}]}, "Mar 20": {monthLabel: "Mar", chartArray:[{'date': "2020-3-01", 'total': "5", 'cats': "10", 'dogs': "10"},{'date': "2020-03-12", 'total': "15", 'cats': "30", 'dogs': "30"}]}};
$.each(chart_object, function(i, chartobject) {
$.each( chartobject.chartArray, function( chartIndex , chartValue ){
date = chartValue['date'];
total = parseInt(chartValue['total']);
catscount = parseInt(chartValue['cats']);
dogscount = parseInt(chartValue['dogs']);
catspercentage = 0;
catspercentageAnnotation = catscount+", percent "+catspercentage+"%";
dogsspercentage = 0;
dogsspercentageAnnotation = dogscount+", percent "+dogsspercentage+"%";
rawData.push([ new Date(date), total, {v: catscount, f: catspercentageAnnotation}, {v: dogscount, f: dogsspercentageAnnotation}]);
});
});
var counter = 0;
drawChart();
function drawChart() {
if(counter < rawData.length){
chartData.addRow(rawData[counter]);
// build x-axis ticks to prevent repeated labels
var dateFormat = new google.visualization.DateFormat({
pattern: 'yyyy-MM-dd'
});
var dateRange = chartData.getColumnRange(0);
var ticks = [];
var dateTick = dateRange.min;
while (dateTick.getTime() <= dateRange.max.getTime()) {
if (ticks.length === 0) {
// format first tick
ticks.push({
v: dateTick,
f: dateFormat.formatValue(dateTick)
});
} else {
ticks.push(dateTick);
}
dateTick = new Date(dateTick.getFullYear(), dateTick.getMonth() + 1, 1);
}
options.hAxis.ticks = ticks;
chart.draw(chartData, options);
counter++;
window.setTimeout(drawChart, 1000);
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript" ></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Am I missing any chart option? How can I display the months at the month beginning itself without getting removed?
Can anyone help me to fix this? Thanks in advance.
Upvotes: 1
Views: 1113
Reputation: 1606
You have a few options:
{
options: {
hAxis: {
maxAlternation: 4
}
}
}
Documentation description:
Maximum number of levels of horizontal axis text. If axis text labels become too crowded, the server might shift neighboring labels up or down in order to fit labels closer together. This value specifies the most number of levels to use; the server can use fewer levels, if labels can fit without overlapping. For dates and times, the default is 1.
This will allow the labels to alternate among 4 different levels, thus allowing all of the to coexist. You can replace 4 with another number you find adequate. Also, you might need to tick with the chart area to make space for the extra lines of labels.
{
options: {
hAxis: {
slantedTextAngle: 90
}
}
}
Documentation description:
The angle of the horizontal axis text, if it's drawn slanted. Ignored if hAxis.slantedText is false, or is in auto mode, and the chart decided to draw the text horizontally. If the angle is positive, the rotation is counter-clockwise, and if negative, it is clockwise.
This will make the labels be written vertically, so each label will only utilize a very small space at the axis.
{
options: {
chartArea: {
bottom: 80
}
}
}
Documentation description:
An object with members to configure the placement and size of the chart area (where the chart itself is drawn, excluding axis and legends). Two formats are supported: a number, or a number followed by %. A simple number is a value in pixels; a number followed by % is a percentage. Example: chartArea:{left:20,top:0,width:'50%',height:'75%'}
By increase the free space at the bottom of your chart, you will allow the slanted text to work automatically, by tilting your labels "just enough".
Upvotes: 1