diff --git a/ChartJs.Blazor.Samples/Client/Pages/Charts/Bar/Vertical.razor b/ChartJs.Blazor.Samples/Client/Pages/Charts/Bar/Vertical.razor index 418bdea..98618c1 100644 --- a/ChartJs.Blazor.Samples/Client/Pages/Charts/Bar/Vertical.razor +++ b/ChartJs.Blazor.Samples/Client/Pages/Charts/Bar/Vertical.razor @@ -6,7 +6,8 @@ - + + @@ -79,7 +80,7 @@ _chart.Update(); } - private void AddDataset() + private void AddDataset(bool? hidden = null) { Color color = ChartColors.All[_config.Data.Datasets.Count % ChartColors.All.Count]; IDataset dataset = new BarDataset(RandomScalingFactor(_config.Data.Labels.Count)) @@ -87,13 +88,16 @@ Label = $"Dataset {_config.Data.Datasets.Count}", BackgroundColor = ColorUtil.FromDrawingColor(Color.FromArgb(128, color)), BorderColor = ColorUtil.FromDrawingColor(color), - BorderWidth = 1 + BorderWidth = 1, + Hidden = hidden }; + _config.Data.Datasets.Add(dataset); _chart.Update(); } + private void RemoveDataset() { IList datasets = _config.Data.Datasets; diff --git a/ChartJs.Blazor.Samples/Client/Pages/Workarounds/Gradient.razor b/ChartJs.Blazor.Samples/Client/Pages/Workarounds/Gradient.razor index 30b9cdb..ce5f676 100644 --- a/ChartJs.Blazor.Samples/Client/Pages/Workarounds/Gradient.razor +++ b/ChartJs.Blazor.Samples/Client/Pages/Workarounds/Gradient.razor @@ -3,12 +3,13 @@ @layout SampleLayout @inject IJSRuntime jsRuntime - + @code { private const int InitalCount = 7; private LineConfig _config; private Random _rng = new Random(); + private Chart _chart; protected override void OnInitialized() { @@ -61,5 +62,5 @@ } private async Task SetupCompletedCallback() => - await jsRuntime.InvokeVoidAsync("workaroundGradient", _config.CanvasId); + await jsRuntime.InvokeVoidAsync("workaroundGradient", _chart.ChartId); } diff --git a/src/ChartJs.Blazor/Chart.razor b/src/ChartJs.Blazor/Chart.razor index ac20d2f..525a5fa 100644 --- a/src/ChartJs.Blazor/Chart.razor +++ b/src/ChartJs.Blazor/Chart.razor @@ -1 +1 @@ - + diff --git a/src/ChartJs.Blazor/Chart.razor.cs b/src/ChartJs.Blazor/Chart.razor.cs index d499190..4fc032d 100644 --- a/src/ChartJs.Blazor/Chart.razor.cs +++ b/src/ChartJs.Blazor/Chart.razor.cs @@ -12,6 +12,8 @@ namespace ChartJs.Blazor /// public partial class Chart { + private ConfigBase _config; + /// /// This event is fired when the chart has been setup through interop and /// the JavaScript chart object is available. Use this callback if you need to setup @@ -30,7 +32,17 @@ public partial class Chart /// Gets or sets the configuration of the chart. /// [Parameter] - public ConfigBase Config { get; set; } + public ConfigBase Config + { + get => _config; + set + { + // Need to synchronize the Config.CanvasId with Chart.ChartId + if (_config != null) _config.CanvasId = null; + _config = value; + if (_config != null) _config.CanvasId = ChartId; + } + } /// /// Gets or sets the width of the canvas HTML element. @@ -44,6 +56,12 @@ public partial class Chart [Parameter] public int? Height { get; set; } + /// + /// Gets the id for the html canvas element associated with this chart. + /// This property is initialized to a random GUID-string upon creation. + /// + public string ChartId { get; } = Guid.NewGuid().ToString(); + /// protected override async Task OnAfterRenderAsync(bool firstRender) { diff --git a/src/ChartJs.Blazor/Common/ConfigBase.cs b/src/ChartJs.Blazor/Common/ConfigBase.cs index 7e3725a..dff2ec0 100644 --- a/src/ChartJs.Blazor/Common/ConfigBase.cs +++ b/src/ChartJs.Blazor/Common/ConfigBase.cs @@ -20,15 +20,25 @@ protected ConfigBase(ChartType chartType) } /// - /// Gets the type of chart this config is for. + /// Gets or sets the id for the html canvas element associated with this chart. + /// This property must be set to the ChartId of the chart. /// - public ChartType Type { get; } + public string CanvasId { get; internal set; } /// - /// Gets the id for the html canvas element associated with this chart. - /// This property is initialized to a random GUID-string upon creation. + /// Gets a value indicating whether debug messages should be written to the console + /// in the Javascript code. /// - public string CanvasId { get; } = Guid.NewGuid().ToString(); +#if DEBUG + public bool Debug => System.Diagnostics.Debugger.IsAttached; +#else + public bool Debug => false; +#endif + + /// + /// Gets the type of chart this config is for. + /// + public ChartType Type { get; } /// /// Gets the list of inline plugins for this chart. Consider diff --git a/src/ChartJs.Blazor/Common/Dataset.cs b/src/ChartJs.Blazor/Common/Dataset.cs index b119a38..2864a0b 100644 --- a/src/ChartJs.Blazor/Common/Dataset.cs +++ b/src/ChartJs.Blazor/Common/Dataset.cs @@ -45,6 +45,12 @@ protected Dataset(ChartType type, string id = null) : base(new List()) Type = type; } + /// + /// Sets the dataset to be hidden when rendered. The label will still show in the Legend so + /// that the user can click on it and show the data. + /// + public bool? Hidden { get; set; } + /// /// Adds the elements of the specified collection to the end of the . /// diff --git a/src/ChartJs.Blazor/Interop/TypeScript/ChartJsInterop.ts b/src/ChartJs.Blazor/Interop/TypeScript/ChartJsInterop.ts index 54324b2..0869829 100644 --- a/src/ChartJs.Blazor/Interop/TypeScript/ChartJsInterop.ts +++ b/src/ChartJs.Blazor/Interop/TypeScript/ChartJsInterop.ts @@ -1,6 +1,7 @@ /// interface ChartConfiguration extends Chart.ChartConfiguration { + debug: boolean; canvasId: string; } @@ -26,6 +27,9 @@ class ChartJsInterop { BlazorCharts = new Map(); public setupChart(config: ChartConfiguration): boolean { + if(config.debug) + console.debug("ChartJS.Blazor.ChartJSInterop", config); + if (!this.BlazorCharts.has(config.canvasId)) { this.wireUpCallbacks(config);