Search in sources :

Example 1 with StandardCategoryItemLabelGenerator

use of org.jfree.chart.labels.StandardCategoryItemLabelGenerator in project ice by Netflix.

the class BasicWeeklyCostEmailService method createImage.

private File createImage(ApplicationGroup appgroup) throws IOException {
    Map<String, Double> costs = Maps.newHashMap();
    DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0);
    Interval interval = new Interval(end.minusWeeks(numWeeks), end);
    for (Product product : products) {
        List<ResourceGroup> resourceGroups = getResourceGroups(appgroup, product);
        if (resourceGroups.size() == 0) {
            continue;
        }
        DataManager dataManager = config.managers.getCostManager(product, ConsolidateType.weekly);
        if (dataManager == null) {
            continue;
        }
        TagLists tagLists = new TagLists(accounts, regions, null, Lists.newArrayList(product), null, null, resourceGroups);
        Map<Tag, double[]> data = dataManager.getData(interval, tagLists, TagType.Product, AggregateType.none, false);
        for (Tag tag : data.keySet()) {
            for (int week = 0; week < numWeeks; week++) {
                String key = tag + "|" + week;
                if (costs.containsKey(key))
                    costs.put(key, data.get(tag)[week] + costs.get(key));
                else
                    costs.put(key, data.get(tag)[week]);
            }
        }
    }
    boolean hasData = false;
    for (Map.Entry<String, Double> entry : costs.entrySet()) {
        if (!entry.getKey().contains("monitor") && entry.getValue() != null && entry.getValue() >= 0.1) {
            hasData = true;
            break;
        }
    }
    if (!hasData)
        return null;
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (Product product : products) {
        for (int week = 0; week < numWeeks; week++) {
            String weekStr = String.format("%s - %s week", formatter.print(end.minusWeeks(numWeeks - week)).substring(5), formatter.print(end.minusWeeks(numWeeks - week - 1)).substring(5));
            dataset.addValue(costs.get(product + "|" + week), product.name, weekStr);
        }
    }
    JFreeChart chart = ChartFactory.createBarChart3D(appgroup.getDisplayName() + " Weekly AWS Costs", "", "Costs", dataset, PlotOrientation.VERTICAL, true, false, false);
    CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    BarRenderer3D renderer = (BarRenderer3D) categoryplot.getRenderer();
    renderer.setItemLabelAnchorOffset(10.0);
    TextTitle title = chart.getTitle();
    title.setFont(title.getFont().deriveFont((title.getFont().getSize() - 3)));
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator() {

        public java.lang.String generateLabel(org.jfree.data.category.CategoryDataset dataset, int row, int column) {
            return costFormatter.format(dataset.getValue(row, column));
        }
    });
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setNumberFormatOverride(costFormatter);
    BufferedImage image = chart.createBufferedImage(1200, 400);
    File outputfile = File.createTempFile("awscost", "png");
    ImageIO.write(image, "png", outputfile);
    return outputfile;
}
Also used : StandardCategoryItemLabelGenerator(org.jfree.chart.labels.StandardCategoryItemLabelGenerator) NumberAxis(org.jfree.chart.axis.NumberAxis) DateTime(org.joda.time.DateTime) BufferedImage(java.awt.image.BufferedImage) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition) JFreeChart(org.jfree.chart.JFreeChart) CategoryPlot(org.jfree.chart.plot.CategoryPlot) TextTitle(org.jfree.chart.title.TextTitle) BarRenderer3D(org.jfree.chart.renderer.category.BarRenderer3D) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) File(java.io.File) Interval(org.joda.time.Interval)

Example 2 with StandardCategoryItemLabelGenerator

use of org.jfree.chart.labels.StandardCategoryItemLabelGenerator 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

StandardCategoryItemLabelGenerator (org.jfree.chart.labels.StandardCategoryItemLabelGenerator)2 Color (java.awt.Color)1 Paint (java.awt.Paint)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 DecimalFormat (java.text.DecimalFormat)1 JFreeChart (org.jfree.chart.JFreeChart)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 CategoryItemLabelGenerator (org.jfree.chart.labels.CategoryItemLabelGenerator)1 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)1 CategoryPlot (org.jfree.chart.plot.CategoryPlot)1 BarRenderer (org.jfree.chart.renderer.category.BarRenderer)1 BarRenderer3D (org.jfree.chart.renderer.category.BarRenderer3D)1 TextTitle (org.jfree.chart.title.TextTitle)1 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)1 DateTime (org.joda.time.DateTime)1 Interval (org.joda.time.Interval)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