Improved color management for country chart. Back to rgb colors.

This commit is contained in:
Christian P. MOMON 2021-01-17 05:49:28 +01:00
parent 979ebac18d
commit 4b9de167e0
4 changed files with 115 additions and 24 deletions

View file

@ -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)

View file

@ -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;

View file

@ -37,6 +37,24 @@ public class ChartColors extends ArrayList<ChartColor>
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.
*

View file

@ -29,12 +29,41 @@ public class CountryStats extends HashMap<String, Integer>
{
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<String, Integer>
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<String, Integer>
{
if (StringUtils.isBlank(country))
{
inc("Inconnu");
incUnknown();
}
else
{
Integer currentValue = get(country);
if (currentValue == null)
{
currentValue = 0;
put(country, get(country) + 1);
}
}
currentValue += 1;
put(country, currentValue);
}
/**
* Inc unknown.
*/
public void incUnknown()
{
inc(UNKNOWN_LABEL);
}
}