Skip to content

Commit dc8f431

Browse files
committed
Changed VisualMap to polymorphic serialization
1 parent 6e75a8d commit dc8f431

File tree

9 files changed

+171
-27
lines changed

9 files changed

+171
-27
lines changed

src/Vizor.ECharts.BindingGenerator/GenerateOptionBindingTool.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ public int Run(GenerateOptionBindingOptions options)
113113
iDataZoomGenerator.Generate();
114114
}
115115

116+
// Generate IVisualMap interface with all JsonDerivedType attributes
117+
var visualMapTypes = typeCollection.ListObjectTypesToGenerate()
118+
.Where(t => !t.IsShared && t.Name.EndsWith("VisualMap"))
119+
.ToList();
120+
121+
if (visualMapTypes.Count > 0)
122+
{
123+
var iVisualMapGenerator = new PolymorphicInterfaceGenerator(
124+
options.OutputDirectory,
125+
"IVisualMap",
126+
"Options/VisualMap",
127+
visualMapTypes,
128+
"VisualMap",
129+
echartsVersion);
130+
iVisualMapGenerator.Generate();
131+
}
132+
116133
Console.WriteLine("done.");
117134

118135
return 0;

src/Vizor.ECharts.BindingGenerator/Generators/ObjectTypeClassGenerator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ public void Generate()
6161
var isSeries = objectType.TypeGroup.Contains("Series") &&
6262
objectType.DotNetType.EndsWith("Series") &&
6363
!objectType.DotNetType.EndsWith("SeriesData");
64-
var baseTypes = isSeries ? new[] { "ISeries" } : Array.Empty<string>();
64+
65+
// Add IVisualMap interface for VisualMap types
66+
var isVisualMap = objectType.TypeGroup == "VisualMap" &&
67+
objectType.DotNetType.EndsWith("VisualMap");
68+
69+
var baseTypes = isSeries ? new[] { "ISeries" } :
70+
isVisualMap ? new[] { "IVisualMap" } :
71+
Array.Empty<string>();
6572
writer.WriteClassDeclaration(objectType.DotNetType, baseTypes);
6673
writer.OpenBrace();
6774

