statoolinfosweb/src/fr/devinsy/statoolinfos/htmlize/charts/PieChartView.java

96 lines
3.2 KiB
Java
Raw Normal View History

/*
* 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);
/**
* Builds the.
*
* @param pie
* the pie
* @return the string
* @throws StatoolInfosException
*/
public static String build(final PieChart pie) throws StatoolInfosException
{
String result;
result = build(pie.getTitle(), pie.getLabels(), new StringList(pie.getValues()), pie.getColors());
//
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
*/
public static String build(final String title, final StringList labels, final StringList values, final StringList colors) throws StatoolInfosException
{
String result;
try
{
String source = XidynUtils.load(PieChartView.class.getResource("/fr/devinsy/statoolinfos/htmlize/charts/pieChartView.xhtml"));
result = XidynUtils.extractBodyContent(source);
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;
}
}