mattosaurus / ChartJSCore

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

Plugin Dynamic Converts everything to a string #80

Closed tbasallo closed 2 years ago

tbasallo commented 2 years ago

I think I understand how this is supposed to work reading through previous issues. However, I always get a string for the function. Is there something missing? Or a verion bug?

v3 with chartJS v3.7.1

I am trying to get a custom tooltip using the sample code from chartjs:

https://www.chartjs.org/docs/latest/samples/tooltip/html.html

I've tried both, since I am not sure which one it is:

    HistoricalChart.Options.PluginDynamic.Add("plugins", new p_1 { tooltip = new p_2 { callbacks = new p_3() } });
    HistoricalChart.PluginDynamic.Add("plugins", new p_1 { tooltip = new p_2 { callbacks = new p_3() } });

    public class p_1
    {
        [JsonConverter(typeof(PlainJsonStringConverter))]
        public p_2 tooltip { get; set; }
    }
    public class p_2
    {
        [JsonConverter(typeof(PlainJsonStringConverter))]
        public p_3 callbacks { get; set; }
    }
    public class p_3
    {
        [JsonConverter(typeof(PlainJsonStringConverter))]
        public string footer { get; set; } = "function(){ Console.log('give me something')}";
    }

And I end up with:

{
  "options": {
    "plugins": {
      "tooltip": {
        "callbacks": {
          "footer": "function(){ Console.log('dsdsd')}"
        }
      }
    }
  },
  "plugins": {
    "tooltip": {
      "callbacks": {
        "footer": "function(){ Console.log('dsdsd')}"
      }
    }
  }
}
tbasallo commented 2 years ago

I was going through the code to fork and maybe fix/figure out and I noticed your usings: Newton. My attribute was referencing System.Text.

A quick change to this, got it work:

[Newtonsoft.Json.JsonConverter(typeof(PlainJsonStringConverter))]
public string footer { get; set; } = "function(){ console.log('please be kind to me')}";