Example usage for org.jfree.chart.plot PiePlot getDataset

List of usage examples for org.jfree.chart.plot PiePlot getDataset

Introduction

In this page you can find the example usage for org.jfree.chart.plot PiePlot getDataset.

Prototype

public PieDataset getDataset() 

Source Link

Document

Returns the dataset.

Usage

From source file:org.gaixie.micrite.jfreechart.style.PieStyle.java

/**
 * ?1 ,? Familiar(30%) //from  w w  w  .  j a  v a2 s .  c om
 * @param chart
 */
public static void styleOne(JFreeChart chart) {
    PiePlot plot = (PiePlot) chart.getPlot();
    PieDataset pd = plot.getDataset();
    PieStyle.setBackground(chart);
    if (pd != null) {
        //?
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
        //??
        for (int i = 0; i < pd.getItemCount(); i++) {
            int color = i % colors.length;
            plot.setSectionPaint(pd.getKey(i), colors[color]);
        }
    } else {
        plot.setNoDataMessage("NO DATA");
    }

}

From source file:canreg.client.analysis.Tools.java

public static String getChartData(JFreeChart chart, String separatingCharacter, boolean quotesOn) {

    String endLine = "\n";
    String quotes = "";
    if (quotesOn) {
        quotes = "\"";
    }//from   w w w  . j av a  2  s  .  c  o  m

    StringBuilder stringBuilder = new StringBuilder();
    String plotType = chart.getPlot().getPlotType();
    // System.out.println("Plot Type: " + plotType);

    if (plotType.equalsIgnoreCase("Pie Plot")) {
        PiePlot plot = (PiePlot) chart.getPlot();
        PieDataset dataset = plot.getDataset();

        Comparable key;

        for (int i = 0; i < dataset.getKeys().size(); i++) {
            key = dataset.getKey(i);
            stringBuilder.append(quotes).append(key).append(quotes).append(separatingCharacter).append(quotes)
                    .append(dataset.getValue(key)).append(quotes).append(endLine);
        }
    } else if (plotType.equalsIgnoreCase("Category Plot")) {
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        CategoryDataset dataset = plot.getDataset();

        Comparable rowkey;
        Comparable columnkey;

        for (int r = 0; r < dataset.getRowCount(); r++) {
            rowkey = dataset.getRowKey(r);
            columnkey = dataset.getColumnKey(r); // this is weird... but it works!
            stringBuilder.append(quotes).append(columnkey).append(quotes).append(separatingCharacter)
                    .append(quotes).append(dataset.getValue(rowkey, columnkey)).append(quotes).append(endLine);
        }
    }
    return stringBuilder.toString();
}

From source file:canreg.client.analysis.Tools.java

public static void setPiePlotColours(JFreeChart chart, int numberOfSections, Color baseColor) {
    Color color = baseColor;//  ww  w.j a va  2  s .c o  m
    PiePlot plot = (PiePlot) chart.getPlot();
    for (int i = 0; i < numberOfSections; i++) {
        try {
            plot.setSectionOutlinePaint(plot.getDataset().getKey(i), baseColor.darker().darker().darker());
            color = darken(color);
            plot.setSectionPaint(plot.getDataset().getKey(i), color);
        } catch (java.lang.IndexOutOfBoundsException ex) {
            // not data for all the categories - that is fine
            Logger.getLogger(TopNChartTableBuilder.class.getName()).log(Level.INFO, null, ex);
        }
    }
}

From source file:net.sf.dynamicreports.design.transformation.chartcustomizer.SeriesColorsByNameCustomizer.java

