From af79286cb06f23267d0012f9095b399e2abcb126 Mon Sep 17 00:00:00 2001 From: Pit Friedrich Date: Mon, 6 Jul 2026 07:33:15 +0200 Subject: [PATCH] Different types of charts --- .gites/workflows/ci.yml | 24 ++++++++++++ .../com/example/components/ApexChart.java | 35 ++++------------- .../com/example/components/AxisChart.java | 21 ++++++++++ .../java/com/example/components/BarChart.java | 8 ++++ .../com/example/components/LineChart.java | 8 ++++ .../java/com/example/components/PieChart.java | 21 ++++++++++ .../java/com/example/views/DashboardView.java | 38 +++++++++++++++---- src/main/resources/application.properties | 9 ----- src/main/resources/application.yml | 16 ++++++++ 9 files changed, 136 insertions(+), 44 deletions(-) create mode 100644 .gites/workflows/ci.yml create mode 100644 src/main/java/com/example/components/AxisChart.java create mode 100644 src/main/java/com/example/components/BarChart.java create mode 100644 src/main/java/com/example/components/LineChart.java create mode 100644 src/main/java/com/example/components/PieChart.java delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml diff --git a/.gites/workflows/ci.yml b/.gites/workflows/ci.yml new file mode 100644 index 0000000..b05fec1 --- /dev/null +++ b/.gites/workflows/ci.yml @@ -0,0 +1,24 @@ +name: CI + +on: + pull_request: + branches: + - master + +jobs: + build-and-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Java 25 (Temurin) + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '25' + cache: maven + + - name: Build and Test + run: | + chmod +x mvnw + ./mvnw verify diff --git a/src/main/java/com/example/components/ApexChart.java b/src/main/java/com/example/components/ApexChart.java index 942b113..55b827b 100644 --- a/src/main/java/com/example/components/ApexChart.java +++ b/src/main/java/com/example/components/ApexChart.java @@ -7,45 +7,26 @@ import com.vaadin.flow.component.dependency.NpmPackage; import com.vaadin.flow.shared.Registration; import tools.jackson.databind.json.JsonMapper; -import java.util.List; import java.util.Map; @Tag("apex-chart") @NpmPackage(value = "apexcharts", version = "5.15.2") @JsModule("./components/apex-chart.ts") -public class ApexChart extends Component implements HasSize { +public abstract class ApexChart extends Component implements HasSize { - public enum ChartType { - BAR ("bar"), - LINE ("line") - ; + protected static final JsonMapper MAPPER = JsonMapper.builder().build(); - private final String type; + private final String chartType; - ChartType(String type) { - this.type = type; - } - - public String getType() { - return type; - } - } - - private static final JsonMapper MAPPER = JsonMapper.builder().build(); - - private final ChartType chartType; - - public ApexChart(ChartType chartType) { + protected ApexChart(String chartType) { this.chartType = chartType; } - public void setData(String seriesName, List values, List categories) { - Map options = Map.of( - "chart", Map.of("type", chartType.getType(), "height", "100%"), - "series", List.of(Map.of("name", seriesName, "data", values)), - "xaxis", Map.of("categories", categories) - ); + protected String getChartType() { + return chartType; + } + protected void sendOptions(Map options) { getElement().callJsFunction("renderChart", MAPPER.writeValueAsString(options)); } diff --git a/src/main/java/com/example/components/AxisChart.java b/src/main/java/com/example/components/AxisChart.java new file mode 100644 index 0000000..3c09cad --- /dev/null +++ b/src/main/java/com/example/components/AxisChart.java @@ -0,0 +1,21 @@ +package com.example.components; + +import java.util.List; +import java.util.Map; + +public abstract class AxisChart extends ApexChart { + + protected AxisChart(String chartType) { + super(chartType); + } + + public void setData(String seriesName, List values, List categories) { + Map options = Map.of( + "chart", Map.of("type", getChartType(), "height", "100%"), + "series", List.of(Map.of("name", seriesName, "data", values)), + "xaxis", Map.of("categories", categories) + ); + + sendOptions(options); + } +} diff --git a/src/main/java/com/example/components/BarChart.java b/src/main/java/com/example/components/BarChart.java new file mode 100644 index 0000000..973f7a8 --- /dev/null +++ b/src/main/java/com/example/components/BarChart.java @@ -0,0 +1,8 @@ +package com.example.components; + +public class BarChart extends AxisChart { + + public BarChart() { + super("bar"); + } +} diff --git a/src/main/java/com/example/components/LineChart.java b/src/main/java/com/example/components/LineChart.java new file mode 100644 index 0000000..3de4502 --- /dev/null +++ b/src/main/java/com/example/components/LineChart.java @@ -0,0 +1,8 @@ +package com.example.components; + +public class LineChart extends AxisChart { + + public LineChart() { + super("line"); + } +} diff --git a/src/main/java/com/example/components/PieChart.java b/src/main/java/com/example/components/PieChart.java new file mode 100644 index 0000000..8a341c4 --- /dev/null +++ b/src/main/java/com/example/components/PieChart.java @@ -0,0 +1,21 @@ +package com.example.components; + +import java.util.List; +import java.util.Map; + +public class PieChart extends ApexChart { + + public PieChart() { + super("pie"); + } + + public void setData(List values, List labels) { + Map options = Map.of( + "chart", Map.of("type", "pie", "height", "100%"), + "series", values, + "labels", labels + ); + + sendOptions(options); + } +} diff --git a/src/main/java/com/example/views/DashboardView.java b/src/main/java/com/example/views/DashboardView.java index 3c88026..00a98d7 100644 --- a/src/main/java/com/example/views/DashboardView.java +++ b/src/main/java/com/example/views/DashboardView.java @@ -1,6 +1,8 @@ package com.example.views; -import com.example.components.ApexChart; +import com.example.components.BarChart; +import com.example.components.LineChart; +import com.example.components.PieChart; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.Route; @@ -11,18 +13,38 @@ import java.util.List; public class DashboardView extends VerticalLayout { public DashboardView() { - ApexChart chart = new ApexChart(ApexChart.ChartType.LINE); - chart.setWidthFull(); - chart.setHeight("400px"); - chart.setData("Umsatz 2026", + LineChart lineChart = new LineChart(); + lineChart.setWidthFull(); + lineChart.setHeight("400px"); + lineChart.setData("Umsatz 2026", List.of(30.0, 40.0, 35.0, 50.0, 49.0, 60.0), List.of("Jan", "Feb", "Mär", "Apr", "Mai", "Jun")); - - chart.addPointClickListener(e -> { + lineChart.addPointClickListener(e -> { Notification.show("Serie %d, Punkt %d" .formatted(e.getSeriesIndex(), e.getDataPointIndex())); }); - add(chart); + BarChart barChart = new BarChart(); + barChart.setWidthFull(); + barChart.setHeight("400px"); + barChart.setData("Umsatz 2026", + List.of(30.0, 40.0, 35.0, 50.0, 49.0, 60.0), + List.of("Jan", "Feb", "Mär", "Apr", "Mai", "Jun")); + barChart.addPointClickListener(e -> { + Notification.show("Serie %d, Punkt %d" + .formatted(e.getSeriesIndex(), e.getDataPointIndex())); + }); + + PieChart pieChart = new PieChart(); + pieChart.setWidthFull(); + pieChart.setHeight("400px"); + pieChart.setData( + List.of(30.0, 40.0, 35.0, 50.0), + List.of("Nord", "Süd", "Ost", "West")); + pieChart.addPointClickListener(e -> { + Notification.show("Slice %d".formatted(e.getDataPointIndex())); + }); + + add(lineChart, barChart, pieChart); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 850c9e3..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,9 +0,0 @@ -server.port=${PORT:8080} -logging.level.org.atmosphere=warn - -# Launch the default browser when starting the application in development mode -vaadin.launch-browser=true - -# To improve the performance during development. -# For more information https://vaadin.com/docs/latest/flow/integrations/spring/configuration#special-configuration-parameters -vaadin.allowed-packages=com.vaadin,org.vaadin,com.flowingcode,com.example diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..741aa91 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,16 @@ +server: + port: ${PORT:8080} + servlet: + session: + timeout: ${SESSION_TIMEOUT:15m} + +logging: + level: + org.atmosphere: warn + +vaadin: + # Launch the default browser when starting the application in development mode + launch-browser: true + # To improve the performance during development. + # For more information https://vaadin.com/docs/latest/flow/integrations/spring/configuration#special-configuration-parameters + allowed-packages: com.vaadin,org.vaadin,com.flowingcode,com.example