1
votes

I have an animated line chart with 3 lines.

Fiddle:

https://jsfiddle.net/t5rxamd7/

I'm displaying data on monthly basis like Dec, Jan 2020 etc. How can I display multiple values for each months. Currently my object is like this

var chart_object = {"Dec 19": {monthLabel: "Dec", date: "30-Dec-2019", total: "42", cats: "0", dogs: "55", catspercentage: "0", dogsspercentage: "131"}, "Jan 20": {monthLabel: "Jan 2020", date: "", total: "", cats: "", dogs: ""}, "Feb 20": {monthLabel: "Feb", date: "", total: "", cats: "", dogs: ""}, "Mar 20": {monthLabel: "Mar", date: "", total: "", cats: "", dogs: ""}};

For each month, instead of showing total, I want to show it on daily basis without changing the x-axis. ie, the chart line should show daily progress, but the x-axis should still show the same labels.

How can I do that? can anyone help me to do this?

Thanks in advance.

1
you can change the x-axis to datetime, but set format option on hAxis to display month (e.g. 'MMM yy')WhiteHat
@WhiteHat Can you please edit the jsfiddle or provide the code?Jenz

1 Answers

1
votes

first, only need package --> 'corechart' -- 'bar' & 'line' are for drawing material charts

change x-axis column to date type...

var chartData = new google.visualization.DataTable();
chartData.addColumn('date', 'Date');  <-- change to date

add hAxis option format: 'MMM yy'
remove hAxis options for minValue & viewWindow
if you want to replace, they will need to be dates, not numbers (0)

hAxis: {
  format: 'MMM yy',  // <-- add
  //minValue: 0,  // <-- remove
  viewWindowMode: "explicit",
  //viewWindow: { min: 0 }, // <-- remove

use new Date(date) for first value in data row, here...

rawData.push([ new Date(date), total, {v: catscount, f: catspercentageAnnotation}, {v: dogscount, f: dogsspercentageAnnotation}]);

finally, when using date on the x-axis, with a month format,
the x-axis labels will repeat.
will need to build custom ticks

here, we use getColumnRange to determine how many ticks to add

  // build x-axis ticks to prevent repeated labels
  var dateRange = chartData.getColumnRange(0);
  var ticks = [];
  var dateTick = dateRange.min;
  while (dateTick.getTime() <= dateRange.max.getTime()) {
    ticks.push(dateTick);
    dateTick = new Date(dateTick.getFullYear(), dateTick.getMonth() + 2, 0);
  }
  options.hAxis.ticks = ticks;

see following working snippet...

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'],

    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", date: "30-Dec-2019", total: "42", cats: "0", dogs: "55", catspercentage: "0", dogsspercentage: "131"}, "Jan 20": {monthLabel: "Jan 2020", date: "31-Jan-2020", total: "0", cats: "0", dogs: "0"}, "Feb 20": {monthLabel: "Feb", date: "29-Feb-2020", total: "0", cats: "0", dogs: "0"}, "Mar 20": {monthLabel: "Mar", date: "31-Mar-2020", total: "0", cats: "0", dogs: "0"}};
  $.each(chart_object, function(i, chartobject) {
    date = chartobject.date;
    total = parseInt(chartobject.total);
    catscount = parseInt(chartobject.cats);
    dogscount = parseInt(chartobject.dogs);
    catspercentage = parseInt(chartobject.catspercentage);
    catspercentageAnnotation = catscount+",  percent "+catspercentage+"%";
    dogsspercentage = parseInt(chartobject.dogsspercentage);
    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 dateRange = chartData.getColumnRange(0);
      var ticks = [];
      var dateTick = dateRange.min;
      while (dateTick.getTime() <= dateRange.max.getTime()) {
        ticks.push(dateTick);
        dateTick = new Date(dateTick.getFullYear(), dateTick.getMonth() + 2, 0);
      }
      options.hAxis.ticks = ticks;

      chart.draw(chartData, options);
      counter++;
      window.setTimeout(drawChart, 1000);
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>