Search in sources :

Example 81 with JFreeChart

use of org.jfree.chart.JFreeChart in project dhis2-core by dhis2.

the class ChartResult method execute.

/**
     * Executes the result. Writes the given chart as a PNG to the servlet
     * output stream.
     * 
     * @param invocation an encapsulation of the action execution state.
     * @throws Exception if an error occurs when creating or writing the chart
     *         to the servlet output stream.
     */
@Override
public void execute(ActionInvocation invocation) throws Exception {
    JFreeChart stackChart = (JFreeChart) invocation.getStack().findValue("chart");
    chart = stackChart != null ? stackChart : chart;
    Integer stackHeight = (Integer) invocation.getStack().findValue("height");
    height = stackHeight != null && stackHeight > 0 ? stackHeight : height != null ? height : DEFAULT_HEIGHT;
    Integer stackWidth = (Integer) invocation.getStack().findValue("width");
    width = stackWidth != null && stackWidth > 0 ? stackWidth : width != null ? width : DEFAULT_WIDTH;
    String stackFilename = (String) invocation.getStack().findValue("filename");
    filename = StringUtils.defaultIfEmpty(stackFilename, DEFAULT_FILENAME);
    if (chart == null) {
        log.warn("No chart found");
        return;
    }
    HttpServletResponse response = ServletActionContext.getResponse();
    ContextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_PNG, true, filename, false);
    OutputStream os = response.getOutputStream();
    ChartUtilities.writeChartAsPNG(os, chart, width, height);
    os.flush();
}
Also used : OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) JFreeChart(org.jfree.chart.JFreeChart)

Example 82 with JFreeChart

use of org.jfree.chart.JFreeChart in project processdash by dtuma.

the class TimeLogPhaseWaterfallChart method createChart.

private JFreeChart createChart(IntervalXYDataset dataset, ProcessUtil process, List<String> phases, GapSkipTracker gaps) {
    JFreeChart result = ChartFactory.createXYBarChart(null, null, true, null, dataset, PlotOrientation.VERTICAL, false, true, false);
    XYPlot xyplot = (XYPlot) result.getPlot();
    DateAxis dateAxis = new DateAxis(null);
    dateAxis.setTickMarksVisible(false);
    dateAxis.setTickLabelsVisible(false);
    dateAxis.setLowerMargin(0.01);
    dateAxis.setUpperMargin(0.01);
    setupGaps(gaps, dateAxis, xyplot);
    xyplot.setDomainAxis(dateAxis);
    String[] phaseNameList = new String[phases.size()];
    for (int i = 0; i < phaseNameList.length; i++) phaseNameList[i] = Translator.translate(phases.get(i));
    SymbolAxis symbolaxis = new SymbolAxis(null, phaseNameList);
    symbolaxis.setGridBandsVisible(false);
    xyplot.setRangeAxis(symbolaxis);
    final XYBarRenderer renderer = (XYBarRenderer) xyplot.getRenderer();
    renderer.setUseYInterval(true);
    renderer.setDrawBarOutline(true);
    renderer.setBaseOutlinePaint(Color.black);
    new PhaseChartColorer(process, phases) {

        public void setItemColor(Object key, int pos, Color c) {
            renderer.setSeriesPaint(pos, c);
        }
    }.run();
    int exceptionSeries = dataset.getSeriesCount() - 1;
    renderer.setSeriesPaint(exceptionSeries, EXCEPTION_PAINT);
    renderer.setSeriesFillPaint(exceptionSeries, EXCEPTION_PAINT);
    renderer.setSeriesOutlinePaint(exceptionSeries, EXCEPTION_PAINT);
    renderer.setBaseToolTipGenerator(new TooltipGenerator());
    xyplot.setBackgroundPaint(Color.white);
    xyplot.setDomainGridlinesVisible(false);
    xyplot.setRangeGridlinePaint(Color.lightGray);
    xyplot.setDrawingSupplier(DRAWING_SUPPLIER_FACTORY.newDrawingSupplier());
    result.setAntiAlias(false);
    result.setTextAntiAlias(true);
    parameters.put("title", resources.getString("Title"));
    return result;
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) XYPlot(org.jfree.chart.plot.XYPlot) PhaseChartColorer(net.sourceforge.processdash.ui.lib.chart.PhaseChartColorer) Color(java.awt.Color) JFreeChart(org.jfree.chart.JFreeChart) XYBarRenderer(org.jfree.chart.renderer.xy.XYBarRenderer) SymbolAxis(org.jfree.chart.axis.SymbolAxis)

