Show only seven days of data on google chart

2019-02-27 10:19发布

I have a google chart i want to show only seven days data to users on the graph, And they should be able to see the previous seven day data.

For example if i have data of 1/7/2017 to 15/7/2017

Intialy grpah should show only 8/7/2017 to 15/7/2017. But if users want they can go back to previous 1/7/2017 to 8/7/2017 results.

Now i am showing all 14 days data which doesn't seem good

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
  </script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
 <script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart','line']});

google.charts.setOnLoadCallback(seven_day_compare);

// Draw the chart and set the chart values

function seven_day_compare() {
  var data = google.visualization.arrayToDataTable(
 [["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]    
);

  // Optional; add a title and set the width and height of the chart
  var options = {'title':'Compare like and share', 'width':550, 'height':400,'pointSize': 10,'hAxis':{
        'slantedText': true, 
          'slantedTextAngle':30},'vAxis':{}};

  // Display the chart inside the <div> element with id="differnt"

  var chart = new google.visualization.LineChart(document.getElementById('chart'));
  chart.draw(data, options);
} 
</script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id = "chart"></div>
  </body>
</html>

So basically i want to break the graph into seven days time periods Is it possible with google charts. Or any other suggestions

1条回答
贪生不怕死
2楼-- · 2019-02-27 10:47

this is possible using a DataView

first, need to convert the first column to an actual date,
this can be done with a calculated column...

// convert first column to date
view.setColumns([{
  calc: function (dt, row) {
    return new Date(dt.getValue(row, 0));
  },
  label: data.getColumnLabel(0),
  type: 'date'
}, 1, 2]);

then use the getFilteredRows method to limit the date range shown on the chart...

// filter date range
view.setRows(view.getFilteredRows([{
  column: 0,
  minValue: new Date(2017, 6, 8),
  maxValue: new Date(2017, 6, 15)
}]));

see following working snippet...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]);

    var view = new google.visualization.DataView(data);

    // convert first column to date
    view.setColumns([{
      calc: function (dt, row) {
        return new Date(dt.getValue(row, 0));
      },
      label: data.getColumnLabel(0),
      type: 'date'
    }, 1, 2]);

    // filter date range
    view.setRows(view.getFilteredRows([{
      column: 0,
      minValue: new Date(2017, 6, 8),
      maxValue: new Date(2017, 6, 15)
    }]));

    var chart = new google.visualization.LineChart($('#chart').get(0));
    chart.draw(view);
  },
  packages:['corechart']
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>


EDIT

use option --> hAxis.ticks -- to ensure which dates appear on the x-axis

to build ticks dynamically, use data table / view method --> getColumnRange,
which returns an object with properties for min & max

see following working snippet...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]);

    var view = new google.visualization.DataView(data);

    // convert first column to date
    view.setColumns([{
      calc: function (dt, row) {
        return new Date(dt.getValue(row, 0));
      },
      label: data.getColumnLabel(0),
      type: 'date'
    }, 1, 2]);

    // filter date range
    view.setRows(view.getFilteredRows([{
      column: 0,
      minValue: new Date(2017, 6, 8),
      maxValue: new Date(2017, 6, 15)
    }]));

    // build tick for each day
    var oneDay = (1000 * 60 * 60 * 24);
    var dateRange = view.getColumnRange(0);
    var ticksAxisH = [];
    for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) {
      ticksAxisH.push(new Date(i));
    }
    // optional, add final tick at far right
    if (ticksAxisH.length > 0) {
      ticksAxisH.push(new Date(ticksAxisH[ticksAxisH.length - 1].getTime() + oneDay));
    }

    var chart = new google.visualization.LineChart($('#chart').get(0));
    chart.draw(view, {
      hAxis: {
        ticks: ticksAxisH
      }
    });
  },
  packages:['corechart']
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

查看更多
登录 后发表回答