Example usage for org.jfree.data.general PieDataset getKey

List of usage examples for org.jfree.data.general PieDataset getKey

Introduction

In this page you can find the example usage for org.jfree.data.general PieDataset getKey.

Prototype

public Comparable getKey(int index);

Source Link

Document

Returns the key associated with the item at a given position.

Usage

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

/**
 * ?1 ,? Familiar(30%) /*w w  w  .j  av a  2s  .co m*/
 * @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  ww  w  . ja v  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:org.talend.dataprofiler.chart.ChartDecorator.java

/**
 * DOC bZhou Comment method "decorate"./*from w  w  w  .jav  a2  s.c  o  m*/
 * 
 * @param chart
 */
public static void decorate(JFreeChart chart, PlotOrientation orientation) {
    if (chart != null) {
        // TDQ-11522: Set white background on charts in the editors
        chart.setBackgroundPaint(Color.white);
        // TDQ-11522~
        Plot plot = chart.getPlot();
        if (plot instanceof CategoryPlot) {
            decorateCategoryPlot(chart, orientation);

            CategoryDataset dataset = chart.getCategoryPlot().getDataset();
            int rowCount = dataset != null ? dataset.getRowCount() : 20;
            for (int i = 0; i < rowCount; i++) {
                // by zshen bug 14173 add the color in the colorList when chart need more the color than 8.
                if (i >= COLOR_LIST.size()) {
                    COLOR_LIST.add(generateRandomColor(COLOR_LIST));
                }
                // ~14173
                ((CategoryPlot) plot).getRenderer().setSeriesPaint(i, COLOR_LIST.get(i));
            }

        } else if (plot instanceof XYPlot) {
            decorateXYPlot(chart);

            int count = chart.getXYPlot().getDataset().getSeriesCount();
            for (int i = 0; i < count; i++) {
                // by zshen bug 14173 add the color in the colorList when chart need the colors more than 8.
                if (i >= COLOR_LIST.size()) {
                    COLOR_LIST.add(generateRandomColor(COLOR_LIST));
                }
                // ~14173
                ((XYPlot) plot).getRenderer().setSeriesPaint(i, COLOR_LIST.get(i));
            }
        } else if (plot instanceof PiePlot) {
            decoratePiePlot(chart);

            // ADD msjian TDQ-8046 2013-10-17: add the color's control for pie chart
            PieDataset piedataset = ((PiePlot) plot).getDataset();
            for (int i = 0; i < piedataset.getItemCount(); i++) {
                if (i >= PIE_COLOR_LIST.size()) {
                    PIE_COLOR_LIST.add(generateRandomColor(PIE_COLOR_LIST));
                }
                Comparable<?> key = piedataset.getKey(i);
                ((PiePlot) plot).setSectionPaint(key, PIE_COLOR_LIST.get(i));
            }
            // TDQ-8046~
        }
    }
}

From source file:org.talend.dataprofiler.chart.ChartDecorator.java

public static void decorateDuplicatePieChart(JFreeChart chart) {
    if (chart != null) {
        Plot plot = chart.getPlot();//from  w w  w . ja v a  2s. c  o m
        if (plot instanceof PiePlot) {
            decoratePiePlot(chart);

            // ADD msjian TDQ-8046 2013-10-17: add the color's control for pie chart
            PieDataset piedataset = ((PiePlot) plot).getDataset();
            for (int i = 0; i < piedataset.getItemCount(); i++) {
                if (i >= DUPLICATE_PIE_COLOR_LIST.size()) {
                    DUPLICATE_PIE_COLOR_LIST.add(generateRandomColor(DUPLICATE_PIE_COLOR_LIST));
                }
                Comparable<?> key = piedataset.getKey(i);
                ((PiePlot) plot).setSectionPaint(key, DUPLICATE_PIE_COLOR_LIST.get(i));
            }
            // TDQ-8046~
        }
    }
}

