From 3082d484e20f80ccb11e7930e5acdbae939a95fa Mon Sep 17 00:00:00 2001 From: Koy Zhuang Date: Tue, 2 Jul 2024 22:56:56 +0800 Subject: [PATCH] update: add radar chart sample --- .../server/SimpleChartServerRenderTest.java | 8 +-- .../koooooo7/echarts4j/chart/BarChart.java | 15 +++++ .../koooooo7/echarts4j/chart/ChartType.java | 5 +- .../koooooo7/echarts4j/chart/LineChart.java | 15 +++++ .../koooooo7/echarts4j/chart/RadarChart.java | 14 +++++ .../echarts4j/option/ChartOption.java | 2 + .../echarts4j/option/chart/Radar.java | 33 ++++++++++ ...Option.java => LineChartSeriesOption.java} | 2 +- .../option/series/RadarChartSeriesOption.java | 38 +++++++++++ .../echarts4j/chart/LineChartTests.java | 6 +- .../echarts4j/chart/OverlapTests.java | 7 +-- .../echarts4j/chart/RadarChartTests.java | 63 +++++++++++++++++++ .../echarts4j/chart/ScatterChartTests.java | 2 - .../echarts4j/chart/ScatterTests.java | 34 ---------- .../playwright/SnapshotRenderTest.java | 4 +- 15 files changed, 195 insertions(+), 53 deletions(-) create mode 100644 echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/RadarChart.java create mode 100644 echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/chart/Radar.java rename echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/{ListChartSeriesOption.java => LineChartSeriesOption.java} (93%) create mode 100644 echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/RadarChartSeriesOption.java create mode 100644 echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/RadarChartTests.java delete mode 100644 echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterTests.java diff --git a/echarts4j-chart-server/src/test/java/com/github/koooooo7/echarts4j/chart/server/SimpleChartServerRenderTest.java b/echarts4j-chart-server/src/test/java/com/github/koooooo7/echarts4j/chart/server/SimpleChartServerRenderTest.java index bbc8bab..9842d31 100644 --- a/echarts4j-chart-server/src/test/java/com/github/koooooo7/echarts4j/chart/server/SimpleChartServerRenderTest.java +++ b/echarts4j-chart-server/src/test/java/com/github/koooooo7/echarts4j/chart/server/SimpleChartServerRenderTest.java @@ -10,7 +10,7 @@ import com.github.koooooo7.echarts4j.option.chart.XAxis; import com.github.koooooo7.echarts4j.option.chart.YAxis; import com.github.koooooo7.echarts4j.option.series.BarChartSeriesOption; -import com.github.koooooo7.echarts4j.option.series.ListChartSeriesOption; +import com.github.koooooo7.echarts4j.option.series.LineChartSeriesOption; import com.github.koooooo7.echarts4j.option.series.SeriesOption; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -97,7 +97,7 @@ void shouldServerChart_WhenUseTheChartServer_GivenChartAndCallTheRender() throws .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build()) @@ -137,7 +137,7 @@ void shouldServerLiveUpdateChart_WhenUseTheChartServer_GivenChartWithCustomUpdat .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build()) @@ -207,7 +207,7 @@ void shouldServerLiveUpdateChart_WhenUseTheChartServer_Given2ChartWithDifferentU .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build()) diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/BarChart.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/BarChart.java index d251d3b..74336f6 100644 --- a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/BarChart.java +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/BarChart.java @@ -1,14 +1,29 @@ package com.github.koooooo7.echarts4j.chart; +import com.github.koooooo7.echarts4j.option.chart.XAxis; +import com.github.koooooo7.echarts4j.option.chart.YAxis; import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.SuperBuilder; +import java.util.Objects; + @Data @SuperBuilder @EqualsAndHashCode(callSuper = true) public class BarChart extends BaseChart { @Builder.Default private ChartType chartType = ChartType.Bar; + + @Override + public void postProcessor() { + super.postProcessor(); + if (Objects.isNull(getChartOptions().getXAxis())) { + getChartOptions().setXAxis(XAxis.builder().build()); + } + if (Objects.isNull(getChartOptions().getYAxis())) { + getChartOptions().setYAxis(YAxis.builder().build()); + } + } } diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/ChartType.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/ChartType.java index e045592..15fdf81 100644 --- a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/ChartType.java +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/ChartType.java @@ -19,8 +19,7 @@ public enum ChartType { Bar("bar"), Pie("pie"), Scatter("scatter"), - EffectScatter("effectScatter") - - ; + EffectScatter("effectScatter"), + Radar("radar"); private final String type; } diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/LineChart.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/LineChart.java index 0670f29..29ad0c1 100644 --- a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/LineChart.java +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/LineChart.java @@ -1,14 +1,29 @@ package com.github.koooooo7.echarts4j.chart; +import com.github.koooooo7.echarts4j.option.chart.XAxis; +import com.github.koooooo7.echarts4j.option.chart.YAxis; import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.SuperBuilder; +import java.util.Objects; + @Data @SuperBuilder @EqualsAndHashCode(callSuper = true) public class LineChart extends BaseChart { @Builder.Default private ChartType chartType = ChartType.Line; + + @Override + public void postProcessor() { + super.postProcessor(); + if (Objects.isNull(getChartOptions().getXAxis())) { + getChartOptions().setXAxis(XAxis.builder().build()); + } + if (Objects.isNull(getChartOptions().getYAxis())) { + getChartOptions().setYAxis(YAxis.builder().build()); + } + } } diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/RadarChart.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/RadarChart.java new file mode 100644 index 0000000..4403dc5 --- /dev/null +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/chart/RadarChart.java @@ -0,0 +1,14 @@ +package com.github.koooooo7.echarts4j.chart; + +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.SuperBuilder; + +@Data +@SuperBuilder +@EqualsAndHashCode(callSuper = true) +public class RadarChart extends BaseChart { + @Builder.Default + private ChartType chartType = ChartType.Radar; +} diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/ChartOption.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/ChartOption.java index a0f56d3..151cd2d 100644 --- a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/ChartOption.java +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/ChartOption.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.github.koooooo7.echarts4j.option.chart.Grid; import com.github.koooooo7.echarts4j.option.chart.Legend; +import com.github.koooooo7.echarts4j.option.chart.Radar; import com.github.koooooo7.echarts4j.option.chart.Toolbox; import com.github.koooooo7.echarts4j.option.chart.Tooltip; import com.github.koooooo7.echarts4j.option.chart.XAxis; @@ -29,6 +30,7 @@ public class ChartOption { private XAxis xAxis; @JsonProperty("yAxis") private YAxis yAxis; + private Radar radar; private Tooltip tooltip; private Toolbox toolbox; private Object color; diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/chart/Radar.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/chart/Radar.java new file mode 100644 index 0000000..a6f2fbe --- /dev/null +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/chart/Radar.java @@ -0,0 +1,33 @@ +package com.github.koooooo7.echarts4j.option.chart; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.github.koooooo7.echarts4j.type.FuncStr; +import lombok.Builder; +import lombok.Data; + +import java.util.List; + +@Data +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) +public class Radar { + private String id; + @JsonProperty("zlevel") + private Integer zLevel; + private Integer z; + private FuncStr center; + private Object radius; + private List indicator; + + + @Data + @Builder + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class IndicatorDataItem { + private String name; + private Integer max; + private Integer min; + private String color; + } +} diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/ListChartSeriesOption.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/LineChartSeriesOption.java similarity index 93% rename from echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/ListChartSeriesOption.java rename to echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/LineChartSeriesOption.java index fb24f47..640a0a6 100644 --- a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/ListChartSeriesOption.java +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/LineChartSeriesOption.java @@ -13,7 +13,7 @@ @SuperBuilder @JsonInclude(JsonInclude.Include.NON_NULL) @EqualsAndHashCode(callSuper = true) -public class ListChartSeriesOption extends GenericSeriesOption implements SeriesOption { +public class LineChartSeriesOption extends GenericSeriesOption implements SeriesOption { @Builder.Default private String type = ChartType.Line.getType(); private List data; diff --git a/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/RadarChartSeriesOption.java b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/RadarChartSeriesOption.java new file mode 100644 index 0000000..d14e2e2 --- /dev/null +++ b/echarts4j-project/src/main/java/com/github/koooooo7/echarts4j/option/series/RadarChartSeriesOption.java @@ -0,0 +1,38 @@ +package com.github.koooooo7.echarts4j.option.series; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.github.koooooo7.echarts4j.chart.ChartType; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.SuperBuilder; + +import java.util.List; + +@Data +@SuperBuilder +@JsonInclude(JsonInclude.Include.NON_NULL) +@EqualsAndHashCode(callSuper = true) +public class RadarChartSeriesOption extends GenericSeriesOption implements SeriesOption { + @Builder.Default + private String type = ChartType.Radar.getType(); + private List data; + + + @Data + @SuperBuilder + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class RadarDataItem { + private String name; + private Integer value; + private String groupId; + private String childGroupId; + private String symbol; + private Object symbolSize; + private Integer symbolRotate; + private Boolean symbolKeepAspect; + private List symbolOffset; + + } + +} diff --git a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/LineChartTests.java b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/LineChartTests.java index 580a8cc..4752b45 100644 --- a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/LineChartTests.java +++ b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/LineChartTests.java @@ -6,7 +6,7 @@ import com.github.koooooo7.echarts4j.option.chart.Toolbox; import com.github.koooooo7.echarts4j.option.chart.XAxis; import com.github.koooooo7.echarts4j.option.chart.YAxis; -import com.github.koooooo7.echarts4j.option.series.ListChartSeriesOption; +import com.github.koooooo7.echarts4j.option.series.LineChartSeriesOption; import com.github.koooooo7.echarts4j.render.Render; import com.github.koooooo7.echarts4j.render.RenderProvider; import com.github.koooooo7.echarts4j.type.FuncStr; @@ -66,11 +66,11 @@ void shouldRenderCorrectly_WhenRenderTheLineCharts_GivenRelatedConfigs() throws .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build()) - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName2) .data(data2) .build()) diff --git a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/OverlapTests.java b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/OverlapTests.java index 3453293..906c040 100644 --- a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/OverlapTests.java +++ b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/OverlapTests.java @@ -6,7 +6,7 @@ import com.github.koooooo7.echarts4j.option.chart.XAxis; import com.github.koooooo7.echarts4j.option.chart.YAxis; import com.github.koooooo7.echarts4j.option.series.BarChartSeriesOption; -import com.github.koooooo7.echarts4j.option.series.ListChartSeriesOption; +import com.github.koooooo7.echarts4j.option.series.LineChartSeriesOption; import com.github.koooooo7.echarts4j.render.Render; import com.github.koooooo7.echarts4j.render.RenderProvider; import org.apache.commons.lang3.RandomStringUtils; @@ -14,7 +14,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.FileWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; @@ -54,7 +53,7 @@ void showRenderOverlapChart_WhenCallTheOverlapViaSeries_GivenCorrectConfigs() { .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build()) @@ -108,7 +107,7 @@ void showRenderOverlapChart_WhenCallTheOverlapViaChartOverlapMethod_GivenCorrect .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName2) .data(data1) .build()) diff --git a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/RadarChartTests.java b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/RadarChartTests.java new file mode 100644 index 0000000..91edcd2 --- /dev/null +++ b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/RadarChartTests.java @@ -0,0 +1,63 @@ +package com.github.koooooo7.echarts4j.chart; + +import com.github.koooooo7.echarts4j.helper.DataHelper; +import com.github.koooooo7.echarts4j.option.ChartOption; +import com.github.koooooo7.echarts4j.option.chart.Legend; +import com.github.koooooo7.echarts4j.option.chart.Radar; +import com.github.koooooo7.echarts4j.option.chart.Title; +import com.github.koooooo7.echarts4j.option.series.RadarChartSeriesOption; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +public class RadarChartTests { + @Test + void shouldGenerateScatterChart_WhenCallThePieChartConfig_GivenNecessaryConfigs() { + List item = new ArrayList<>(); + List item2 = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + item.add(ThreadLocalRandom.current().nextInt(5000, 16000)); + item2.add(ThreadLocalRandom.current().nextInt(5000, 16000)); + } + + final List> finalData = DataHelper.create() + .addNameField() + .addValueField(List.class) + .build() + .addData("Promotion", item) + .addData("Baseline", item2) + .get(); + final List legendDate = new ArrayList<>(); + legendDate.add("Allocated Budget"); + legendDate.add("Actual Spending"); + + final List indicator = new ArrayList<>(); + indicator.add(Radar.IndicatorDataItem.builder().name("Sales").max(16500).build()); + indicator.add(Radar.IndicatorDataItem.builder().name("Administration").max(26000).build()); + indicator.add(Radar.IndicatorDataItem.builder().name("Information Technology").max(35000).build()); + indicator.add(Radar.IndicatorDataItem.builder().name("Customer Support").max(16500).build()); + indicator.add(Radar.IndicatorDataItem.builder().name("Development").max(65000).build()); + indicator.add(Radar.IndicatorDataItem.builder().name("Marketing").max(16500).build()); + + Canvas.builder() + .addCharts(RadarChart.builder() + .options(ChartOption.builder() + .title(Title.builder().text("Basic Radar Chart").build()) + .legend(Legend.builder().data(legendDate).build()) + .radar(Radar.builder() + .indicator(indicator) + .build()) + .build() + .addSeries(RadarChartSeriesOption.builder() + .name("P&L") + .data(finalData) + .build())) + .build()) + .build() + .render(); + + } +} diff --git a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterChartTests.java b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterChartTests.java index 2621b61..aed5029 100644 --- a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterChartTests.java +++ b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterChartTests.java @@ -7,7 +7,6 @@ import com.github.koooooo7.echarts4j.option.chart.XAxis; import com.github.koooooo7.echarts4j.option.chart.YAxis; import com.github.koooooo7.echarts4j.option.series.EffectScatterChartSeriesOption; -import com.github.koooooo7.echarts4j.option.series.ListChartSeriesOption; import com.github.koooooo7.echarts4j.option.series.ScatterChartSeriesOption; import com.github.koooooo7.echarts4j.render.Render; import com.github.koooooo7.echarts4j.render.RenderProvider; @@ -17,7 +16,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; diff --git a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterTests.java b/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterTests.java deleted file mode 100644 index ce27dae..0000000 --- a/echarts4j-project/src/test/java/com/github/koooooo7/echarts4j/chart/ScatterTests.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.koooooo7.echarts4j.chart; - -import com.github.koooooo7.echarts4j.option.ChartOption; -import com.github.koooooo7.echarts4j.option.chart.Title; -import com.github.koooooo7.echarts4j.option.series.ScatterChartSeriesOption; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ThreadLocalRandom; - -public class ScatterTests { - @Test - void shouldGenerateScatterChart_WhenCallThePieChartConfig_GivenNecessaryConfigs() { - List> data = new ArrayList<>(); - for (int i = 0; i < 20; i++) { - List item = new ArrayList<>(); - item.add(ThreadLocalRandom.current().nextInt(100)); - item.add(ThreadLocalRandom.current().nextInt(100)); - data.add(item); - } - Canvas.builder() - .addCharts(ScatterChart.builder() - .options(ChartOption.builder() - .title(Title.builder().text("Scatter Chart").build()) - .build() - .addSeries(ScatterChartSeriesOption.builder() - .data(data).build())) - .build()) - .build() - .render(); - - } -} diff --git a/echarts4j-snapshot-playwright/src/test/java/com/github/koooooo7/echarts4j/snapshot/playwright/SnapshotRenderTest.java b/echarts4j-snapshot-playwright/src/test/java/com/github/koooooo7/echarts4j/snapshot/playwright/SnapshotRenderTest.java index 4481268..4e4c4f0 100644 --- a/echarts4j-snapshot-playwright/src/test/java/com/github/koooooo7/echarts4j/snapshot/playwright/SnapshotRenderTest.java +++ b/echarts4j-snapshot-playwright/src/test/java/com/github/koooooo7/echarts4j/snapshot/playwright/SnapshotRenderTest.java @@ -8,7 +8,7 @@ import com.github.koooooo7.echarts4j.option.chart.XAxis; import com.github.koooooo7.echarts4j.option.chart.YAxis; import com.github.koooooo7.echarts4j.option.series.BarChartSeriesOption; -import com.github.koooooo7.echarts4j.option.series.ListChartSeriesOption; +import com.github.koooooo7.echarts4j.option.series.LineChartSeriesOption; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -52,7 +52,7 @@ void shouldGeneratePNG_WhenCallTheRender_GivenSnapshotRender() { .build()) .yAxis(YAxis.builder().build()) .build() - .addSeries(ListChartSeriesOption.builder() + .addSeries(LineChartSeriesOption.builder() .name(seriesName) .data(data1) .build())