Skip to content
Advertisement

ChartJS Separate Labels for each dataset/independent datasets?

I’m essentially attempting to create a bar chart with 2-8 items where the label on the bottom/legend is the short product code(ex: 4380) and mousing over the bar shows the full SKU/product name.

I have gotten it mostly working but my implementation goes one of two undesirable ways.

  1. The data points all combine into the first product number/chart label.

All datasets combine

  1. The blank spots make the bars tiny/not fill up the full width.

Too small of bars

My code for rendering the chart is as follows:

JavaScript

The ajax response for the two versions is as follows:

Version 1:

JavaScript

Version 2:

JavaScript

The only difference is either I always use the 0 indexes for datasets[index].data or I fill in null depending on where it should be.

Should I be changing the way the chart is rendered or should I change the way the data is passed in?

For the record, the mouseover shows the proper sku/full name.

Advertisement

Answer

I would define the data in a single dataset and keep the full product names in a separate property.

JavaScript

To get the product names displayed in the tooltip, you would have to define a label callback function as follows:

JavaScript

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

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