0

I would like to draw a chart to my html, the chart I am using is google chart. However, while my x value is increasing, the chart is getting bigger. But I just want a fixed size window which increase both minimum x value and maximum x value. Like sliding window.

The attachment below is my code. This is the js code which updates the gets the value and updates the chart

// load google charts library
google.load("visualization", "1", {packages:["corechart"]});

// for rest, walk, fast_walk data
var data, options, chart;

var xMin = 0;
var xMax = 10;

var i = 0;


/* initialize chart1 - rest, walk, fast_walk data */
function drawChart(data, options) {
    var chart = new 
    google.visualization.LineChart(document.getElementById('data-container'));
    chart.draw(data, options);
    return(chart);
}

/* update the chart1 - rest, walk, fast_walk data */
function updateChart(percentage) {
    i = (i + 1);

    data.addRow([
        ""+i,
        percentage
    ]);

    if(xMax >= 9) {
      xMin + 1;
    }
    xMax + 1;

    chart.draw(data, options);
}

$(function() {

    data = google.visualization.arrayToDataTable([
        ['Time', 'percentage'],
        ['0', 0],
    ]);

    options = {
        title: 'Energy data',
        "curveType": "function",
        vAxis: {
          min: xMin,
          max: xMax
        }
    };

    chart = drawChart(data, options);
});


/* reset charts */
function reset(){
    i = 0;

    data = google.visualization.arrayToDataTable([
        ['Time', 'percentage'],
        ['0', 0],
    ]);

    options = {
        title: 'Energy data',
        "curveType": "function",
        hAxis: {
          viewWindow: {
              min: 0,
              max: 10
          }
        }
    };

    chart = drawChart(data, options);
}

I am wondering if it can be designed into a sliding window, so that the x value doesn't stick with the minimum value 0. instead if we want to see the earliest value, we can just scroll left.

Liu Hantao
  • 620
  • 1
  • 9
  • 19
  • google's answer for this is the [ChartRangeFilter control](https://developers.google.com/chart/interactive/docs/gallery/controls#chartrangefilter)... – WhiteHat Feb 06 '19 at 12:17

2 Answers2

0

According to the documentation at [https://developers.google.com/chart/interactive/docs/gallery/linechart]

/* copied from site*/
var options = {
    chart: {
      title: 'Box Office Earnings in First Two Weeks of Opening',
      subtitle: 'in millions of dollars (USD)'
    },
    width: 900, //<--- set fixed width like so
    height: 500
  };

To get a scrollable div,you can wrap your chart inside another div

<div class='h-scrollable' > <!---- chart code here ----> </div>

and for css

.h-scrollable {
    width: 100%;
    overflow: hidden;  // only if you don't want y axis to be scrollable
    overflow-x: auto;
}

Now, for the chart give a width depending on the number of x values you have. You could do a mathematical computation by taking the width of the h-scrollbale div in javascript and dividing it by number of x-points you want in a window and then multiplying it with total x values you have and setting it as chart width.

Update: inititally get the width of h-scrollable as let viewWidth = document.querySelector(".h-scrollable").offsetWidth [refer : How to find the width of a div using raw JavaScript?

Then if you want to show 10 x values in a view, divide viewWidth by 10 to get one xWidth. Now you can re-render the chart each time by setting width as no.of X values * xWidth so that it scrolls accordingly

Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
  • It doesn't change the minimum value as the x value grows, so it is not like the sliding window. I am wondering if there is a method which doesn't need to redraw the chart and able to accomplish that. – Liu Hantao Feb 06 '19 at 03:06
  • By sliding window do you mean to have a horizontal scroll ? – Dhananjai Pai Feb 06 '19 at 03:07
  • Yes. What I want is that, say if the maximum x values I allowed to display on the screen is 10, then the chart will only display data point from 0 to 9 and then from 1 to 10. – Liu Hantao Feb 06 '19 at 03:10
  • @LiuHantao see the updated answer and ask if you run into problems or have trouble understanding. Hope it helps – Dhananjai Pai Feb 06 '19 at 03:18
  • Thanks! I am now wondering how do I take the width from h-scrollable and reset the chart? I only created chart at the bigining, so do I need to recreate the chart or simply update the xMin and xMax? – Liu Hantao Feb 06 '19 at 04:52
  • you may have to recreate the chart each time since the width should also grow and hence the scales and plot will also have to be recomputed. – Dhananjai Pai Feb 06 '19 at 04:57
0

Upon each update, you can just remove the first raw.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 08 '22 at 14:44