Skip to content

Commit 43cf2b4

Browse files
committed
Added custom enum converter to support enum values
1 parent 0ac9383 commit 43cf2b4

File tree

7 files changed

+81
-7
lines changed

7 files changed

+81
-7
lines changed

samples/ChartSamples/Pages/DateTimeChart.razor

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
<h3>DateTime Chart</h3>
44

5+
6+
57
<div class="row">
68
<div class="col-md-12 col-lg-6">
7-
<ApexChart TItem="Order" Title="Orders Value"
9+
<ApexChart TItem="Order" Title="Orders Value" Debug
810
OnDataPointSelection="DataPointSelected"
911
ChartType="ChartType.Bar"
1012
XAxisType="XaxisType.Datetime"
@@ -32,9 +34,12 @@
3234
@code {
3335
private ApexChartOptions<Order> options = new ApexCharts.ApexChartOptions<Order>();
3436
private SelectedData<Order> selectedData;
35-
3637
protected override void OnInitialized()
3738
{
39+
40+
//Stacked 100% test
41+
options.Chart = new Chart { Stacked = true, Type = ChartType.Bar, StackType = StackType.Percent100 };
42+
3843
options.Tooltip = new Tooltip { X = new TooltipX { Format = @"MMMM \ yyyy" } };
3944

4045
options.Subtitle = new Subtitle { OffsetY = 15, Text = "DateTime sample with options" };

samples/ChartSamples/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "Blazor-ApexCharts/series-options",
14+
"launchUrl": "Blazor-ApexCharts/datetime-chart",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
},

src/Blazor-ApexCharts/ApexChart.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ public async Task Render()
193193
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
194194
IgnoreNullValues = true,
195195
};
196-
serializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
196+
197197
serializerOptions.Converters.Add(new DataPointConverter<TItem>());
198-
198+
serializerOptions.Converters.Add(new CustomJsonStringEnumConverter());
199199
var jsonOptions = JsonSerializer.Serialize(Options, serializerOptions);
200200
await JSRuntime.InvokeVoidAsync("blazor_apexchart.renderChart", ObjectReference, ChartContainer, jsonOptions);
201201
await OnDataPointSelection.InvokeAsync(null);

src/Blazor-ApexCharts/ApexSeries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ApexSeries<TItem> : ComponentBase, IDisposable where TItem : class
2626
private readonly Series<TItem> series = new Series<TItem>();
2727
private IEnumerable<DataPoint<TItem>> currentDatalist;
2828

29-
protected override async Task OnParametersSetAsync()
29+
protected override void OnParametersSet()
3030
{
3131
series.Name = Name;
3232
series.ShowDataLabels = ShowDataLabels;

src/Blazor-ApexCharts/Models/ApexChartOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System;
3+
using System.Runtime.Serialization;
34

45
namespace ApexCharts
56
{
@@ -3137,7 +3138,10 @@ public class ApexThemeMonochrome
31373138

31383139
public enum Easing { Easein, Easeinout, Easeout, Linear };
31393140

3140-
public enum StackType { Normal, The100 };
3141+
public enum StackType {
3142+
Normal,
3143+
[EnumMember(Value = "100%")]
3144+
Percent100 };
31413145

31423146
public enum AutoSelected { Pan, Selection, Zoom };
31433147

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Runtime.Serialization;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
9+
namespace ApexCharts.Models
10+
{
11+
12+
//https://stackoverflow.com/questions/59059989/system-text-json-how-do-i-specify-a-custom-name-for-an-enum-value
13+
public class CustomJsonStringEnumConverter : JsonConverterFactory
14+
{
15+
private readonly JsonNamingPolicy namingPolicy;
16+
private readonly bool allowIntegerValues;
17+
private readonly JsonStringEnumConverter baseConverter;
18+
19+
public CustomJsonStringEnumConverter() : this(null, true) { }
20+
21+
public CustomJsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true)
22+
{
23+
this.namingPolicy = namingPolicy;
24+
this.allowIntegerValues = allowIntegerValues;
25+
this.baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues);
26+
}
27+
28+
public override bool CanConvert(Type typeToConvert) => baseConverter.CanConvert(typeToConvert);
29+
30+
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
31+
{
32+
var query = from field in typeToConvert.GetFields(BindingFlags.Public | BindingFlags.Static)
33+
let attr = field.GetCustomAttribute<EnumMemberAttribute>()
34+
where attr != null
35+
select (field.Name, attr.Value);
36+
var dictionary = query.ToDictionary(p => p.Item1, p => p.Item2);
37+
if (dictionary.Count > 0)
38+
{
39+
return new JsonStringEnumConverter(new DictionaryLookupNamingPolicy(dictionary, namingPolicy), allowIntegerValues).CreateConverter(typeToConvert, options);
40+
}
41+
else
42+
{
43+
return new JsonStringEnumConverter(JsonNamingPolicy.CamelCase).CreateConverter(typeToConvert, options);
44+
}
45+
}
46+
}
47+
48+
public class JsonNamingPolicyDecorator : JsonNamingPolicy
49+
{
50+
readonly JsonNamingPolicy underlyingNamingPolicy;
51+
52+
public JsonNamingPolicyDecorator(JsonNamingPolicy underlyingNamingPolicy) => this.underlyingNamingPolicy = underlyingNamingPolicy;
53+
54+
public override string ConvertName(string name) => underlyingNamingPolicy == null ? name : underlyingNamingPolicy.ConvertName(name);
55+
}
56+
57+
internal class DictionaryLookupNamingPolicy : JsonNamingPolicyDecorator
58+
{
59+
readonly Dictionary<string, string> dictionary;
60+
61+
public DictionaryLookupNamingPolicy(Dictionary<string, string> dictionary, JsonNamingPolicy underlyingNamingPolicy) : base(underlyingNamingPolicy) => this.dictionary = dictionary ?? throw new ArgumentNullException();
62+
63+
public override string ConvertName(string name) => dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name);
64+
}
65+
}

0 commit comments

Comments
 (0)