@Override
public void customize(JFreeChart chart, ReportParameters reportParameters) {
    if (chart.getPlot() instanceof CategoryPlot) {
        CategoryItemRenderer renderer = chart.getCategoryPlot().getRenderer();
        CategoryDataset dataset = chart.getCategoryPlot().getDataset();
        Set<String> legend = new LinkedHashSet<String>();
        if (dataset != null) {
            for (int i = 0; i < dataset.getRowCount(); i++) {
                String key = (String) dataset.getRowKey(i);
                if (renderer instanceof GroupedStackedBarRenderer) {
                    key = StringUtils.substringAfter(key, GroupedStackedBarRendererCustomizer.GROUP_SERIES_KEY);
                    legend.add(key);//from   ww  w  .j  a  v a 2 s.c  o  m
                }
                renderer.setSeriesPaint(i, seriesColorsByName.get(key));
            }
        }
        if (!legend.isEmpty()) {
            LegendItemCollection legendItems = new LegendItemCollection();
            for (String item : legend) {
                legendItems.add(new LegendItem(item, seriesColorsByName.get(item)));
            }
            chart.getCategoryPlot().setFixedLegendItems(legendItems);
        }
    } else if (chart.getPlot() instanceof PiePlot) {
        PiePlot plot = (PiePlot) chart.getPlot();
        PieDataset dataset = plot.getDataset();
        for (int i = 0; i < dataset.getItemCount(); i++) {
            String key = (String) dataset.getKey(i);
            plot.setSectionPaint(key, seriesColorsByName.get(key));
        }
    } else if (chart.getPlot() instanceof XYPlot) {
        XYItemRenderer renderer = chart.getXYPlot().getRenderer();
        XYDataset dataset = chart.getXYPlot().getDataset();
        for (int i = 0; i < dataset.getSeriesCount(); i++) {
            String key = (String) dataset.getSeriesKey(i);
            renderer.setSeriesPaint(i, seriesColorsByName.get(key));
        }
    }
}

From source file:lu.lippmann.cdb.common.gui.DragAndDroppablePieChartPanel.java

/**
 * {@inheritDoc}//from ww  w. j  av  a2  s. c  o  m
 */
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    if (!released && source != null) {
        final PiePlot plot = ((PiePlot) getChart().getPlot());
        final Comparable<?> key = plot.getDataset().getKey(source.getSectionIndex());
        final Color color = (Color) plot.getSectionPaint(key);
        g2d.translate(tx, ty);
        g2d.setColor(color);
        g2d.fill(source.getArea());
    }
}

From source file:net.sf.dynamicreports.test.jasper.chart.ChartSeriesColorsByNameTest.java

