Skip to content

Commit 063f4da

Browse files
author
Mischa Spelt
committed
Allow arbitrary chart plugins
1 parent 0267598 commit 063f4da

File tree

1 file changed

+68
-37
lines changed

1 file changed

+68
-37
lines changed

blazorbootstrap/Models/Charts/ChartPlugins/ChartPlugins.cs

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,67 @@
1-
namespace BlazorBootstrap;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace BlazorBootstrap;
4+
5+
public abstract class ChartPlugin { }
26

37
public class ChartPlugins
48
{
9+
#region Fields
10+
11+
[JsonInclude]
12+
[JsonExtensionData]
13+
private Dictionary<string, object> customPlugins = new();
14+
15+
[JsonIgnore]
16+
private Dictionary<Type, ChartPlugin> customPluginsByType = new();
17+
18+
#endregion
19+
20+
#region Methods
21+
22+
public void Add<T>( string key, T plugin )
23+
where T : ChartPlugin
24+
{
25+
customPlugins.Add( key, plugin );
26+
customPluginsByType.Add( typeof( T ), plugin );
27+
}
28+
29+
public bool TryAdd<T>( string key, ChartPlugin plugin )
30+
where T : ChartPlugin
31+
=> customPlugins.TryAdd( key, plugin ) && customPluginsByType.TryAdd( typeof( T ), plugin );
32+
33+
public T Get<T>( string key ) where T : ChartPlugin
34+
=> (T)customPlugins[ key ];
35+
36+
public T? Get<T>() where T : ChartPlugin
37+
=> customPluginsByType.TryGetValue( typeof( T ), out var plugin ) ? (T)plugin : null;
38+
39+
public bool TryGet<T>( string key, [NotNullWhen( true )] out T? value ) where T : ChartPlugin
40+
{
41+
value = null;
42+
if( customPlugins.TryGetValue( key, out var plugin ) && ( plugin is T pluginAsT ) )
43+
{
44+
value = pluginAsT;
45+
return true;
46+
}
47+
48+
return false;
49+
}
50+
51+
public bool TryGet<T>( [NotNullWhen( true )] out T? value ) where T : ChartPlugin
52+
{
53+
value = null;
54+
if( customPluginsByType.TryGetValue( typeof( T ), out var plugin ) && ( plugin is T pluginAsT ) )
55+
{
56+
value = pluginAsT;
57+
return true;
58+
}
59+
60+
return false;
61+
}
62+
63+
#endregion
64+
565
#region Properties, Indexers
666

767
/// <summary>
@@ -23,13 +83,6 @@ public class ChartPlugins
2383
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
2484
public ChartPluginsTooltip? Tooltip { get; set; }
2585

26-
/// <summary>
27-
/// Tooltip for automatic color selection.
28-
/// <see href="https://www.chartjs.org/docs/latest/general/colors.html"/>
29-
/// </summary>
30-
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
31-
public ChartPluginsColors? Colors { get; set; }
32-
3386
/// <summary>
3487
/// Annotations are a way to draw over chart data, and can be used to draw guides, lines, or any other custom elements.
3588
/// </summary>
@@ -39,7 +92,7 @@ public class ChartPlugins
3992
#endregion
4093
}
4194

42-
public class ChartPluginsAnnotation
95+
public class ChartPluginsAnnotation : ChartPlugin
4396
{
4497
#region Inner classes
4598

@@ -93,7 +146,7 @@ public class ChartPluginsAnnotationsCommon
93146
#endregion
94147
}
95148

96-
public class ChartPluginsLegend
149+
public class ChartPluginsLegend : ChartPlugin
97150
{
98151
#region Properties, Indexers
99152

@@ -160,7 +213,7 @@ public class ChartPluginsLegend
160213
#endregion
161214
}
162215

163-
public class ChartPluginsLegendTitle
216+
public class ChartPluginsLegendTitle : ChartPlugin
164217
{
165218
/// <summary>
166219
/// Color of the legend. Default value is 'black'.
@@ -190,7 +243,7 @@ public class ChartPluginsLegendTitle
190243
/// The chart label settings
191244
/// <see href="https://www.chartjs.org/docs/latest/configuration/legend.html#legend-label-configuration" />
192245
/// </summary>
193-
public class ChartPluginsLegendLabels
246+
public class ChartPluginsLegendLabels : ChartPlugin
194247
{
195248
/// <summary>
196249
/// Width of coloured box.
@@ -253,7 +306,7 @@ public class ChartPluginsLegendLabels
253306
/// The chart title defines text to draw at the top of the chart.
254307
/// <see href="https://www.chartjs.org/docs/latest/configuration/title.html" />
255308
/// </summary>
256-
public class ChartPluginsTitle
309+
public class ChartPluginsTitle : ChartPlugin
257310
{
258311
#region Properties, Indexers
259312

@@ -291,7 +344,7 @@ public class ChartPluginsTitle
291344
/// Tooltip for bubble, doughnut, pie, polar area, and scatter charts
292345
/// <see href="https://www.chartjs.org/docs/latest/configuration/tooltip.html" />
293346
/// </summary>
294-
public class ChartPluginsTooltip
347+
public class ChartPluginsTooltip : ChartPlugin
295348
{
296349
#region Properties, Indexers
297350

@@ -398,7 +451,7 @@ public class ChartPluginsTooltip
398451
/// <summary>
399452
/// <see href="https://www.chartjs.org/docs/latest/general/fonts.html" />
400453
/// </summary>
401-
public class ChartPluginsTooltipFont
454+
public class ChartPluginsTooltipFont : ChartPlugin
402455
{
403456
#region Properties, Indexers
404457

@@ -436,25 +489,3 @@ public class ChartPluginsTooltipFont
436489

437490
#endregion
438491
}
439-
440-
/// <summary>
441-
/// <see href="https://www.chartjs.org/docs/latest/general/colors.html" />
442-
/// </summary>
443-
public class ChartPluginsColors
444-
{
445-
#region Properties, Indexers
446-
447-
/// <summary>
448-
/// Whether the colors plugin is enabled.
449-
/// </summary>
450-
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
451-
public bool? Enabled { get; set; }
452-
453-
/// <summary>
454-
/// Whether the colors plugin should be used to select series colors, regardless of whether they are specified in the dataset.
455-
/// </summary>
456-
[JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
457-
public bool? ForceOverride { get; set; }
458-
459-
#endregion
460-
}

0 commit comments

Comments
 (0)