From source file:lucee.runtime.chart.PieSectionLegendLabelGeneratorImpl.java

public String generateSectionLabel(PieDataset pd, Comparable c) {
    String value = Caster.toString(pd.getKey(pd.getIndex(c)), "");

    StringList list = ListUtil.toList(value, '\n');
    StringBuilder sb = new StringBuilder();
    String line;/*from w w w . ja va  2 s  .c  o m*/
    int lineLen;
    while (list.hasNext()) {
        line = list.next();
        lineLen = metrics.stringWidth(line);
        if (lineLen > with) {
            reorganize(sb, list, new StringBuilder(line));
            break;
        }
        if (sb.length() > 0)
            sb.append('\n');
        sb.append(line);
    }

    //int strLen = metrics.stringWidth(value);
    return sb.toString();//metrics.stringWidth(value)+"-"+with+":"+value;
    //return "StringUtil.reverse()";
}

From source file:unikn.dbis.univis.visualization.chart.LabelGenerator.java

/**
 * Generates a label for a pie section./*from www.  j a  va2  s. co m*/
 *
 * @param dataset the dataset (<code>null</code> not permitted).
 * @param key     the section key (<code>null</code> not permitted).
 * @return The label (possibly <code>null</code>).
 */
public String generateSectionLabel(PieDataset dataset, Comparable key) {
    Integer value = dataset.getValue(key).intValue();
    Comparable name = dataset.getKey(dataset.getIndex(key));
    Double percent = ((full / total) * value);

    return name.toString() + " = " + value.toString() + " -> " + decimalFormat.format(percent) + "%";
}

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

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

    numberOfPagesTest(1);/*  w  w  w  . j  a  va  2  s.  c  om*/

    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:edu.uara.wrappers.customcharts.CustomPieChart.java

/**
 * get series color (section paint)//ww w.  j  av 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// w  w  w  . ja  v a 2 s. c o m
 * @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:de.laures.cewolf.taglib.tags.ChartMapTag.java

private String generateToolTip(Dataset dataset, ChartEntity ce) throws JspException {
    String tooltip = null;/*from  w  ww  .  ja va 2s . c o m*/
    if (useJFreeChartTooltipGenerator) {
        tooltip = ce.getToolTipText();
    } else if (toolTipGenerator instanceof CategoryToolTipGenerator
            || toolTipGenerator instanceof XYToolTipGenerator
            || toolTipGenerator instanceof PieToolTipGenerator) {
        if (toolTipGenerator instanceof CategoryToolTipGenerator) {
            if (ce instanceof CategoryItemEntity) {
                CategoryItemEntity catEnt = (CategoryItemEntity) ce;
                tooltip = ((CategoryToolTipGenerator) toolTipGenerator).generateToolTip(
                        (CategoryDataset) dataset, catEnt.getSeries(), catEnt.getCategoryIndex());
            }
        }

        if (toolTipGenerator instanceof XYToolTipGenerator) {
            if (ce instanceof XYItemEntity) {
                XYItemEntity xyEnt = (XYItemEntity) ce;
                tooltip = ((XYToolTipGenerator) toolTipGenerator).generateToolTip((XYDataset) dataset,
                        xyEnt.getSeriesIndex(), xyEnt.getItem());
            }
        }

        if (toolTipGenerator instanceof PieToolTipGenerator) {
            if (ce instanceof PieSectionEntity) {
                PieSectionEntity pieEnt = (PieSectionEntity) ce;
                PieDataset ds = (PieDataset) dataset;
                final int index = pieEnt.getSectionIndex();
                tooltip = ((PieToolTipGenerator) toolTipGenerator).generateToolTip(ds, ds.getKey(index), index);
            }
        }
    } else {
        // throw because category is unknown
        throw new JspException("TooltipgGenerator of class " + toolTipGenerator.getClass().getName()
                + " does not implement the appropriate TooltipGenerator interface for entity type "
                + ce.getClass().getName());
    }
    return tooltip;
}