@Override
public void test() {
    super.test();

    numberOfPagesTest(1);/*ww w  . j a  v a  2s  .  co  m*/

    chartCountTest("summary.chart1", 1);
    JFreeChart chart = getChart("summary.chart1", 0);
    CategoryItemRenderer renderer1 = chart.getCategoryPlot().getRenderer();
    CategoryDataset dataset1 = chart.getCategoryPlot().getDataset();
    for (int i = 0; i < dataset1.getRowCount(); i++) {
        String key = (String) dataset1.getRowKey(i);
        Assert.assertNotNull("null series color", colors.get(key));
        Assert.assertEquals("series color", colors.get(key), renderer1.getSeriesPaint(i));
    }

    chartCountTest("summary.chart2", 1);
    chart = getChart("summary.chart2", 0);
    CategoryItemRenderer renderer2 = chart.getCategoryPlot().getRenderer();
    CategoryDataset dataset2 = chart.getCategoryPlot().getDataset();
    for (int i = 0; i < dataset2.getRowCount(); i++) {
        String key = (String) dataset2.getRowKey(i);
        key = StringUtils.substringAfter(key, GroupedStackedBarRendererCustomizer.GROUP_SERIES_KEY);
        Assert.assertNotNull("null series color", colors.get(key));
        Assert.assertEquals("series color", colors.get(key), renderer2.getSeriesPaint(i));
    }
    for (int i = 0; i < chart.getCategoryPlot().getFixedLegendItems().getItemCount(); i++) {
        LegendItem legendItem = chart.getCategoryPlot().getFixedLegendItems().get(i);
        Assert.assertNotNull("null series color", colors.get(legendItem.getLabel()));
        Assert.assertEquals("series color", colors.get(legendItem.getLabel()), legendItem.getFillPaint());
    }

    chartCountTest("summary.chart3", 1);
    chart = getChart("summary.chart3", 0);
    PiePlot plot3 = (PiePlot) chart.getPlot();
    PieDataset dataset3 = plot3.getDataset();
    for (int i = 0; i < dataset3.getItemCount(); i++) {
        String key = (String) dataset3.getKey(i);
        Assert.assertNotNull("null series color", colors.get(key));
        Assert.assertEquals("series color", colors.get(key), plot3.getSectionPaint(key));
    }

    chartCountTest("summary.chart4", 1);
    chart = getChart("summary.chart4", 0);
    XYItemRenderer renderer4 = chart.getXYPlot().getRenderer();
    XYDataset dataset4 = chart.getXYPlot().getDataset();
    for (int i = 0; i < dataset4.getSeriesCount(); i++) {
        String key = (String) dataset4.getSeriesKey(i);
        Assert.assertNotNull("null series color", colors.get(key));
        Assert.assertEquals("series color", colors.get(key), renderer4.getSeriesPaint(i));
    }
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.PieChartExpression.java

protected void configureExplode(final PiePlot pp) {
    final PieDataset pieDS = pp.getDataset();

    final int explodeType = computeExplodeType();
    if (explodeType == EXPLODE_VALUE) {
        try {/*from  w  ww  . j  a v a2  s .  c o m*/
            final int actualSegment = Integer.parseInt(explodeSegment);
            if (actualSegment >= 0) {
                pp.setExplodePercent(pieDS.getKey(actualSegment), explodePct.doubleValue());
            }
        } catch (Exception ignored) {
        }
        return;
    }

    // Calculate min and max...
    if (pieDS != null) {
        final int itemCount = pieDS.getItemCount();
        Number maxNum = new Double(Integer.MIN_VALUE);
        Number minNum = new Double(Integer.MAX_VALUE);
        int maxSegment = -1;
        int minSegment = -1;
        for (int i = 0; i < itemCount; i++) {
            final Number nbr = pieDS.getValue(i);
            if (nbr.doubleValue() > maxNum.doubleValue()) {
                maxNum = nbr;
                maxSegment = i;
            }
            if (nbr.doubleValue() < minNum.doubleValue()) {
                minNum = nbr;
                minSegment = i;
            }
        }

        if (explodeType == EXPLODE_MIN) { //$NON-NLS-1$
            if (minSegment >= 0) {
                pp.setExplodePercent(pieDS.getKey(minSegment), explodePct.doubleValue());
            }
        } else {
            if (maxSegment >= 0) {
                pp.setExplodePercent(pieDS.getKey(maxSegment), explodePct.doubleValue());
            }
        }
    }

}

From source file:edu.uara.wrappers.customcharts.CustomPieChart.java

/**
 * get series color (section paint)/*from  www  . j  a  v  a  2 s  .  com*/
 * @param series
 * @return
 */
@Override
public Color getSeriesColor(int series) {
    Color color = null;
    Plot plot = chart.getPlot();
    if (plot instanceof PiePlot) {
        PiePlot p = (PiePlot) chart.getPlot();
        PieDataset pds = p.getDataset();
        String section = pds.getKey(series).toString();
        color = (Color) p.getSectionPaint(section);
    } else if (plot instanceof MultiplePiePlot) {
        MultiplePiePlot p = (MultiplePiePlot) plot;
        JFreeChart pieCh = p.getPieChart();
        PiePlot piePlot = (PiePlot) pieCh.getPlot();
        PieDataset pds = piePlot.getDataset();
        String section = pds.getKey(series).toString();
        color = (Color) piePlot.getSectionPaint(section);
    }
    return color;
}

From source file:edu.uara.wrappers.customcharts.CustomPieChart.java

/**
 * set series color (Section for piechart)
 * @param series/*from www. ja  v  a2  s. com*/
 * @param color
 */
@Override
public void setSeriesColor(int series, Color color) {
    //call setSectionPaint
    Plot plot = chart.getPlot();
    if (plot instanceof PiePlot) {
        PiePlot p = (PiePlot) chart.getPlot();
        PieDataset pds = p.getDataset();
        String section = pds.getKey(series).toString();
        this.setSectionPaint(section, color);
    } else if (plot instanceof MultiplePiePlot) {
        MultiplePiePlot p = (MultiplePiePlot) plot;
        JFreeChart pieCh = p.getPieChart();
        PiePlot piePlot = (PiePlot) pieCh.getPlot();
        PieDataset pds = piePlot.getDataset();
        String section = pds.getKey(series).toString();
        setSectionPaint(section, color);
        chart.fireChartChanged();
    }
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.PieChartExpression.java

protected void configureChart(final JFreeChart chart) {
    super.configureChart(chart);

    final Plot plot = chart.getPlot();
    final PiePlot pp = (PiePlot) plot;
    final PieDataset pieDS = pp.getDataset();
    pp.setDirection(rotationClockwise ? Rotation.CLOCKWISE : Rotation.ANTICLOCKWISE);
    if ((explodeSegment != null) && (explodePct != null)) {
        configureExplode(pp);//from   w  w  w.  j  a v a2  s.  co m
    }
    if (StringUtils.isEmpty(getTooltipFormula()) == false) {
        pp.setToolTipGenerator(new FormulaPieTooltipGenerator(getRuntime(), getTooltipFormula()));
    }
    if (StringUtils.isEmpty(getUrlFormula()) == false) {
        pp.setURLGenerator(new FormulaPieURLGenerator(getRuntime(), getUrlFormula()));
    }

    pp.setIgnoreNullValues(ignoreNulls);
    pp.setIgnoreZeroValues(ignoreZeros);
    if (Boolean.FALSE.equals(getItemsLabelVisible())) {
        pp.setLabelGenerator(null);
    } else {
        final ExpressionRuntime runtime = getRuntime();
        final Locale locale = runtime.getResourceBundleFactory().getLocale();

        final FastDecimalFormat fastPercent = new FastDecimalFormat(FastDecimalFormat.TYPE_PERCENT, locale);
        final FastDecimalFormat fastInteger = new FastDecimalFormat(FastDecimalFormat.TYPE_INTEGER, locale);

        final DecimalFormat numFormat = new DecimalFormat(fastInteger.getPattern(),
                new DecimalFormatSymbols(locale));
        numFormat.setRoundingMode(RoundingMode.HALF_UP);

        final DecimalFormat percentFormat = new DecimalFormat(fastPercent.getPattern(),
                new DecimalFormatSymbols(locale));
        percentFormat.setRoundingMode(RoundingMode.HALF_UP);

        final StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(pieLabelFormat,
                numFormat, percentFormat);
        pp.setLabelGenerator(labelGen);

        final StandardPieSectionLabelGenerator legendGen = new StandardPieSectionLabelGenerator(
                pieLegendLabelFormat, numFormat, percentFormat);
        pp.setLegendLabelGenerator(legendGen);
    }

    if (StringUtils.isEmpty(getLabelFont()) == false) {
        pp.setLabelFont(Font.decode(getLabelFont()));
    }

    if (pieDS != null) {
        final String[] colors = getSeriesColor();
        for (int i = 0; i < colors.length; i++) {
            if (i < pieDS.getItemCount()) {
                pp.setSectionPaint(pieDS.getKey(i), parseColorFromString(colors[i]));
            } else {
                break;
            }
        }
    }

    if (shadowPaint != null) {
        pp.setShadowPaint(shadowPaint);
    }
    if (shadowXOffset != null) {
        pp.setShadowXOffset(shadowXOffset.doubleValue());
    }
    if (shadowYOffset != null) {
        pp.setShadowYOffset(shadowYOffset.doubleValue());
    }
    pp.setCircular(circular);

    if (isShowBorder() == false || isChartSectionOutline() == false) {
        chart.setBorderVisible(false);
        chart.getPlot().setOutlineVisible(false);
    }

}