Skip to content
Advertisement

Can Chart.js Horizontal bar work with time series data?

I am trying to develop a stacked bar graph based on time series data. The data is representing the running of a machine logged to Influx.

Looking about I have not seen this done and cannot seem to get my data to line up correctly to make the stacked bar chart. Essentially I want to show one color for running and one for not running. This isn’t an issue as this can be changed programmatically.

However, the issue I seem to be facing is possible more of a configuration issue. I would expect the time series adaptor to automatically align my data up with how it would correspond in my dataset.

See my example so far. To clarify, I am using the latest Chart.js at time of writing 3.7

JavaScript
JavaScript

Any point in the correct direction would be most beneficial. I will also answer any queries to clarify my though process on the config also.

Advertisement

Answer

The problem is that Date represents milliseconds since 1 January 1970 UTC. Therefore you need to define the start date to see only the period of interest. This can be done by adding the min option to the x-axis as follows:

JavaScript

You should also use moment().startOf('day') when defining your data. That way, the ticks on the x-axis will be correctly positioned.

Please take a look at your amended code below and see how it works.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement