0

I have a partial, which could be re-used many times in the same view. It hosts a chartjs element.
The Partial loads, makes an ajax call to get its data.
The various keys of the Chart are populated from the returned data and executes and chart loads.
The partial's canvas and containing div are uniquely identified by a numeric counter of partials on the parent page which iterates using Razor to build the divs and canvases for each chart.
All good.
e.g.

@{
    divId = "divDetail" + ViewBag.ChartIndex.ToString();
    canvasId = "canvas" + ViewBag.ChartIndex.ToString();
}
<div id="@divId">
    <canvas id="@canvasId" style="background-color:black;">
        @Html.Partial("ChartPartial", Model)
    </canvas>
</div>

But, the chart has an 'onClick' key, which I populate with ChartClick0, ChartClick1 etc. in the Data controller action called by ajax.

How do I dynamically create a script called ChartClick0 or ChartClick1 such that the correct function per Chart is called and executed?

I tried New Function and variations on ViewBag in the parent screen, but no luck. With New Function, I can't concatenate the 'canvas' and the index to produce the new function.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Neal Rogers
  • 498
  • 9
  • 27
  • https://stackoverflow.com/questions/26257268/click-events-on-pie-charts-in-chart-js – Noman Nov 05 '20 at 13:19
  • What does ChartClick0, ChartClick1, etc. do? Seems like it would be better to attach the events in JavaScript instead of through HTML/Razor, using `addEventListener`... – Heretic Monkey Nov 05 '20 at 13:21
  • ChartClick0, ChartClick1 would be the names of the functions given to the chart's 'onClick' key. As, each chart loaded would be named numerically and incremented as chart0, chart1 – Neal Rogers Nov 06 '20 at 06:57
  • Re: addEventListener, that won't work as the built in onClick event-key in the chart needs to return x and y label data from the quadrants or bars in the chart. That will only give me a click event on the whole chart and not the bar or quadrant clicked on. – Neal Rogers Nov 06 '20 at 07:07

0 Answers0