From 4b9de167e0c1a0d0b9b2a686f8ab39d97b96f5f1 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 17 Jan 2021 05:49:28 +0100 Subject: [PATCH] Improved color management for country chart. Back to rgb colors. --- .../statoolinfos/htmlize/Htmlizer.java | 41 +++++++++--- .../htmlize/charts/ChartColor.java | 17 ++--- .../htmlize/charts/ChartColors.java | 18 ++++++ .../stats/country/CountryStats.java | 63 ++++++++++++++++--- 4 files changed, 115 insertions(+), 24 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java index 7bf6f97..37a656c 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java +++ b/src/fr/devinsy/statoolinfos/htmlize/Htmlizer.java @@ -38,6 +38,7 @@ import fr.devinsy.statoolinfos.core.StatoolInfosUtils; import fr.devinsy.statoolinfos.htmlize.charts.BarChart; import fr.devinsy.statoolinfos.htmlize.charts.BarChartView; import fr.devinsy.statoolinfos.htmlize.charts.ChartColor; +import fr.devinsy.statoolinfos.htmlize.charts.ChartColors; import fr.devinsy.statoolinfos.htmlize.charts.DoughnutChartView; import fr.devinsy.statoolinfos.htmlize.charts.PieChart; import fr.devinsy.statoolinfos.htmlize.charts.PieChart.Position; @@ -52,6 +53,7 @@ import fr.devinsy.statoolinfos.stats.services.HostServerTypeStats; import fr.devinsy.statoolinfos.stats.services.RegistrationStats; import fr.devinsy.statoolinfos.stats.services.ServiceInstallTypeStats; import fr.devinsy.statoolinfos.util.URLUtils; +import fr.devinsy.strings.StringList; /** * The Class Htmlizer. @@ -294,14 +296,27 @@ public class Htmlizer CountryStats stats = StatAgent.statsCountry(organizations); PieChart pie = new PieChart("Pays des membres"); + + StringList countries = new StringList(stats.keySet()).sort(); + countries.remove(CountryStats.UNKNOWN_LABEL); + + ChartColors colors = new ChartColors(); + colors.add(ChartColor.GREEN); + colors.add(ChartColor.ORANGE); + colors.add(ChartColor.RED); + colors.add(ChartColor.PURPLE); + colors.add(ChartColor.TURQUOISE); + colors.add(ChartColor.YELLOW); + int index = 0; - int maxIndex = ChartColor.values().length; - for (String country : stats.keySet()) + for (String country : countries) { - pie.add(country, stats.get(country), ChartColor.values()[index % maxIndex]); + pie.add(country, stats.get(country), colors.get(index)); index += 1; } + pie.add("Inconnu", stats.getUnknown(), ChartColor.BLUE); + pie.setLegendPosition(Position.RIGHT); result = PieChartView.build(pie); @@ -375,14 +390,26 @@ public class Htmlizer CountryStats stats = StatAgent.statsCountry(services); PieChart pie = new PieChart("Pays des services"); + StringList countries = new StringList(stats.keySet()).sort(); + countries.remove(CountryStats.UNKNOWN_LABEL); + + ChartColors colors = new ChartColors(); + colors.add(ChartColor.GREEN); + colors.add(ChartColor.ORANGE); + colors.add(ChartColor.RED); + colors.add(ChartColor.PURPLE); + colors.add(ChartColor.TURQUOISE); + colors.add(ChartColor.YELLOW); + int index = 0; - int maxIndex = ChartColor.values().length; - for (String country : stats.keySet()) + for (String country : countries) { - pie.add(country, stats.get(country), ChartColor.values()[index % maxIndex]); + pie.add(country, stats.get(country), colors.get(index)); index += 1; } + pie.add("Inconnu", stats.getUnknown(), ChartColor.BLUE); + pie.setLegendPosition(Position.RIGHT); result = PieChartView.build(pie); @@ -427,7 +454,7 @@ public class Htmlizer * To bar chart. * * @param stats - * the stats + * the statsf * @return the bar chart */ public static BarChart toBarChart(final RegistrationStats stats) diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColor.java b/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColor.java index 1047850..3f68975 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColor.java +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColor.java @@ -24,14 +24,15 @@ package fr.devinsy.statoolinfos.htmlize.charts; public enum ChartColor { // https://html-color-codes.info/ - RED("#ff6384"), - ORANGE("#ff9f40"), - YELLOW("#ffcd56"), - TURQUOISE("#40E0D0"), - GREEN("#4bc0c0"), - BLUE("#36a2eb"), - PURPLE("#9966ff"), - GREY("#c9cbcf"); + + RED("rgb(255, 99, 132)"), + ORANGE("rgb(255, 159, 64)"), + YELLOW("rgb(255, 205, 86)"), + TURQUOISE("rgb(64, 224, 208)"), + GREEN("rgb(75, 192, 192)"), + BLUE("rgb(54, 162, 235)"), + PURPLE("rgb(153, 102, 255)"), + GREY("rgb(201, 203, 207)"); private String code; private String light; diff --git a/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColors.java b/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColors.java index 40572a5..8532529 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColors.java +++ b/src/fr/devinsy/statoolinfos/htmlize/charts/ChartColors.java @@ -37,6 +37,24 @@ public class ChartColors extends ArrayList super(); } + /** + * Gets the ChartColor of the index with safe overflow. + * + * @param index + * the index + * @return the chart color + */ + @Override + public ChartColor get(final int index) + { + ChartColor result; + + result = super.get(index % size()); + + // + return result; + } + /** * Gets the colors. * diff --git a/src/fr/devinsy/statoolinfos/stats/country/CountryStats.java b/src/fr/devinsy/statoolinfos/stats/country/CountryStats.java index c3385d1..0ca9586 100644 --- a/src/fr/devinsy/statoolinfos/stats/country/CountryStats.java +++ b/src/fr/devinsy/statoolinfos/stats/country/CountryStats.java @@ -29,12 +29,41 @@ public class CountryStats extends HashMap { private static final long serialVersionUID = -8177123413128556290L; + public static final String UNKNOWN_LABEL = "Inconnu"; + /** * Instantiates a new country stats. */ public CountryStats() { super(); + put(UNKNOWN_LABEL, 0); + } + + /** + * Gets the. + * + * @param country + * the country + * @return the integer + */ + public int get(final String country) + { + int result; + + Integer count = super.get(country); + + if (count == null) + { + result = 0; + } + else + { + result = count; + } + + // + return result; } /** @@ -56,6 +85,21 @@ public class CountryStats extends HashMap return result; } + /** + * Gets the unknown. + * + * @return the unknown + */ + public int getUnknown() + { + int result; + + result = get(UNKNOWN_LABEL); + + // + return result; + } + /** * Inc. * @@ -66,18 +110,19 @@ public class CountryStats extends HashMap { if (StringUtils.isBlank(country)) { - inc("Inconnu"); + incUnknown(); } else { - Integer currentValue = get(country); - if (currentValue == null) - { - currentValue = 0; - } - - currentValue += 1; - put(country, currentValue); + put(country, get(country) + 1); } } + + /** + * Inc unknown. + */ + public void incUnknown() + { + inc(UNKNOWN_LABEL); + } }