Search in sources :

Example 86 with JFreeChart

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

the class PieChart method createChart.

/** Create a  line chart. */
@Override
public JFreeChart createChart() {
    CategoryDataset catData = data.catDataSource();
    PieDataset pieData = null;
    if (catData.getColumnCount() == 1)
        pieData = DatasetUtilities.createPieDatasetForColumn(catData, 0);
    else
        pieData = DatasetUtilities.createPieDatasetForRow(catData, 0);
    JFreeChart chart = null;
    if (get3DSetting()) {
        chart = ChartFactory.createPieChart3D(null, pieData, true, true, false);
        chart.getPlot().setForegroundAlpha(ALPHA);
    } else {
        chart = ChartFactory.createPieChart(null, pieData, true, true, false);
    }
    PiePlot plot = (PiePlot) chart.getPlot();
    if (parameters.get("skipItemLabels") != null || parameters.get("skipWedgeLabels") != null)
        plot.setLabelGenerator(null);
    else if (parameters.get("wedgeLabelFontSize") != null)
        try {
            float fontSize = Float.parseFloat((String) parameters.get("wedgeLabelFontSize"));
            plot.setLabelFont(plot.getLabelFont().deriveFont(fontSize));
        } catch (Exception lfe) {
        }
    if (parameters.get("ellipse") != null)
        plot.setCircular(true);
    else
        plot.setCircular(false);
    Object colorScheme = parameters.get("colorScheme");
    if ("byPhase".equals(colorScheme))
        maybeConfigurePhaseColors(plot, pieData);
    else if ("consistent".equals(colorScheme))
        // since 2.0.9
        configureConsistentColors(plot, pieData);
    else if (parameters.containsKey("c1"))
        configureIndividualColors(plot, pieData);
    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) {
        }
    if (!parameters.containsKey("showZeroValues")) {
        plot.setIgnoreZeroValues(true);
        plot.setIgnoreNullValues(true);
    }
    return chart;
}
Also used : PieDataset(org.jfree.data.general.PieDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) PiePlot(org.jfree.chart.plot.PiePlot) JFreeChart(org.jfree.chart.JFreeChart)

Example 87 with JFreeChart

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

the class CGIChartBase method writeContents.

/** Generate CGI chart output. */
@Override
protected void writeContents() throws IOException {
    // get the data for display
    buildData();
    chromeless = (parameters.get("chromeless") != null);
    JFreeChart chart = createChart();
    int width = getIntSetting("width");
    int height = getIntSetting("height");
    Color initGradColor = getColorSetting("initGradColor");
    Color finalGradColor = getColorSetting("finalGradColor");
    chart.setBackgroundPaint(new GradientPaint(0, 0, initGradColor, width, height, finalGradColor));
    if (parameters.get("hideOutline") != null)
        chart.getPlot().setOutlinePaint(INVISIBLE);
    String title = getSetting("title");
    if (chromeless || title == null || title.length() == 0)
        chart.setTitle((TextTitle) null);
    else {
        chart.setTitle(Translator.translate(title));
        String titleFontSize = getSetting("titleFontSize");
        if (titleFontSize != null)
            try {
                float fontSize = Float.parseFloat(titleFontSize);
                TextTitle t = chart.getTitle();
                t.setFont(t.getFont().deriveFont(fontSize));
            } catch (Exception tfe) {
            }
    }
    if (chromeless || parameters.get("hideLegend") != null)
        chart.removeLegend();
    else {
        LegendTitle l = chart.getLegend();
        String legendFontSize = getSetting("legendFontSize");
        if (l != null && legendFontSize != null)
            try {
                float fontSize = Float.parseFloat(legendFontSize);
                l.setItemFont(l.getItemFont().deriveFont(fontSize));
            } catch (Exception lfe) {
            }
    }
    chart.getPlot().setNoDataMessage(resources.getString("No_Data_Message"));
    Axis xAxis = getHorizontalAxis(chart);
    if (xAxis != null) {
        if (parameters.get("hideTickLabels") != null || parameters.get("hideXTickLabels") != null) {
            xAxis.setTickLabelsVisible(false);
        } else if (parameters.get("tickLabelFontSize") != null || parameters.get("xTickLabelFontSize") != null) {
            String tfs = getParameter("xTickLabelFontSize");
            if (tfs == null)
                tfs = getParameter("tickLabelFontSize");
            float fontSize = Float.parseFloat(tfs);
            xAxis.setTickLabelFont(xAxis.getTickLabelFont().deriveFont(fontSize));
        }
    }
    Axis yAxis = getVerticalAxis(chart);
    if (yAxis != null) {
        if (parameters.get("hideTickLabels") != null || parameters.get("hideYTickLabels") != null) {
            yAxis.setTickLabelsVisible(false);
        } else if (parameters.get("tickLabelFontSize") != null || parameters.get("yTickLabelFontSize") != null) {
            String tfs = getParameter("yTickLabelFontSize");
            if (tfs == null)
                tfs = getParameter("tickLabelFontSize");
            float fontSize = Float.parseFloat(tfs);
            yAxis.setTickLabelFont(yAxis.getTickLabelFont().deriveFont(fontSize));
        }
    }
    String axisFontSize = getSetting("axisLabelFontSize");
    if (axisFontSize != null)
        try {
            float fontSize = Float.parseFloat(axisFontSize);
            if (xAxis != null)
                xAxis.setLabelFont(xAxis.getLabelFont().deriveFont(fontSize));
            if (yAxis != null)
                yAxis.setLabelFont(yAxis.getLabelFont().deriveFont(fontSize));
        } catch (Exception afs) {
        }
    ChartRenderingInfo info = (isHtmlMode() ? new ChartRenderingInfo() : null);
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();
    if ("auto".equals(getSetting("titleFontSize")))
        maybeAdjustTitleFontSize(chart, g2, width);
    chart.draw(g2, new Rectangle2D.Double(0, 0, width, height), info);
    g2.dispose();
    String outputFormat = getSetting("outputFormat");
    OutputStream imgOut;
    if (isHtmlMode()) {
        imgOut = PngCache.getOutputStream();
    } else {
        imgOut = outStream;
    }
    ImageIO.write(img, outputFormat, imgOut);
    imgOut.flush();
    imgOut.close();
    if (isHtmlMode())
        writeImageHtml(width, height, imgOut.hashCode(), info);
}
Also used : Color(java.awt.Color) OutputStream(java.io.OutputStream) Rectangle2D(java.awt.geom.Rectangle2D) GradientPaint(java.awt.GradientPaint) LegendTitle(org.jfree.chart.title.LegendTitle) JFreeChart(org.jfree.chart.JFreeChart) GradientPaint(java.awt.GradientPaint) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) TextTitle(org.jfree.chart.title.TextTitle) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) Axis(org.jfree.chart.axis.Axis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) ValueAxis(org.jfree.chart.axis.ValueAxis)

