|
| 1 | +package software.xdev.vaadin.chartjs.demo; |
| 2 | + |
| 3 | +import java.util.concurrent.CompletableFuture; |
| 4 | + |
| 5 | +import com.vaadin.flow.component.AttachEvent; |
| 6 | +import com.vaadin.flow.component.UI; |
| 7 | +import com.vaadin.flow.component.button.Button; |
| 8 | +import com.vaadin.flow.component.checkbox.Checkbox; |
| 9 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 10 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 11 | +import com.vaadin.flow.router.Route; |
| 12 | +import com.vaadin.flow.theme.lumo.Lumo; |
| 13 | + |
| 14 | +import software.xdev.chartjs.model.charts.BarChart; |
| 15 | +import software.xdev.chartjs.model.data.BarData; |
| 16 | +import software.xdev.chartjs.model.dataset.BarDataset; |
| 17 | +import software.xdev.chartjs.model.options.BarOptions; |
| 18 | +import software.xdev.chartjs.model.options.Plugins; |
| 19 | +import software.xdev.chartjs.model.options.Title; |
| 20 | +import software.xdev.vaadin.chartjs.ChartContainer; |
| 21 | +import software.xdev.vaadin.chartjs.resources.js.src.ChartThemeManager; |
| 22 | + |
| 23 | + |
| 24 | +@Route(FunctionalityShowcaseDemo.NAV) |
| 25 | +public class FunctionalityShowcaseDemo extends VerticalLayout |
| 26 | +{ |
| 27 | + public static final String NAV = "/functionality"; |
| 28 | + |
| 29 | + private final DummyChartContainer chart = new DummyChartContainer(); |
| 30 | + |
| 31 | + public FunctionalityShowcaseDemo() |
| 32 | + { |
| 33 | + this.setSizeFull(); |
| 34 | + this.chart.setHeight("50%"); |
| 35 | + |
| 36 | + this.add( |
| 37 | + this.chart, |
| 38 | + new Button("Run dummy load", ev -> this.runDummyLoad()), |
| 39 | + new HorizontalLayout( |
| 40 | + new Button("Load", ev -> this.chart.showLoading()), |
| 41 | + new Button("Show data", ev -> this.chart.show()), |
| 42 | + new Button( |
| 43 | + "Show data with problem indicator", ev -> { |
| 44 | + this.chart.show(); |
| 45 | + this.chart.showProblemsIndicator("Failed to load some data because ..."); |
| 46 | + }), |
| 47 | + new Button("Error", ev -> this.chart.showFailed("Something went wrong")) |
| 48 | + ), |
| 49 | + new Checkbox( |
| 50 | + "DarkMode (requires chart rebuild)", ev -> { |
| 51 | + this.getElement().executeJs( |
| 52 | + "document.documentElement.setAttribute('theme', $0)", |
| 53 | + Boolean.TRUE.equals(ev.getValue()) ? Lumo.DARK : Lumo.LIGHT); |
| 54 | + this.getElement().executeJs(ChartThemeManager.TRY_UPDATE_CHART_JS_THEMING.formatted(true)); |
| 55 | + })); |
| 56 | + } |
| 57 | + |
| 58 | + private void runDummyLoad() |
| 59 | + { |
| 60 | + this.chart.showLoading(); |
| 61 | + |
| 62 | + final UI ui = UI.getCurrent(); |
| 63 | + CompletableFuture.runAsync(() -> { |
| 64 | + try |
| 65 | + { |
| 66 | + Thread.sleep(1000); |
| 67 | + } |
| 68 | + catch(final InterruptedException iex) |
| 69 | + { |
| 70 | + Thread.currentThread().interrupt(); |
| 71 | + } |
| 72 | + ui.access(this.chart::show); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void onAttach(final AttachEvent attachEvent) |
| 78 | + { |
| 79 | + this.runDummyLoad(); |
| 80 | + } |
| 81 | + |
| 82 | + @SuppressWarnings("java:S110") |
| 83 | + public static class DummyChartContainer extends ChartContainer |
| 84 | + { |
| 85 | + public void show() |
| 86 | + { |
| 87 | + this.showChart(new BarChart(new BarData() |
| 88 | + .addLabels("2020", "2021", "2022", "2023") |
| 89 | + .addDataset(new BarDataset() |
| 90 | + .setBackgroundColor("#c02222") |
| 91 | + .setLabel("Hans") |
| 92 | + .addData(1) |
| 93 | + .addData(2) |
| 94 | + .addData(3) |
| 95 | + .addData(4)) |
| 96 | + .addDataset(new BarDataset() |
| 97 | + .setBackgroundColor("orange") |
| 98 | + .setLabel("Franz") |
| 99 | + .addData(2) |
| 100 | + .addData(3) |
| 101 | + .addData(4) |
| 102 | + .addData(5))) |
| 103 | + .setOptions(new BarOptions() |
| 104 | + .setResponsive(true) |
| 105 | + .setMaintainAspectRatio(false) |
| 106 | + .setPlugins(new Plugins() |
| 107 | + .setTitle(new Title() |
| 108 | + .setText("Age") |
| 109 | + .setDisplay(true)))) |
| 110 | + .toJson()); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments