Decrease the number of bars in the chart BarChart chart js

0

How can I decrease the number of bars to display according to the month we are?

Ec: Currently our month is 11 (Nov), so I would like to see only the top 10 columns of the chart.

    
asked by anonymous 11.11.2016 / 10:51

2 answers

1

Before assembling the graph, you have to think about how and what it will show, since a graph is based on values, that is, it is a visual representation of values passed by the user, data coming from request HTTP , or even a JSON file.

By getting the data, the first thing you should do is to manipulate it so that it is in the form you want to display on the chart. Example:

  • If you have data that represents a period of 24 hours, but only wants to show from 13:00 to 17:00. You must filter this data from the original object, generate a new object with the data you want and then move on to the graph, thus showing only what you want.
  • Following the reasoning of your question, if you want to display only the first 10 months, based on the current month (11), you should then filter the original object / array and pick up only the data from the first 10 months and then go through for the graph. Because it does not make sense to make these filters in the graph itself, unless you want to interact with the user, causing him to see month 11 by clicking on some button, for example.

In conclusion, first mount all your strategy, from what the chart will look like, how it will look, how it will be viewed, and then you will pick up the data, filter it, and then move on to the chart.

I hope I have helped.

    
11.11.2016 / 11:54
0

I did something similar in highcharts. just put a function that will act as a data manipulator before being delivered to the charts.

Then in this function you check the month and use the rule you want.

something like

var data = function (array) {

var ret = [];

var mes = new Date().getMonth();  // janeiro é 0

for(var k in array){

   if(k < mes){

        ret.push(array[k]);

    }

}

return ret;

} (array);

This way you make a filter, and since January is 0, November is 10, and the variable k goes through its array q assuming it is 1 for each month it goes from 0 to 11. Then the condition k < mes perfectly.

I hope to have helped a little

    
11.11.2016 / 11:58