Skip to content

Setting resizeDelay to non-zero value gives "Cannot read properties of undefined (reading 'left')" on clipArea error #909

Open
@edwardmjackson

Description

@edwardmjackson

Using [email protected] and [email protected], an error is generated "Cannot read properties of undefined (reading 'left') on clipArea" when the resizeDelay option is set to non-zero.

JSFiddle: here (or just copy and paste this into a new fiddle)

<div>
  <canvas id="myChart"></canvas>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<script>
  const ctx = document.getElementById('myChart');

  new Chart(ctx, {
    type: 'bar',
    data: {
      labels: [1, 2, 3, 4, 5, 6],
      
      datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true
        }
      },      
      resizeDelay: 100,
      annotation: {
        annotations: {
          box1: {
            // Indicates the type of annotation
            type: 'box',
            xMin: 1,
            xMax: 2,
            yMin: 8,
            yMax: 10,
            backgroundColor: 'rgba(255, 99, 132, 0.25)'
          }
        }
      }
    }
  });
</script>

If you comment and uncomment line 28, resizeDelay: 100, the JSFiddle shows "script error" appear when resizeDelay is set.

The error appears to be in the Draw function

function draw(chart, caller, clip) {
const {ctx, chartArea} = chart;
const state = chartStates.get(chart);
if (clip) {
clipArea(ctx, chartArea);
}
const drawableElements = getDrawableElements(state.visibleElements, caller).sort((a, b) => a.element.options.z - b.element.options.z);
for (const item of drawableElements) {
drawElement(ctx, chartArea, state, item);
}
if (clip) {
unclipArea(ctx);
}
}

Where clipArea(ctx, chartArea) is called but chartArea is not yet defined, hence the chart.js helper at
https://github.com/chartjs/Chart.js/blob/master/src/helpers/helpers.canvas.ts#L327
fails.

My guess at a possible solution could be to only draw if ctx and chartArea are defined?

alternatively, it may be one for the maintainers of chartJS to fix for all users of plugin helprs. It only seems to affect chartjs-plugin-annotation however, not other plugins we're currently using such as chartjs-plugin-zoom

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions