diff --git a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java index 8f20f04..b40af00 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java @@ -271,6 +271,7 @@ public class Htmlizer BarChart chart; chart = new BarChart("Nombre de membres"); + chart.addDataset("Membres"); PathPropertyList values = federation.getByYearPrefix("metrics.members.count").sortByPath(); @@ -455,7 +456,7 @@ public class Htmlizer * To bar chart. * * @param stats - * the statsf + * the stats * @return the bar chart */ public static BarChart toBarChart(final RegistrationStats stats) @@ -463,6 +464,7 @@ public class Htmlizer BarChart result; result = new BarChart("Types d'inscription"); + result.addDataset("Nombre"); result.add("Sans", stats.getNoneCount(), ChartColor.GREEN); result.add("Libre", stats.getFreeCount(), ChartColor.YELLOW); result.add("Membre", stats.getMemberCount(), ChartColor.ORANGE); diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChart.java b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChart.java index 53e0b98..9a60751 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChart.java +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChart.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Christian Pierre MOMON + * Copyright (C) 2020-2021 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -32,7 +32,8 @@ public class BarChart private String title; private boolean displayTitle; - private BarChartDatas datas; + private StringList labels; + private BarChartDatasets datasets; /** * Instantiates a new bar chart. @@ -44,7 +45,8 @@ public class BarChart { this.title = title; this.displayTitle = true; - this.datas = new BarChartDatas(); + this.labels = new StringList(); + this.datasets = new BarChartDatasets(); } /** @@ -59,7 +61,27 @@ public class BarChart */ public void add(final String label, final double value, final ChartColor color) { - this.datas.add(new BarChartData(label, value, color)); + this.labels.add(label); + this.datasets.get(0).add(new BarChartData(label, value, color)); + } + + /** + * Adds the dataset. + * + * @param name + * the name + * @return the int + */ + public int addDataset(final String name) + { + int result; + + this.datasets.add(new BarChartDataset(name)); + + result = this.datasets.size() - 1; + + // + return result; } /** @@ -71,20 +93,32 @@ public class BarChart { ChartColors result; - result = new ChartColors(); - - for (BarChartData data : this.datas) - { - result.add(data.getColor()); - } + result = this.datasets.get(0).getColors(); // return result; } - public BarChartDatas getDatas() + /** + * Gets the dataset. + * + * @param index + * the index + * @return the dataset + */ + public BarChartDataset getDataset(final int index) { - return this.datas; + BarChartDataset result; + + result = this.datasets.get(index); + + // + return result; + } + + public BarChartDatasets getDatasets() + { + return this.datasets; } /** @@ -96,42 +130,20 @@ public class BarChart { StringList result; - result = new StringList(); - - for (BarChartData data : this.datas) - { - result.add(data.getLabel()); - } + result = this.labels; // return result; } - public String getTitle() - { - return this.title; - } - /** - * Gets the values. + * Gets the title. * - * @return the values + * @return the title */ - public double[] getValues() + public String getTitle() { - double[] result; - - result = new double[this.datas.size()]; - - int index = 0; - for (BarChartData data : this.datas) - { - result[index] = data.getValue(); - index += 1; - } - - // - return result; + return this.title; } public boolean isDisplayTitle() diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatas.java b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatas.java deleted file mode 100644 index 741a743..0000000 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatas.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2020 Christian Pierre MOMON - * - * This file is part of StatoolInfos, simple service statistics tool. - * - * StatoolInfos is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * StatoolInfos is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with StatoolInfos. If not, see . - */ -package fr.devinsy.statoolinfos.htmlize.charts; - -import java.util.ArrayList; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The Class BarChartDatas. - */ -public class BarChartDatas extends ArrayList -{ - private static final long serialVersionUID = -5230173915580487951L; - private static Logger logger = LoggerFactory.getLogger(BarChartDatas.class); - - /** - * Instantiates a new bar chart datas. - */ - public BarChartDatas() - { - super(); - } -} diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDataset.java b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDataset.java new file mode 100644 index 0000000..28b460d --- /dev/null +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDataset.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2020-2021 Christian Pierre MOMON + * + * This file is part of StatoolInfos, simple service statistics tool. + * + * StatoolInfos is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * StatoolInfos is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with StatoolInfos. If not, see . + */ +package fr.devinsy.statoolinfos.htmlize.charts; + +import java.util.ArrayList; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class BarChartDataset. + */ +public class BarChartDataset extends ArrayList +{ + private static final long serialVersionUID = -5230173915580487951L; + private static Logger logger = LoggerFactory.getLogger(BarChartDataset.class); + + private String name; + private int borderWidth; + + /** + * Instantiates a new bar chart dataset. + * + * @param name + * the name + */ + public BarChartDataset(final String name) + { + super(); + this.name = name; + this.borderWidth = 1; + } + + public int getBorderWidth() + { + return this.borderWidth; + } + + /** + * Gets the colors. + * + * @return the colors + */ + public ChartColors getColors() + { + ChartColors result; + + result = new ChartColors(); + + for (BarChartData data : this) + { + result.add(data.getColor()); + } + + // + return result; + } + + public String getName() + { + return this.name; + } + + /** + * Gets the values. + * + * @return the values + */ + public double[] getValues() + { + double[] result; + + result = new double[this.size()]; + + int index = 0; + for (BarChartData data : this) + { + result[index] = data.getValue(); + index += 1; + } + + // + return result; + } + + public void setBorderWidth(final int borderWidth) + { + this.borderWidth = borderWidth; + } + + public void setName(final String name) + { + this.name = name; + } +} diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatasets.java b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatasets.java new file mode 100644 index 0000000..648d8f6 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartDatasets.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020-2021 Christian Pierre MOMON + * + * This file is part of StatoolInfos, simple service statistics tool. + * + * StatoolInfos is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * StatoolInfos is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with StatoolInfos. If not, see . + */ +package fr.devinsy.statoolinfos.htmlize.charts; + +import java.util.ArrayList; +import java.util.Iterator; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class BarChartDatasets. + */ +public class BarChartDatasets extends ArrayList +{ + private static final long serialVersionUID = 306645976348400468L; + + private static Logger logger = LoggerFactory.getLogger(BarChartDatasets.class); + + /** + * Instantiates a new bar chart datasets. + */ + public BarChartDatasets() + { + super(); + } + + /** + * Gets the by name. + * + * @param name + * the name + * @return the by name + */ + public BarChartDataset get(final String name) + { + BarChartDataset result; + + boolean ended = false; + result = null; + Iterator iterator = this.iterator(); + while (!ended) + { + if (iterator.hasNext()) + { + BarChartDataset dataset = iterator.next(); + if (StringUtils.equals(dataset.getName(), name)) + { + ended = true; + result = dataset; + } + } + else + { + ended = true; + result = null; + } + } + + // + return result; + } + +} diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartView.java b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartView.java index 13f9182..6a06d33 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartView.java +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/BarChartView.java @@ -21,6 +21,7 @@ package fr.devinsy.statoolinfos.htmlize.charts; import java.io.IOException; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,12 +54,66 @@ public class BarChartView String source = XidynUtils.load(BarChartView.class.getResource("/fr/devinsy/statoolinfos/htmlize/charts/barChartView.xhtml")); result = XidynUtils.extractBodyContent(source); - result = result.replace("myChart", "myChart_" + DigestUtils.sha1Hex(bar.getTitle() + "PieChart")); - result = result.replaceFirst("labels: \\[.*\\]", "labels: " + ChabuUtils.toJSonStrings(bar.getLabels())); - result = result.replaceFirst("data: \\[.*\\]", "data: " + ChabuUtils.toJSonNumbers(new StringList(bar.getValues()))); - result = result.replaceFirst("backgroundColor: \\[.*\\]", "backgroundColor: " + ChabuUtils.toJSonStrings(bar.getColors().getCodes())); - result = result.replaceFirst("borderColor: \\[.*\\]", "borderColor: " + ChabuUtils.toJSonStrings(bar.getColors().getLights())); - result = result.replaceFirst("text: '.*'", "text: '" + bar.getTitle().replace("'", "\\\\'") + "'"); + result = result.replace("myChart", "myChart_" + DigestUtils.sha1Hex(bar.getTitle() + "barChart")); + + StringList lines = new StringList(); + lines.append("{\n"); + lines.append(" type: 'bar',\n"); + lines.append(" data: \n"); + lines.append(" {\n"); + lines.append(" labels: ").append(ChabuUtils.toJSonStrings(bar.getLabels())).appendln(","); + lines.append(" datasets: \n"); + lines.append(" [\n"); + for (BarChartDataset dataset : bar.getDatasets()) + { + lines.append(" {\n"); + lines.append(" label: '").append(escape(dataset.getName())).appendln("',"); + lines.append(" data: ").append(ChabuUtils.toJSonNumbers(dataset.getValues())).appendln(","); + lines.append(" backgroundColor: ").append(ChabuUtils.toJSonStrings(dataset.getColors().getCodes())).appendln(","); + lines.append(" borderColor: ").append(ChabuUtils.toJSonStrings(dataset.getColors().getLights())).appendln(","); + lines.append(" borderWidth: ").append(dataset.getBorderWidth()).appendln(","); + lines.append(" }\n"); + lines.append(" ]\n"); + lines.append(" },\n"); + } + lines.append(" options: \n"); + lines.append(" {\n"); + lines.append(" maintainAspectRatio: false,\n"); + lines.append(" display: true,\n"); + lines.append(" responsive: true,\n"); + lines.append(" legend: \n"); + lines.append(" {\n"); + lines.append(" display: false,\n"); + lines.append(" position: 'top',\n"); + lines.append(" },\n"); + lines.append(" title:\n"); + lines.append(" {\n"); + lines.append(" display: true,\n"); + lines.append(" text: '").append(escape(bar.getTitle())).appendln("'"); + lines.append(" },\n"); + lines.append(" scales: \n"); + lines.append(" {\n"); + lines.append(" xAxes:\n"); + lines.append(" [{\n"); + lines.append(" ticks: \n"); + lines.append(" {\n"); + lines.append(" beginAtZero: true\n"); + lines.append(" }\n"); + lines.append(" }],\n"); + lines.append(" yAxes:\n"); + lines.append(" [{\n"); + lines.append(" ticks: \n"); + lines.append(" {\n"); + lines.append(" beginAtZero: true,\n"); + lines.append(" suggestedMax: 10,\n"); + lines.append(" precision: 0\n"); + lines.append(" }\n"); + lines.append(" }]\n"); + lines.append(" }\n"); + lines.append(" }\n"); + lines.append("}"); + + result = result.replaceFirst("\\{[\\s\\S]*\\}", lines.toString()); } catch (IOException exception) { @@ -68,4 +123,28 @@ public class BarChartView // return result; } + + /** + * Escape. + * + * @param input + * the input + * @return the string + */ + public static String escape(final String input) + { + String result; + + if (StringUtils.isBlank(input)) + { + result = ""; + } + else + { + result = input.replace("'", "\\\\'"); + } + + // + return result; + } } diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/ChabuUtils.java b/src/fr/devinsy/statoolinfos/htmlize/charts/ChabuUtils.java index 82b9644..749bb8e 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/ChabuUtils.java +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/ChabuUtils.java @@ -545,6 +545,24 @@ public class ChabuUtils return result; } + /** + * To J son numbers. + * + * @param source + * the source + * @return the string + */ + public static String toJSonNumbers(final double[] values) + { + String result; + + StringList data = new StringList(values); + result = toJSonNumbers(data); + + // + return result; + } + /** * To Json numbers. * @@ -552,11 +570,11 @@ public class ChabuUtils * the source * @return the string */ - public static String toJSonNumbers(final StringList source) + public static String toJSonNumbers(final StringList values) { String result; - result = StringsUtils.toString(source, "[", ",", "]"); + result = StringsUtils.toString(values, "[", ",", "]"); // return result;