Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Chart does not render unless options.labels contains at least one element

Open newclique opened this issue 9 years ago • 3 comments

Expected Behavior

A chart should render whether labels have been assigned or not. At the very least, having to explicitly add a labels array with at least one empty element should not be required in order to get a chart to render.

Current Behavior

Currently, a stacked bar chart (and maybe others) will not render if options.labels is omitted or if options.labels is set to an empty array. If labels is even given a single, empty element, all datasets then render. Note that this seems to be a regressive bug. The same chart definition renders in 2.1.x.

Possible Solution

Do not make rendering dependent upon labels.

Steps to Reproduce (for bugs)

See example here. http://plnkr.co/edit/0aUi8XZDf27EYKDDEZ4f?p=preview

  1. Observe there is no chart.
  2. Add a single, empty element to options.labels.
  3. Observer there is now a chart.

Context

I was seeing a chart render in 2.1.x in an online site (https://codepen.io/jamiecalder/pen/NrROeB). I tried using a similar definition on my 2.6 Chart.JS application and was not seeing any results. After 30 minutes or so of piddling around I tried adding labels and after adding only a single label, all datasets rendered in the 2.6 chart.

Environment

  • Chart.js version: 2.6.0
  • Browser name and version: Chrome 60
  • Link to your project: http://plnkr.co/edit/0aUi8XZDf27EYKDDEZ4f?p=preview

newclique avatar Aug 23 '17 15:08 newclique

This problem is caused by the function determineDataLimits in the file scale.category.js. It only checks the labels to determine the minIndex and maxIndex. A solution could be to add the following code before the other checks:

helpers.each(chart.data.datasets, function(dataset, datasetIndex) {
    // Prevent errors when only Scale Constructor is called,
    // Would otherwise cause problems in testing.
    var isVisible = typeof chart.isDatasetVisible === 'function'
        ? chart.isDatasetVisible(datasetIndex)
        : true;
    if (isVisible) {
        if (dataset.data.length - 1 > me.maxIndex) {
            me.maxIndex = dataset.data.length - 1;
        }
    }
});

This would work, however a fixed max option would still only check the labels. It would only display the data, however the missing label tick lines will not be drawn. Are there any documented behavioral specs for the scales?

jcopperfield avatar Nov 05 '17 07:11 jcopperfield

This seems to have been fixed in 2.7.0 already: http://plnkr.co/edit/3Bue4TgScDgydS657obJ?p=preview

kurkle avatar Feb 12 '19 12:02 kurkle

Great find @kurkle!

etimberg avatar Feb 12 '19 23:02 etimberg