Пример #1
0
        /// <summary>
        /// Returns an object that is equivalent to the given parameter but without any null member AND it preserves DotNetInstanceClickHandler/DotNetInstanceHoverHandler members intact
        ///
        /// <para>Preserving DotNetInstanceClick/HoverHandler members is important because they contain DotNetObjectRefs to the instance whose method should be invoked on click/hover</para>
        ///
        /// <para>This whole method is hacky af but necessary. Stripping null members is only needed because the default config for the Line charts on the Blazor side is somehow messed up. If this were not the case no null member stripping were necessary and hence, the recovery of the DotNetObjectRef members would also not be needed. Nevertheless, The Show must go on!</para>
        /// </summary>
        /// <param name="chartConfig"></param>
        /// <returns></returns>
        private static object StripNulls(ChartConfigBase chartConfig)
        {
            // Serializing with the custom serializer settings remove null members
            var cleanChartConfigStr = JsonConvert.SerializeObject(chartConfig, JsonSerializerSettings);

            // Get back an ExpandoObject dynamic with the clean config - having an ExpandoObject allows us to add/replace members regardless of type
            dynamic clearConfigExpando = JsonConvert.DeserializeObject(cleanChartConfigStr,
                                                                       typeof(ExpandoObject),
                                                                       new ExpandoObjectConverter());

            // Restore any .net refs that need to be passed intact
            var dynamicChartConfig = (dynamic)chartConfig;

            if (dynamicChartConfig?.Options?.Legend?.OnClick != null &&
                dynamicChartConfig?.Options?.Legend?.OnClick is DotNetInstanceClickHandler)
            {
                clearConfigExpando.options                = clearConfigExpando.options ?? new { };
                clearConfigExpando.options.legend         = clearConfigExpando.options.legend ?? new { };
                clearConfigExpando.options.legend.onClick = dynamicChartConfig.Options.Legend.OnClick;
            }

            if (dynamicChartConfig?.Options?.Legend?.OnHover != null &&
                dynamicChartConfig?.Options?.Legend?.OnHover is DotNetInstanceHoverHandler)
            {
                clearConfigExpando.options                = clearConfigExpando.options ?? new { };
                clearConfigExpando.options.legend         = clearConfigExpando.options.legend ?? new { };
                clearConfigExpando.options.legend.onHover = dynamicChartConfig.Options.Legend.OnHover;
            }

            return(clearConfigExpando);
        }
Пример #2
0
        public static Task <bool> UpdateChart(this IJSRuntime jsRuntime, ChartConfigBase chartConfig)
        {
            try
            {
                return(jsRuntime.InvokeAsync <bool>("ChartJSInterop.UpdateChart", StripNulls(chartConfig)));
            }
            catch (Exception exp)
            {
            }

            return(Task.FromResult <bool>(false));
        }
Пример #3
0
        public static Task <bool> UpdateChart(this IJSRuntime jsRuntime, ChartConfigBase chartConfig)
        {
            try
            {
                var jsonConfig = JsonConvert.SerializeObject(StripNulls(chartConfig));
                return(jsRuntime.InvokeAsync <bool>("ChartJSInterop.UpdateChart", jsonConfig));
            }
            catch (Exception exp)
            {
            }

            return(Task.FromResult <bool>(false));
        }
        public static Task <bool> UpdateChart(this IJSRuntime jsRuntime, ChartConfigBase chartConfig)
        {
            try
            {
                dynamic dynParam = StripNulls(chartConfig);
                Dictionary <string, object> param = ConvertDynamicToDictonary(dynParam);
                return(jsRuntime.InvokeAsync <bool>("ChartJSInterop.UpdateChart", param));
            }
            catch (Exception exp)
            {
                Console.WriteLine($"ChartJSInterop.UpdateChart Exception:{exp}");
            }

            return(Task.FromResult <bool>(false));
        }
Пример #5
0
        public static ValueTask <bool> UpdateChart(this IJSRuntime jsRuntime, ChartConfigBase chartConfig)
        {
            try
            {
                dynamic dynParam = StripNulls(chartConfig);
                Dictionary <string, object> param = ConvertExpandoObjectToDictionary(dynParam);
                return(jsRuntime.InvokeAsync <bool>("ChartJSInterop.UpdateChart", param));
            }
            catch (Exception exp)
            {
                Console.WriteLine($"Error while updating chart: {exp.Message}");
            }

            return(new ValueTask <bool>(false));
        }
Пример #6
0
        private static string SerializeConfig(ChartConfigBase config)
        {
            var json = JsonConvert.SerializeObject(config, JsonSettings);

            return(json);
        }
Пример #7
0
 public static Task <bool> ReloadChart(this IJSRuntime jsRuntime, ChartConfigBase chartConfig)
 {
     return(jsRuntime.InvokeAsync <bool>("ChartJSInterop.ReloadChart", SerializeConfig(chartConfig)));
 }