2020-11-17 05:44:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package fr.devinsy.statoolinfos.htmlize.charts;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import fr.devinsy.statoolinfos.core.StatoolInfosException;
|
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
import fr.devinsy.xidyn.utils.XidynUtils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class PieChartView.
|
|
|
|
*/
|
|
|
|
public class PieChartView
|
|
|
|
{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(PieChartView.class);
|
|
|
|
|
2020-11-17 06:15:21 +01:00
|
|
|
enum PieType
|
|
|
|
{
|
|
|
|
DOUGHNUT,
|
|
|
|
PIE
|
|
|
|
}
|
|
|
|
|
2020-11-17 05:44:31 +01:00
|
|
|
/**
|
|
|
|
* Builds the.
|
|
|
|
*
|
|
|
|
* @param pie
|
|
|
|
* the pie
|
|
|
|
* @return the string
|
|
|
|
* @throws StatoolInfosException
|
2020-11-17 06:15:21 +01:00
|
|
|
* the statool infos exception
|
2020-11-17 05:44:31 +01:00
|
|
|
*/
|
|
|
|
public static String build(final PieChart pie) throws StatoolInfosException
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
2020-11-17 06:15:21 +01:00
|
|
|
result = build(PieType.PIE, pie);
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the.
|
|
|
|
*
|
|
|
|
* @param pie
|
|
|
|
* the pie
|
|
|
|
* @return the string
|
|
|
|
* @throws StatoolInfosException
|
|
|
|
*/
|
|
|
|
public static String build(final PieType type, final PieChart pie) throws StatoolInfosException
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
|
|
|
|
result = build(type, pie.getTitle(), pie.getLabels(), new StringList(pie.getValues()), pie.getColors());
|
2020-11-17 05:44:31 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the.
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* the title
|
|
|
|
* @param description
|
|
|
|
* the description
|
|
|
|
* @param labels
|
|
|
|
* the labels
|
|
|
|
* @param values
|
|
|
|
* the values
|
|
|
|
* @param color
|
|
|
|
* the color
|
|
|
|
* @return the string
|
|
|
|
* @throws StatoolInfosException
|
|
|
|
* the statool infos exception
|
|
|
|
*/
|
2020-11-17 06:15:21 +01:00
|
|
|
public static String build(final PieType type, final String title, final StringList labels, final StringList values, final StringList colors) throws StatoolInfosException
|
2020-11-17 05:44:31 +01:00
|
|
|
{
|
|
|
|
String result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String source = XidynUtils.load(PieChartView.class.getResource("/fr/devinsy/statoolinfos/htmlize/charts/pieChartView.xhtml"));
|
|
|
|
result = XidynUtils.extractBodyContent(source);
|
|
|
|
|
2020-11-17 06:15:21 +01:00
|
|
|
result = result.replaceFirst("type: '.*'", "type: '" + type.toString().toLowerCase() + "'");
|
2020-11-17 05:44:31 +01:00
|
|
|
result = result.replace("myChart", "myChart_" + DigestUtils.sha1Hex(title + "PieChart"));
|
|
|
|
result = result.replaceFirst("data: \\[.*\\]", "data: " + ChabuUtils.toJSonNumbers(values));
|
|
|
|
result = result.replaceFirst("backgroundColor: \\[.*\\]", "backgroundColor: " + ChabuUtils.toJSonStrings(colors));
|
|
|
|
result = result.replaceFirst("text: '.*'", "text: '" + title.replace("'", "\\\\'") + "'");
|
|
|
|
result = result.replaceFirst("labels: \\[.*\\]", "labels: " + ChabuUtils.toJSonStrings(labels));
|
|
|
|
}
|
|
|
|
catch (IOException exception)
|
|
|
|
{
|
|
|
|
throw new StatoolInfosException("Error building bar months chart view: " + exception.getMessage(), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|