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

73 lines
3.1 KiB
Java

/*
* 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
* the statool infos exception
*/
public static String build(final PieChart pie) 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.replaceFirst("type: '.*'", "type: '" + pie.getType().toString().toLowerCase() + "'");
result = result.replace("myChart", "myChart_" + DigestUtils.sha1Hex(pie.getTitle() + "PieChart"));
result = result.replaceFirst("data: \\[.*\\]", "data: " + ChabuUtils.toJSonNumbers(new StringList(pie.getValues())));
result = result.replaceFirst("backgroundColor: \\[.*\\]", "backgroundColor: " + ChabuUtils.toJSonStrings(pie.getColors()));
result = result.replaceFirst("text: '.*'", "text: '" + pie.getTitle().replace("'", "\\\\'") + "'");
result = result.replaceFirst("labels: \\[.*\\]", "labels: " + ChabuUtils.toJSonStrings(pie.getLabels()));
result = result.replaceFirst("legend: .*,", String.format("legend: { display: %b, position: '%s' },", pie.isLegendVisible(), pie.getLegendPositionValue()));
result = result.replaceFirst("title: .*,",
String.format("title: { display: %b, position: '%s', text: '%s' },", pie.isTitleVisible(), pie.getTitlePositionValue(), pie.getTitle().replace("'", "\\\\'")));
}
catch (IOException exception)
{
throw new StatoolInfosException("Error building bar months chart view: " + exception.getMessage(), exception);
}
//
return result;
}
}