@@ -137,9 +144,9 @@ public void Generate()
137144
}
138145
else
139146
{
140-
// Skip 'type' property for Series and DataZoom - handled by polymorphic serialization
147+
// Skip 'type' property for polymorphic types (Series, DataZoom, VisualMap) - handled by polymorphic serialization
141148
bool isPolymorphicTypeProperty =
142-
(objectType.TypeGroup == "Series" || objectType.Name.EndsWith("DataZoom")) &&
149+
(objectType.TypeGroup == "Series" || objectType.Name.EndsWith("DataZoom") || objectType.TypeGroup == "VisualMap") &&
143150
prop.Name == "type";
144151

145152
if (isPolymorphicTypeProperty)

src/Vizor.ECharts.BindingGenerator/Types/TypeCollection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public IEnumerable<ObjectType> ListObjectTypesToGenerate()
162162
case ("dataZoom", "ChartOptions"):
163163
// Use typed list of IDataZoom for type safety instead of List<object>
164164
return new GenericListType(new SimpleType("IDataZoom"));
165+
case ("visualMap", "ChartOptions"):
166+
// Allow single visualMap object or array of visual maps
167+
return new SingleOrArrayType("IVisualMap");
165168
case ("dimensions", "Dataset"):
166169
case ("dimensions", _) when parent.DotNetType.EndsWith("Series"):
167170
// Use string array (full union type support planned for future)

src/Vizor.ECharts.Tests/TestFixtures/ChartOptionsBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static ChartOptions ScatterChartWithVisualMap()
5353
Title = new() { Text = "Scatter with Visual Map" },
5454
XAxis = new() { Type = AxisType.Value },
5555
YAxis = new() { Type = AxisType.Value },
56-
VisualMap = new List<object>
56+
VisualMapList = new List<IVisualMap>
5757
{
5858
new ContinuousVisualMap
5959
{

src/Vizor.ECharts/Options/Generated/ChartOptions.cs

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,130 @@ public List<YAxis>? YAxisList
416416
/// ]]>
417417
/// </summary>
418418
[JsonPropertyName("visualMap")]
419-
public List<object>? VisualMap { get; set; }
419+
[JsonInclude]
420+
internal object? VisualMapObject { get; set; }
421+
422+
/// <summary>
423+
/// <![CDATA[
424+
/// visualMap is a type of component for visual encoding, which maps the data to visual channels, including: symbol : Type of symbol.
425+
/// symbolSize : Symbol size.
426+
/// color : Symbol color.
427+
/// colorAlpha : Symbol alpha channel.
428+
/// opacity : Opacity of symbol and others (like labels).
429+
/// colorLightness : Lightness in HSL .
430+
/// colorSaturation : Saturation in HSL .
431+
/// colorHue : Hue in HSL .
432+
///
433+
/// Multiple visualMap component could be defined in a chart instance, which enable that different dimensions of a series data are mapped to different visual channels.
434+
///
435+
/// visualMap could be defined as Piecewise (visualMapPiecewise) or Continuous (visualMapContinuous) , which is distinguished by the property type .
436+
/// For instance: option = {
437+
/// visualMap: [
438+
/// { // the first visualMap component
439+
/// type: 'continuous', // defined to be continuous visualMap
440+
/// ...
441+
/// },
442+
/// { // the second visualMap component
443+
/// type: 'piecewise', // defined to be piecewise visualMap
444+
/// ...
445+
/// }
446+
/// ],
447+
/// ...
448+
/// };
449+
///
450+
/// ✦ Configure mapping ✦
451+
/// The dimension of series.data can be specified by visualMap.dimension , from which the value can be retrieved and mapped onto visual channels, which can be defined in visualMap.inRange and visualMap.outOfRange .
452+
///
453+
///
454+
/// In series that controlled by visualMap, if a data item needs to escape from controlled by visualMap, you can set like this: series: {
455+
/// type: '...',
456+
/// data: [
457+
/// {name: 'Shanghai', value: 251},
458+
/// {name: 'Haikou', value: 21},
459+
/// // Mark as `visualMap: false`, then this item does not controlled by visualMap any more,
460+
/// // and series visual config (like color, symbol, ...) can be used to this item.
461+
/// {name: 'Beijing', value: 821, },
462+
/// ...
463+
/// ]
464+
/// }
465+
///
466+
/// ✦ The relationship between visualMap of ECharts3 and dataRange of ECharts2 ✦
467+
/// visualMap is renamed from the dataRange of ECharts2, and the scope of functionalities are extended a lot.
468+
/// The configurations of dataRange are still compatible in ECharts3, which automatically convert them to visualMap .
469+
/// It is recommended to use visualMap instead of dataRange in ECharts3.
470+
///
471+
///
472+
/// ✦ The detailed configurations of visualMap are elaborated as follows.
473+
/// ✦
474+
/// ]]>
475+
/// </summary>
476+
[JsonIgnore]
477+
public IVisualMap? VisualMap
478+
{
479+
get => VisualMapObject as IVisualMap;
480+
set => VisualMapObject = value;
481+
}
482+
483+
/// <summary>
484+
/// <![CDATA[
485+
/// visualMap is a type of component for visual encoding, which maps the data to visual channels, including: symbol : Type of symbol.
486+
/// symbolSize : Symbol size.
487+
/// color : Symbol color.
488+
/// colorAlpha : Symbol alpha channel.
489+
/// opacity : Opacity of symbol and others (like labels).
490+
/// colorLightness : Lightness in HSL .
491+
/// colorSaturation : Saturation in HSL .
492+
/// colorHue : Hue in HSL .
493+
///
494+
/// Multiple visualMap component could be defined in a chart instance, which enable that different dimensions of a series data are mapped to different visual channels.
495+
///
496+
/// visualMap could be defined as Piecewise (visualMapPiecewise) or Continuous (visualMapContinuous) , which is distinguished by the property type .
497+
/// For instance: option = {
498+
/// visualMap: [
499+
/// { // the first visualMap component
500+
/// type: 'continuous', // defined to be continuous visualMap
501+
/// ...
502+
/// },
503+
/// { // the second visualMap component
504+
/// type: 'piecewise', // defined to be piecewise visualMap
505+
/// ...
506+
/// }
507+
/// ],
508+
/// ...
509+
/// };
510+
///
511+
/// ✦ Configure mapping ✦
512+
/// The dimension of series.data can be specified by visualMap.dimension , from which the value can be retrieved and mapped onto visual channels, which can be defined in visualMap.inRange and visualMap.outOfRange .
513+
///
514+
///
515+
/// In series that controlled by visualMap, if a data item needs to escape from controlled by visualMap, you can set like this: series: {
516+
/// type: '...',
517+
/// data: [
518+
/// {name: 'Shanghai', value: 251},
519+
/// {name: 'Haikou', value: 21},
520+
/// // Mark as `visualMap: false`, then this item does not controlled by visualMap any more,
521+
/// // and series visual config (like color, symbol, ...) can be used to this item.
522+
/// {name: 'Beijing', value: 821, },
523+
/// ...
524+
/// ]
525+
/// }
526+
///
527+
/// ✦ The relationship between visualMap of ECharts3 and dataRange of ECharts2 ✦
528+
/// visualMap is renamed from the dataRange of ECharts2, and the scope of functionalities are extended a lot.
529+
/// The configurations of dataRange are still compatible in ECharts3, which automatically convert them to visualMap .
530+
/// It is recommended to use visualMap instead of dataRange in ECharts3.
531+
///
532+
///
533+
/// ✦ The detailed configurations of visualMap are elaborated as follows.
534+
/// ✦
535+
/// ]]>
536+
/// </summary>
537+
[JsonIgnore]
538+
public List<IVisualMap>? VisualMapList
539+
{
540+
get => VisualMapObject as List<IVisualMap>;
541+
set => VisualMapObject = value;
542+
}
420543

421544
/// <summary>
422545
/// Tooltip component.

src/Vizor.ECharts/Options/Generated/VisualMap/ContinuousVisualMap.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
namespace Vizor.ECharts;
1010

11-
public partial class ContinuousVisualMap
11+
public partial class ContinuousVisualMap : IVisualMap
1212
{
13-
/// <summary>
14-
/// Used to determine that it is a continuous visualMap component.
15-
/// </summary>
16-
[JsonPropertyName("type")]
17-
[DefaultValue("continuous")]
18-
public string Type { get; init; } = "continuous";
19-
2013
/// <summary>
2114
/// Component ID, not specified by default.
2215
/// If specified, it can be used to refer the component in option or API.

src/Vizor.ECharts/Options/Generated/VisualMap/PiecewiseVisualMap.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
namespace Vizor.ECharts;
1010

11-
public partial class PiecewiseVisualMap
11+
public partial class PiecewiseVisualMap : IVisualMap
1212
{
13-
/// <summary>
14-
/// Used to determine it is a piecewise visualMap component.
15-
/// </summary>
16-
[JsonPropertyName("type")]
17-
[DefaultValue("piecewise")]
18-
public string Type { get; init; } = "piecewise";
19-
2013
/// <summary>
2114
/// Component ID, not specified by default.
2215
/// If specified, it can be used to refer the component in option or API.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// AUTO GENERATED - DO NOT EDIT - All changes will be lost
2+
// ECharts Version: 5.6.0
3+
// http://www.datahint.eu/
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace Vizor.ECharts;
8+
9+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
10+
[JsonDerivedType(typeof(ContinuousVisualMap), "continuous")]
11+
[JsonDerivedType(typeof(PiecewiseVisualMap), "piecewise")]
12+
public interface IVisualMap
13+
{
14+
}

src/Vizor.ECharts/Options/VisualMap/IVisualMap.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)