mariusmuntean / ChartJs.Blazor

Brings Chart.js charts to Blazor
https://www.iheartblazor.com/
MIT License
692 stars 152 forks source link

Customising ChartTypes class #25

Closed oleksiizapara closed 4 years ago

oleksiizapara commented 5 years ago

I found a great extension for chartJS which allows to create candlestick chart. https://github.com/chartjs/chartjs-chart-financial

I tired to implement Blazor extension version based on ChartJs.Blazor, However, I found that I cannot add additional ChartTypes. I used reflection to create valid objects.

public static class ChartTypeFactory
    {
        public static ChartTypes CreateCandlestick() =>
         Construct<ChartTypes>(
             new Type[] { typeof(string) },
             new object[] { "candlestick" }
         );

        public static ChartTypes CreateOhlc() =>
        Construct<ChartTypes>(
            new Type[] { typeof(string) },
            new object[] { "ohlc" }
        );

        public static T Construct<T>(Type[] paramTypes, object[] paramValues)
        {
            Type t = typeof(T);

            ConstructorInfo ci = t.GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null, paramTypes, null);

            return (T)ci.Invoke(paramValues);
        }
    }

The sample project with candlestick chart can be access via link https://github.com/oleksiizapara/ChartJsBlazorFinancialSample

I hope that in future release the library will be able to do it straightforward.

May be it can be done via base class that can be inherited?

SeppPenner commented 5 years ago

If I was you, I would take a look at https://github.com/Joelius300/ChartJSBlazor. This project is maintained at least.

Joelius300 commented 4 years ago

We should add a ChartType.Custom(string chartType) method to the ChartType-'enum' with a summary along the lines of

This method constructs a <see cref="ChartType" /> which represents the given value. Only use this method if you're implementing your own chart e.g. for a chart.js extension, otherwise use the given values.

I'm adding this to the backlog so it can be added in a feature release.