sgratzl/chartjs-plugin-hierarchical

beforeDataSetsDraw() crashes if no scaleLabel option is defined

Sutherlandon opened this issue · 2 comments

When I use the "hierarchical" scale type, the hierarchicalPlugin crashes with the following error:

Uncaught TypeError: Cannot read property 'color' of undefined
    at Object.beforeDatasetsDraw (index.esm.js:551)
    at callback (helpers.segment.js:129)
    at PluginService._notify (chart.esm.js:6398)
    at PluginService.notify (chart.esm.js:6373)
    at Chart.notifyPlugins (chart.esm.js:7889)
    at Chart._drawDatasets (chart.esm.js:7555)
    at Chart.draw (chart.esm.js:7519)
    at chart.esm.js:100
    at Map.forEach (<anonymous>)
    at Animator._update (chart.esm.js:73)
    at chart.esm.js:57

To Reproduce

Given a valid ctx = <canvas />, use the following code:

const chartInstance = new ChartJS(ctx, {
  // data,
  options: {
    scales: {
      x: {
        type: HierarchicalScale.id
      }
    }
  },
  type: 'bar',
});

Notice a data object is not required to reproduce the bug

Expected behavior

I expect the scale not to crash but rely on built in defaults for required items.

Context

  • Version: 3.0.0-beta.9
  • Browser: Chrome Version 89.0.4389.90

Additional context
The problem line is here:

// index.esm.js
...
550     var scaleLabel = scale.options.scaleLabel;  // <-- this returns 'undefined'
551     var scaleLabelFontColor = valueOrDefault(
                 scaleLabel.color, // <-- This expression causes the error
                 defaults.color
          );

Specifying the scaleLabel as an option (which is not documented in Chart.js 3 or this repo) prevents the crash from happening

 options: {
    scales: {
      x: {
        type: HierarchicalScale.id,
        scaleLabel: {}
      }
    }
  },

can you try out the latest rc version

I have tried with v3.0.0-rc.3 and have not encountered the same error. The issue appears to be resolved. Thank you! 💯