Example 83 with JFreeChart

use of org.jfree.chart.JFreeChart in project processdash by dtuma.

the class BarChart method createChart.

/** Create a bar chart. */
public JFreeChart createChart() {
    //if (data.numCols() == 1) data = data.transpose();
    JFreeChart chart;
    // default
    boolean vertical = true;
    String direction = getParameter("dir");
    if ((direction != null && direction.toLowerCase().startsWith("hor")) || parameters.get("horizontal") != null)
        vertical = false;
    chart = ChartFactory.createBarChart3D(null, null, null, data.catDataSource(), (vertical ? PlotOrientation.VERTICAL : PlotOrientation.HORIZONTAL), true, true, false);
    setupCategoryChart(chart);
    return chart;
}
Also used : JFreeChart(org.jfree.chart.JFreeChart)

Example 84 with JFreeChart

use of org.jfree.chart.JFreeChart in project processdash by dtuma.

the class DiscChart method createDiscChart.

public static JFreeChart createDiscChart(ResultSet data, Map parameters) {
    // data.sortBy(1, true);
    CategoryDataset catData = data.catDataSource();
    PieDataset pieData = null;
    if (catData.getColumnCount() == 1)
        pieData = DatasetUtilities.createPieDatasetForColumn(catData, 0);
    else
        pieData = DatasetUtilities.createPieDatasetForRow(catData, 0);
    DiscPlot plot = new DiscPlot(pieData);
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    plot.setDrawingSupplier(DRAWING_SUPPLIER_FACTORY.newDrawingSupplier());
    JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    if (parameters.get("skipItemLabels") != null || parameters.get("skipDiscLabels") != null)
        plot.setLabelGenerator(null);
    else if (parameters.get("discLabelFontSize") != null)
        try {
            float fontSize = Float.parseFloat((String) parameters.get("discLabelFontSize"));
            plot.setLabelFont(plot.getLabelFont().deriveFont(fontSize));
        } catch (Exception lfe) {
        }
    if (parameters.get("ellipse") != null)
        ((StandardDiscItemDistributor) plot.getDiscDistributor()).setCircular(false);
    String interiorGap = (String) parameters.get("interiorGap");
    if (interiorGap != null)
        try {
            plot.setInteriorGap(Integer.parseInt(interiorGap) / 100.0);
        } catch (NumberFormatException e) {
        }
    String interiorSpacing = (String) parameters.get("interiorSpacing");
    if (interiorSpacing != null)
        try {
            plot.setInteriorGap(Integer.parseInt(interiorSpacing) / 200.0);
        } catch (NumberFormatException e) {
        }
    return chart;
}
Also used : DiscPlot(net.sourceforge.processdash.ui.lib.chart.DiscPlot) PieDataset(org.jfree.data.general.PieDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) RectangleInsets(org.jfree.ui.RectangleInsets) JFreeChart(org.jfree.chart.JFreeChart)

Example 85 with JFreeChart

use of org.jfree.chart.JFreeChart in project processdash by dtuma.

the class LineChart method createChart.

/** Create a  line chart. */
public JFreeChart createChart() {
    JFreeChart chart = ChartFactory.createLineChart(null, null, null, data.catDataSource(), PlotOrientation.VERTICAL, true, true, false);
    setupCategoryChart(chart);
    return chart;
}
Also used : JFreeChart(org.jfree.chart.JFreeChart)

Aggregations

JFreeChart (org.jfree.chart.JFreeChart)178 XYPlot (org.jfree.chart.plot.XYPlot)40 NumberAxis (org.jfree.chart.axis.NumberAxis)26 Color (java.awt.Color)22 DateAxis (org.jfree.chart.axis.DateAxis)21 DecimalFormat (java.text.DecimalFormat)18 ChartPanel (org.jfree.chart.ChartPanel)17 XYSeries (org.jfree.data.xy.XYSeries)17 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)17 RectangleInsets (org.jfree.ui.RectangleInsets)17 NumberFormat (java.text.NumberFormat)16 CategoryAxis (org.jfree.chart.axis.CategoryAxis)16 CategoryPlot (org.jfree.chart.plot.CategoryPlot)16 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)13 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)13 HashMap (java.util.HashMap)12 ValueAxis (org.jfree.chart.axis.ValueAxis)12 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)11 CategoryDataset (org.jfree.data.category.CategoryDataset)11 Paint (java.awt.Paint)10