chartjs-override.js throws TypeError on pie/doughnut charts due to missing scales guard
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3579222. -->
Reported by: [awearring](https://www.drupal.org/user/112933)
Related to !15
>>>
<p>When <code>localgov_elections</code> is enabled alongside any page that renders a pie or doughnut chart via <code>drupal/charts</code>, the following JavaScript error is thrown in the browser console:</p>
<pre>Uncaught TypeError: can't access property "x", chartData.options.scales is undefined<br> setChartColours chartjs-override.js:34<br> handleChartsConfigsInitialization chartjs-override.js:45</pre><p>This causes the pie/doughnut chart to render without any colour.</p>
<p><strong>Steps to reproduce:</strong></p>
<p> Enable <code>localgov_elections</code><br>
Navigate to any page that renders a pie or doughnut chart via <code>drupal/charts</code><br>
Observe the JavaScript error in the browser console and the unstyled chart</p>
<p><strong>Root cause:</strong></p>
<p>In <code>js/chartjs-override.js</code>, <code>setChartColours()</code> unconditionally accesses <code>chartData.options.scales.x</code>:<br>
<code>chartData.options.scales.x.ticks.precision = 0;</code></p>
<p>Pie and doughnut chart types do not have axes, so `chartData.options.scales` is `undefined` for these chart types, causing the TypeError.</p>
<p>The same issue exists in the <code>handleChartsConfigsInitialization</code> handler where <code>data.options.scales.y</code> is accessed without a guard.</p>
<p><strong>Suggested fix:</strong></p>
<p>Wrap both <code>scales</code> accesses in existence checks:</p>
<pre>if (chartData.options.scales && chartData.options.scales.x) {<br> chartData.options.scales.x.ticks.precision = 0;<br>}</pre><p>And: </p>
<pre>if (data.options.scales && data.options.scales.y) {<br> data.options.scales.y.grid = { display: false };<br> data.options.scales.y.ticks.autoSkip = false;<br>}</pre><p><strong>Environment:</strong></p>
<p> Drupal version: 10.6.5<br>
<code>drupal/charts</code> version: 10.6.5<br>
<code>localgov_elections</code> version: 2.1.0-beta1</p>
issue