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

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

Introduction

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

Prototype

public Number getValue(Comparable key);

Source Link

Document

Returns the value for a given key.

Usage

From source file:org.openfaces.component.chart.impl.helpers.ChartInfoUtil.java

public static PieSectorInfo getPieSectorInfo(PieDataset pieDataset, Comparable comparable, int dsIndex) {
    double total = 0;
    List keys = pieDataset.getKeys();
    for (Object key : keys) {
        Object value = pieDataset.getValue((Comparable) key);
        if (value != null) {
            double dValue = ((Number) value).doubleValue();
            if (dValue > 0)
                total = total + dValue;//from  ww w .  j av  a  2  s .com
        }
    }
    int index = pieDataset.getIndex(comparable);

    Object value = pieDataset.getValue(index);
    double dValue = 0;
    if (value != null) {
        dValue = ((Number) value).doubleValue();
    }

    PieSectorInfo sector = new PieSectorInfoImpl();
    sector.setKey(comparable);
    sector.setValue(value);
    sector.setSeriesTotal(total);
    sector.setIndex(index);
    sector.setSeries(new SeriesInfoImpl());

    sector.getSeries().setIndex(dsIndex);

    double p = (dValue / total);
    DecimalFormat nf1 = new DecimalFormat("#.00%");
    String proportionalPercent = nf1.format(p);

    sector.setProportionalValue(proportionalPercent);
    return sector;
}

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 . 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:lucee.runtime.chart.PieSectionLabelGeneratorImpl.java

public String generateSectionLabel(PieDataset pd, Comparable c) {
    double value = Caster.toDoubleValue(pd.getValue(c), true, 0.0);
    return LabelFormatUtil.format(labelFormat, value);
}

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

/**
 * Generates a label for a pie section./* w  w w  .  j  av a  2s  .com*/
 *
 * @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:lucee.runtime.chart.PieToolTipGeneratorImpl.java

@Override
public String generateToolTip(PieDataset dataset, Comparable key) {

    String result = null;//from w  w  w.  j av a2 s  .  c o  m
    if (dataset != null) {
        result = LabelFormatUtil.format(labelFormat, dataset.getValue(key).doubleValue());
    }
    return result;

    // TODO Auto-generated method stub
    //return toolTipGenerator.generateToolTip(dataset, key);
}

From source file:de.xirp.chart.CustomLabelGenerator.java

/**
 * Generates a section label for the given
 * {@link org.jfree.data.general.PieDataset} and key. If
 * <code>options.is(OptionName.USE_RELATIVE)</code> is
 * <code>true</code> the method returns a string containing
 * information about percentage. If is is <code>false</code> the
 * method returns a string containing information about absolute
 * occurrence.//w w  w  .  ja  va  2 s.c  om
 * 
 * @return A <code>String</code> containing either information
 *         about percentage or absolute occurrences. This text is
 *         set as label in the generated pie chart.
 * @see de.xirp.chart.ChartManager
 * @see org.jfree.chart.labels.PieSectionLabelGenerator#generateSectionLabel(org.jfree.data.general.PieDataset,
 *      java.lang.Comparable)
 */
public String generateSectionLabel(PieDataset dataset, Comparable key) {
    String result = null;
    if (dataset != null) {
        result = key + " " + dataset.getValue(key).toString(); //$NON-NLS-1$
        if (options.is(OptionName.USE_RELATIVE)) {
            result = result + I18n.getString("CustomLabelGenerator.text.percentOccurence"); //$NON-NLS-1$
        } else {
            result = result + I18n.getString("CustomLabelGenerator.text.occurence"); //$NON-NLS-1$
        }
    }
    return result;
}

From source file:org.hxzon.demo.jfreechart.PieDatasetDemo2.java

private static JFreeChart createPieChart(PieDataset dataset, PieDataset previousDataset) {
    final boolean greenForIncrease = true;
    final boolean subTitle = true;
    final boolean showDifference = true;
    int percentDiffForMaxScale = 20;
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));

    if (tooltips) {
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    }//ww  w .java2 s . c o m
    if (urls) {
        plot.setURLGenerator(new StandardPieURLGenerator());
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    List<Comparable> keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }

    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (@SuppressWarnings("rawtypes")
    Comparable key : keys) {
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);

        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.green);
            } else {
                plot.setSectionPaint(key, Color.red);
            }
            if (showDifference) {
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255
                    : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue()
                    || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                series.setValue(
                        key + " (" + (percentChange >= 0 ? "+" : "")
                                + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")",
                        newValue);
            }
        }
    }

    if (showDifference) {
        plot.setDataset(series);
    }

    JFreeChart chart = new JFreeChart("Pie Chart Demo 2", JFreeChart.DEFAULT_TITLE_FONT, plot, legend);

    if (subTitle) {
        TextTitle subtitle = null;
        subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-"
                + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+"
                + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    plot.setNoDataMessage("No data available");

    return chart;

}

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

