Decrease the number of bars on the Barchart Chart js chart

Asked

Viewed 410 times

0

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

Ec: currently our month is 11 (Nov), so I would like to present only the first 10 columns of the chart.

inserir a descrição da imagem aqui

2 answers

1

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

You get the data, the first thing you should do is, manipulate it so it’s in the form you want it to be on the chart. Example:

  • If you have data that represent a 24 hour period, but just want 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 to the graph, thus showing only what you want.
  • Following the reasoning of your question, if you want to present only the first 10 months, based on the current month (11), you should then filter the original object/array and take only the data of the first 10 months and then move to the graph. Because it doesn’t make sense to make these filters on the chart itself, unless you want interaction with the user, making it able to see month 11 by clicking on some button, for example.

In conclusion, first assemble your entire strategy, since how will be the graph, the type of it, how it will be visualized and later you will take the data, filter them and then move to the graph.

I hope I’ve helped.

0

I did something similar on highcharts. just put a function that will act as data handler before they are delivered to 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 will go through your array q assuming it is 1 for each month it goes from 0 to 11. so the condition k < mes fits perfectly.

I hope I helped a little

Browser other questions tagged

You are not signed in. Login or sign up in order to post.