Search in sources :

Example 1 with SeriesDef

use of org.opennms.netmgt.config.charts.SeriesDef in project opennms by OpenNMS.

the class ChartUtils method buildCategoryDataSet.

/**
     * @param chartConfig
     * @param baseDataSet
     * @throws SQLException
     */
private static DefaultCategoryDataset buildCategoryDataSet(BarChart chartConfig) throws SQLException {
    DefaultCategoryDataset baseDataSet = new DefaultCategoryDataset();
    /*
         * Configuration can contain more than one series.  This loop adds
         * single series data sets returned from sql query to a base data set
         * to be displayed in a the chart. 
         */
    Connection conn = null;
    try {
        conn = DataSourceFactory.getInstance().getConnection();
        Iterator<SeriesDef> it = chartConfig.getSeriesDefCollection().iterator();
        while (it.hasNext()) {
            SeriesDef def = it.next();
            JDBCCategoryDataset dataSet = new JDBCCategoryDataset(conn, def.getJdbcDataSet().getSql());
            for (int i = 0; i < dataSet.getRowCount(); i++) {
                for (int j = 0; j < dataSet.getColumnCount(); j++) {
                    baseDataSet.addValue(dataSet.getValue(i, j), def.getSeriesName(), dataSet.getColumnKey(j));
                }
            }
        }
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
    return baseDataSet;
}
Also used : SeriesDef(org.opennms.netmgt.config.charts.SeriesDef) Connection(java.sql.Connection) JDBCCategoryDataset(org.jfree.data.jdbc.JDBCCategoryDataset) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) Paint(java.awt.Paint)

Example 2 with SeriesDef

use of org.opennms.netmgt.config.charts.SeriesDef in project opennms by OpenNMS.

the class ChartUtils method customizeSeries.

/**
     * @param barChart TODO
     * @param chartConfig
     */
private static void customizeSeries(JFreeChart barChart, BarChart chartConfig) {
    /*
         * Set the series colors and labels
         */
    CategoryItemLabelGenerator itemLabelGenerator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0"));
    SeriesDef[] seriesDefs = chartConfig.getSeriesDef();
    CustomSeriesColors seriesColors = null;
    if (chartConfig.getSeriesColorClass().isPresent()) {
        try {
            seriesColors = (CustomSeriesColors) Class.forName(chartConfig.getSeriesColorClass().get()).newInstance();
        } catch (InstantiationException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        } catch (IllegalAccessException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        } catch (ClassNotFoundException e) {
            LOG.error("getBarChart: Couldn't instantiate configured CustomSeriesColors class: {}", seriesColors, e);
        }
    }
    for (int i = 0; i < seriesDefs.length; i++) {
        SeriesDef seriesDef = seriesDefs[i];
        Paint paint = Color.BLACK;
        if (seriesColors != null) {
            Comparable<?> cat = (Comparable<?>) ((BarRenderer) barChart.getCategoryPlot().getRenderer()).getPlot().getCategories().get(i);
            paint = seriesColors.getPaint(cat);
        } else {
            Rgb rgb = seriesDef.getRgb().get();
            paint = new Color(rgb.getRed().getRgbColor(), rgb.getGreen().getRgbColor(), rgb.getBlue().getRgbColor());
        }
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesPaint(i, paint);
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelsVisible(i, seriesDef.getUseLabels());
        ((BarRenderer) barChart.getCategoryPlot().getRenderer()).setSeriesItemLabelGenerator(i, itemLabelGenerator);
    }
}
Also used : StandardCategoryItemLabelGenerator(org.jfree.chart.labels.StandardCategoryItemLabelGenerator) DecimalFormat(java.text.DecimalFormat) Color(java.awt.Color) ChartBackgroundColor(org.opennms.netmgt.config.charts.ChartBackgroundColor) PlotBackgroundColor(org.opennms.netmgt.config.charts.PlotBackgroundColor) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StandardCategoryItemLabelGenerator(org.jfree.chart.labels.StandardCategoryItemLabelGenerator) CategoryItemLabelGenerator(org.jfree.chart.labels.CategoryItemLabelGenerator) Paint(java.awt.Paint) Rgb(org.opennms.netmgt.config.charts.Rgb) Paint(java.awt.Paint) SeriesDef(org.opennms.netmgt.config.charts.SeriesDef)

Aggregations

Paint (java.awt.Paint)2 SeriesDef (org.opennms.netmgt.config.charts.SeriesDef)2 Color (java.awt.Color)1 Connection (java.sql.Connection)1 DecimalFormat (java.text.DecimalFormat)1 CategoryItemLabelGenerator (org.jfree.chart.labels.CategoryItemLabelGenerator)1 StandardCategoryItemLabelGenerator (org.jfree.chart.labels.StandardCategoryItemLabelGenerator)1 BarRenderer (org.jfree.chart.renderer.category.BarRenderer)1 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)1 JDBCCategoryDataset (org.jfree.data.jdbc.JDBCCategoryDataset)1 ChartBackgroundColor (org.opennms.netmgt.config.charts.ChartBackgroundColor)1 PlotBackgroundColor (org.opennms.netmgt.config.charts.PlotBackgroundColor)1 Rgb (org.opennms.netmgt.config.charts.Rgb)1