use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.
the class AreaPlotFactory method create.
@Override
public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
Class rendererClass = params.getClass(ChartParams.RENDERER);
if (rendererClass == null || XYItemRenderer.class.isAssignableFrom(rendererClass)) {
XYItemRenderer renderer;
if (rendererClass != null) {
try {
Constructor ctor = rendererClass.getConstructor(new Class[] {});
renderer = (XYItemRenderer) ctor.newInstance(new Object[] {});
} catch (Throwable e) {
throw new GenerateException(e);
}
} else {
renderer = new XYAreaRenderer();
}
ChartCustomizer.customizeXYItemRenderer(renderer, params);
return XYPlotFactory.getInstance().create(dataSource, renderer, params);
} else if (CategoryItemRenderer.class.isAssignableFrom(rendererClass)) {
CategoryItemRenderer renderer;
try {
Constructor ctor = rendererClass.getConstructor(new Class[] {});
renderer = (CategoryItemRenderer) ctor.newInstance(new Object[] {});
} catch (Throwable e) {
throw new GenerateException(e);
}
ChartCustomizer.customizeCategoryItemRenderer(renderer, params);
return CategoryPlotFactory.getInstance().create(dataSource, renderer, params);
} else {
throw new GenerateException("Incompatible renderer class: " + rendererClass);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.
the class ChartingPlugin method generateSvgChart.
private Chart generateSvgChart(JFreeChart jfchart, ChartParams params, XWikiContext context) throws IOException, GenerateException {
// Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Ask the chart to render into the SVG Graphics2D implementation
Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, params.getInteger(ChartParams.WIDTH).intValue(), params.getInteger(ChartParams.HEIGHT).intValue());
jfchart.draw(svgGenerator, rect);
boolean useCSS = false;
StringWriter swriter = new StringWriter();
svgGenerator.stream(swriter, useCSS);
String svgText = swriter.toString();
String pageURL = null;
SVGPlugin svgPlugin = (SVGPlugin) context.getWiki().getPlugin("svg", context);
if (svgPlugin == null) {
throw new GenerateException("SVGPlugin not loaded");
}
String imageURL;
try {
imageURL = svgPlugin.getSVGImageURL(svgText, params.getInteger(ChartParams.HEIGHT).intValue(), params.getInteger(ChartParams.WIDTH).intValue(), context);
} catch (SVGConverterException sce) {
throw new GenerateException(sce);
}
return new ChartImpl(params, imageURL, pageURL);
}
use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.
the class ChartingPlugin method generateChart.
public Chart generateChart(ChartParams params, XWikiContext context) throws GenerateException {
try {
// Obtain the corresponding data source and wrap it into a data source object
DataSource dataSource = MainDataSourceFactory.getInstance().create(params.getMap(ChartParams.SOURCE), context);
String type = params.getString(ChartParams.TYPE);
Plot plot;
try {
String factoryClassName = ChartingPlugin.class.getPackage().getName() + ".plots." + Character.toUpperCase(type.charAt(0)) + type.toLowerCase().substring(1) + "PlotFactory";
Class factoryClass = Class.forName(factoryClassName);
Method method = factoryClass.getMethod("getInstance", new Class[] {});
PlotFactory factory = (PlotFactory) method.invoke(null, new Object[] {});
plot = factory.create(dataSource, params);
} catch (InvocationTargetException e) {
throw new GenerateException(e.getTargetException());
} catch (Throwable e) {
throw new GenerateException(e);
}
ChartCustomizer.customizePlot(plot, params);
JFreeChart jfchart = new JFreeChart(plot);
ChartCustomizer.customizeChart(jfchart, params);
return generatePngChart(jfchart, params, context);
} catch (IOException ioe) {
throw new GenerateException(ioe);
} catch (DataSourceException dse) {
throw new GenerateException(dse);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.
the class TableXYDatasetFactory method create.
public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
// String type = params.getStringParam("type");
String dataSeries = params.getString(ChartParams.SERIES);
DefaultTableXYDataset dataset = new DefaultTableXYDataset();
if (dataSeries.equals("columns")) {
if (!dataSource.hasHeaderColumn()) {
throw new GenerateException("Header column required");
}
for (int column = 0; column < dataSource.getColumnCount(); column++) {
XYSeries series = new XYSeries(dataSource.hasHeaderRow() ? dataSource.getHeaderRowValue(column) : ("Series " + (column + 1)), false, false);
for (int row = 0; row < dataSource.getRowCount(); row++) {
series.add(Double.parseDouble(dataSource.getHeaderColumnValue(row)), dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else if (dataSeries.equals("rows")) {
if (!dataSource.hasHeaderRow()) {
throw new GenerateException("Header row required");
}
for (int row = 0; row < dataSource.getRowCount(); row++) {
XYSeries series = new XYSeries(dataSource.hasHeaderColumn() ? dataSource.getHeaderColumnValue(row) : ("Series " + (row + 1)), false, false);
for (int column = 0; column < dataSource.getColumnCount(); column++) {
series.add(Double.parseDouble(dataSource.getHeaderRowValue(column)), dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else {
throw new GenerateException("Invalid series parameter:" + dataSeries);
}
return dataset;
}
use of com.xpn.xwiki.plugin.charts.exceptions.GenerateException in project xwiki-platform by xwiki.
the class TimeSeriesCollectionFactory method create.
public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
String dataSeries = params.getString(ChartParams.SERIES);
TimeSeriesCollection dataset = new TimeSeriesCollection();
Class timePeriodClass = params.getClass(ChartParams.TIME_PERIOD_CLASS);
if (timePeriodClass == null) {
timePeriodClass = Day.class;
}
DateFormat format = params.getDateFormat(ChartParams.DATE_FORMAT);
if (format == null) {
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}
if (dataSeries.equals("columns")) {
if (!dataSource.hasHeaderColumn()) {
throw new GenerateException("Header column required");
}
for (int column = 0; column < dataSource.getColumnCount(); column++) {
String seriesName;
if (dataSource.hasHeaderRow()) {
seriesName = dataSource.getHeaderRowValue(column);
} else {
seriesName = "Series " + (column + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int row = 0; row < dataSource.getRowCount(); row++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderColumnValue(row));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else if (dataSeries.equals("rows")) {
if (!dataSource.hasHeaderRow()) {
throw new GenerateException("Header row required");
}
for (int row = 0; row < dataSource.getRowCount(); row++) {
String seriesName;
if (dataSource.hasHeaderColumn()) {
seriesName = dataSource.getHeaderColumnValue(row);
} else {
seriesName = "Series " + (row + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int column = 0; column < dataSource.getColumnCount(); column++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderRowValue(column));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else {
throw new GenerateException("Invalid series parameter:" + dataSeries);
}
return dataset;
}
Aggregations