/**
 * Generates a tool tip text item for the specified item in the dataset. This method can return <code>null</code> to
 * indicate that no tool tip should be displayed for an item.
 *
 * @param dataset the dataset (<code>null</code> not permitted).
 * @param key     the section key (<code>null</code> not permitted).
 * @return The tool tip text (possibly <code>null</code>).
 *///  ww w .  ja  va2 s.  c  om
public String generateToolTip(PieDataset dataset, Comparable key) {
    try {
        final Object[] keys = dataset.getKeys().toArray();
        final Object[] items = new Object[keys.length];
        for (int i = 0; i < keys.length; i++) {
            items[i] = dataset.getValue(i);
        }
        final Object[] values = new Object[] { key, keys, dataset.getValue(key), items,
                IntegerCache.getInteger(dataset.getIndex(key)) };
        formulaExpression.setRuntime(
                new WrapperExpressionRuntime(new StaticDataRow(ADDITIONAL_COLUMN_KEYS, values), runtime));
        final Object o = formulaExpression.getValue();
        if (o == null) {
            return null;
        }
        return String.valueOf(o);
    } finally {
        formulaExpression.setRuntime(null);
    }
}

From source file:com.newatlanta.bluedragon.PieURLGenerator.java

/**
 * Generates a URL./*from  ww w.ja v  a2s .co  m*/
 *
 * @param data  the dataset.
 * @param key  the item key.
 * @param pieIndex  the pie index (ignored).
 *
 * @return A string containing the generated URL.
 */
@SuppressWarnings("deprecation")
public String generateURL(PieDataset data, Comparable key, int pieIndex) {

    String categoryKey = key.toString();
    Number numberValue = data.getValue(key);
    String value;
    if (numberValue == null)
        value = "";
    else if (dateFormat != null)
        value = this.dateFormat.format(numberValue);
    else
        value = this.numberFormat.format(numberValue);

    StringBuilder generatedURL = new StringBuilder(urlLower.length());
    for (int i = 0; i < urlLower.length();) {
        char ch = urlLower.charAt(i);
        if (ch == '$') {
            if (urlLower.regionMatches(i, "$serieslabel$", 0, "$serieslabel$".length())) {
                generatedURL.append("");
                i = i + "$serieslabel$".length();
            } else if (urlLower.regionMatches(i, "$itemlabel$", 0, "$itemlabel$".length())) {
                generatedURL.append(URLEncoder.encode(categoryKey));
                i = i + "$itemlabel$".length();
            } else if (urlLower.regionMatches(i, "$value$", 0, "$value$".length())) {
                generatedURL.append(URLEncoder.encode(value));
                i = i + "$value$".length();
            } else {
                // Preserve case by retrieving char from original URL
                generatedURL.append(url.charAt(i));
                i++;
            }
        } else {
            // Preserve case by retrieving char from original URL
            generatedURL.append(url.charAt(i));
            i++;
        }
    }
    return generatedURL.toString();
}

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

/**
 * Generates a URL for one item in a {@link org.jfree.data.general.PieDataset}. As a guideline, the URL should be
 * valid within the context of an XHTML 1.0 document.
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key      the item key (<code>null</code> not permitted).
 * @param pieIndex the pie index (differentiates between pies in a 'multi' pie chart).
 * @return A string containing the URL./*from   ww  w. j a  v  a  2s .  c om*/
 */
public String generateURL(final PieDataset dataset, final Comparable key, final int pieIndex) {
    try {
        final Object[] keys = dataset.getKeys().toArray();
        final Object[] items = new Object[keys.length];
        for (int i = 0; i < keys.length; i++) {
            items[i] = dataset.getValue(key);
        }
        final Object[] values = new Object[] { key, keys, dataset.getValue(key), items,
                IntegerCache.getInteger(pieIndex) };
        formulaExpression.setRuntime(
                new WrapperExpressionRuntime(new StaticDataRow(ADDITIONAL_COLUMN_KEYS, values), runtime));
        final Object o = formulaExpression.getValue();
        if (o == null) {
            return null;
        }
        return String.valueOf(o);
    } finally {
        formulaExpression.setRuntime(null);
    }
}