mattosaurus / ChartJSCore

Implementation of Chart.js for use with .NET Core.
GNU General Public License v3.0
116 stars 34 forks source link

Many callback properties are being rendered as strings instead of as plain JSON #59

Closed willherr closed 4 years ago

willherr commented 4 years ago

I specifically ran into this issue with the Legend model and the OnClickproperty. I kept getting an error that "onClick" was not a function, and indeed it was not. It was being rendered as a string: "onClick": "function (e) { e.stopPropagation(); }"...

I took a look at how other callbacks are implemented and it appears that the callback properties should be decorated with [JsonConverter(typeof(PlainJsonStringConverter))] in order for them to be serialized correctly. Every callback in the Legend model is probably broken (I took a look at another model and indeed this attribute is missing there too).

A hotfix for my project was to simply extend the class and hide the damaged property:

using ChartJSCore.Helpers;
using ChartJSCore.Models;
using Newtonsoft.Json;

namespace MyNamespace 
{
    public class MyLegend : Legend
    {
        [JsonConverter(typeof(PlainJsonStringConverter))]
        public new string OnClick { get; set; }
    }
}

and applying that to my option in implementation


return new Chart 
{
    Options = new Options
    {
        Legend = new MyLegend
        {
            OnClick = "function (e) { e.stopPropagation(); }"
        }
    }
}
mattosaurus commented 4 years ago

Damm, must have missed these, thanks for pointing it out. I'll add the converter attribute and push aout a new build.

mattosaurus commented 4 years ago

I've added in the missing converter attributes and have published v1.5.8 to NuGet.