Example 88 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getChartAsBufferedImage.

/**
 * Helper method used to return a JFreeChart as a buffered Image.
 *
 * @param chartName a {@link java.lang.String} object.
 * @return a <code>BufferedImage</code>
 * @throws java.io.IOException if any.
 * @throws java.sql.SQLException if any.
 */
public static BufferedImage getChartAsBufferedImage(String chartName) throws IOException, SQLException {
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;
    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }
    return chart.createBufferedImage(hzPixels, vtPixels);
}
Also used : ImageSize(org.opennms.netmgt.config.charts.ImageSize) BarChart(org.opennms.netmgt.config.charts.BarChart) JFreeChart(org.jfree.chart.JFreeChart) Paint(java.awt.Paint)

Example 89 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getBarChart.

/**
 * This method will returns a JFreeChart bar chart constructed based on XML configuration.
 *
 * @param chartName Name specified in chart-configuration.xml
 * @return <code>JFreeChart</code> constructed from the chartName
 * @throws java.io.IOException if any.
 * @throws java.sql.SQLException if any.
 */
public static JFreeChart getBarChart(String chartName) throws IOException, SQLException {
    // ChartConfigFactory.reload();
    BarChart chartConfig = null;
    chartConfig = getBarChartConfigByName(chartName);
    if (chartConfig == null) {
        throw new IllegalArgumentException("getBarChart: Invalid chart name.");
    }
    DefaultCategoryDataset baseDataSet = buildCategoryDataSet(chartConfig);
    JFreeChart barChart = createBarChart(chartConfig, baseDataSet);
    addSubTitles(chartConfig, barChart);
    if (chartConfig.getSubLabelClass().isPresent()) {
        addSubLabels(barChart, chartConfig.getSubLabelClass().get());
    }
    customizeSeries(barChart, chartConfig);
    return barChart;
}
Also used : BarChart(org.opennms.netmgt.config.charts.BarChart) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) JFreeChart(org.jfree.chart.JFreeChart)

Example 90 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getBarChartAsPNGByteArray.

/**
 * Helper method that returns the JFreeChart as a PNG byte array.
 *
 * @param chartName a {@link java.lang.String} object.
 * @return a byte array
 * @throws java.io.IOException if any.
 * @throws java.sql.SQLException if any.
 */
public static byte[] getBarChartAsPNGByteArray(String chartName) throws IOException, SQLException {
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;
    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }
    return ChartUtilities.encodeAsPNG(chart.createBufferedImage(hzPixels, vtPixels));
}
Also used : ImageSize(org.opennms.netmgt.config.charts.ImageSize) BarChart(org.opennms.netmgt.config.charts.BarChart) JFreeChart(org.jfree.chart.JFreeChart) Paint(java.awt.Paint)

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