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

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

Introduction

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

Prototype

public int getIndex(Comparable key);

Source Link

Document

Returns the index 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  w w w.j  a v a2  s  .  co m*/
        }
    }
    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:org.jfree.eastwood.GPieSectionLabelGenerator.java

/**
 * Returns the section label as supplied in the constructor.
 *
 * @param dataset  the dataset.//from   w w w. j  ava  2 s. com
 * @param key  the section key.
 *
 * @return The section label.
 */
public String generateSectionLabel(PieDataset dataset, Comparable key) {
    int index = dataset.getIndex(key);
    if (index < this.labels.size()) {
        return this.labels.get(index).toString();
    } else {
        return null;
    }
}

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  ww w .j  a v a  2s.  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./*  w  w w.j a  v a 2  s .  c om*/
 *
 * @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: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>).
 *///from   w w  w .j a  v a 2  s.com
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:jgnash.ui.report.compiled.IncomeExpensePieChart.java

private JFreeChart createPieChart(final Account a) {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    Objects.requireNonNull(a);//from   w  ww. j a  v  a 2s.co m

    PieDataset data = createPieDataSet(a);
    PiePlot plot = new PiePlot(data);

    // rebuilt each time because they're based on the account's commodity
    CurrencyNode defaultCurrency = engine.getDefaultCurrency();
    NumberFormat valueFormat = CommodityFormat.getFullNumberFormat(a.getCurrencyNode());
    NumberFormat percentFormat = new DecimalFormat("0.0#%");
    defaultLabels = new StandardPieSectionLabelGenerator("{0} = {1}", valueFormat, percentFormat);
    percentLabels = new StandardPieSectionLabelGenerator("{0} = {1}\n{2}", valueFormat, percentFormat);

    plot.setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels);

    plot.setLabelGap(.02);
    plot.setInteriorGap(.1);

    // if we had to add a section for the account (because it has it's
    // own transactions, not just child accounts), separate it from children.
    if (data.getIndex(a) != -1) {
        plot.setExplodePercent(a, .10);
    }

    String title;

    // pick an appropriate title
    if (a.getAccountType() == AccountType.EXPENSE) {
        title = rb.getString("Title.PercentExpense");
    } else if (a.getAccountType() == AccountType.INCOME) {
        title = rb.getString("Title.PercentIncome");
    } else {
        title = rb.getString("Title.PercentDist");
    }

    title = title + " - " + a.getPathName();

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    BigDecimal total = a.getTreeBalance(startField.getLocalDate(), endField.getLocalDate()).abs();

    String subtitle = valueFormat.format(total);
    if (!defaultCurrency.equals(a.getCurrencyNode())) {
        BigDecimal totalDefaultCurrency = total.multiply(a.getCurrencyNode().getExchangeRate(defaultCurrency));
        NumberFormat defaultValueFormat = CommodityFormat.getFullNumberFormat(defaultCurrency);
        subtitle += "  -  " + defaultValueFormat.format(totalDefaultCurrency);
    }
    chart.addSubtitle(new TextTitle(subtitle));
    chart.setBackgroundPaint(null);

    return chart;
}