mattosaurus / ChartJSCore

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

PluginDynamic functions rendered as strings instead of plain Json #60

Closed a335967 closed 3 years ago

a335967 commented 4 years ago

This may be similar to #59. Is there a way to specify that certain elements are to be rendered as plain Json instead of strings within the PluginDynamic object?

Specifically, I'm using the chartjs-plugin-datalabels plugin for labels and need to use a function in the formatter:

"datalabels": {
        "color": "#000",
        "backgroundColor": "#4ecbb9",
        "align": "top",
        "offset": 10,
        "borderRadius": 10,
        "formatter": function abbreviateNumber(number) {
          var SI_POSTFIXES = ["", "k", "M", "G", "T", "P", "E"];
          var DOLLAR_SIGN = "$";
          var tier = Math.log10(Math.abs(number)) / 3 | 0;
          if (tier == 0) return number;
          var postfix = SI_POSTFIXES[tier];
          var scale = Math.pow(10, tier * 3);
          var scaled = number / scale;
          var formatted = scaled.toFixed(1) + '';
          if (/\.0$/.test(formatted))
            formatted = formatted.substr(0, formatted.length - 2);
          return DOLLAR_SIGN + formatted + postfix;
        }
      }

Any help you could provide is greatly appreciated!

mattosaurus commented 4 years ago

Hi, this isn't possible using the PluginDynamic attribute, this essentially just writes out the key/value pairs as properties.

You'd probably be better off creating you your own implementation of the Options class and using that rather than the provided one. Something like.

public class DataLabelsOptions : Options
{
    public DataLabels DataLabels { get; set; }
}

public class DataLabels
{
    public string Color { get; set; }
    // Other datalabel properties here...
}