Java Code Examples for org.jfree.data.general.DatasetUtilities#calculatePieDatasetTotal()

The following examples show how to use org.jfree.data.general.DatasetUtilities#calculatePieDatasetTotal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPieItemLabelGenerator.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 2
Source File: AbstractPieItemLabelGenerator.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 3
Source File: AbstractPieItemLabelGenerator.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 4
Source File: AbstractPieItemLabelGenerator.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 5
Source File: AbstractPieItemLabelGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 6
Source File: AbstractPieItemLabelGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the 
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);  
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total; 
        }
    }       
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 7
Source File: AbstractPieItemLabelGenerator.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the 
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);  
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total; 
        }
    }       
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 8
Source File: AbstractPieItemLabelGenerator.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 9
Source File: AbstractPieItemLabelGenerator.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates the array of items that can be passed to the
 * {@link MessageFormat} class for creating labels.  The returned array
 * contains four values:
 * <ul>
 * <li>result[0] = the section key converted to a <code>String</code>;</li>
 * <li>result[1] = the formatted data value;</li>
 * <li>result[2] = the formatted percentage (of the total);</li>
 * <li>result[3] = the formatted total value.</li>
 * </ul>
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param key  the key (<code>null</code> not permitted).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[4];
    double total = DatasetUtilities.calculatePieDatasetTotal(dataset);
    result[0] = key.toString();
    Number value = dataset.getValue(key);
    if (value != null) {
        result[1] = this.numberFormat.format(value);
    }
    else {
        result[1] = "null";
    }
    double percent = 0.0;
    if (value != null) {
        double v = value.doubleValue();
        if (v > 0.0) {
            percent = v / total;
        }
    }
    result[2] = this.percentFormat.format(percent);
    result[3] = this.numberFormat.format(total);
    return result;
}
 
Example 10
Source File: PiePlot.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the pie.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param info  chart rendering info.
 */
protected void drawPie(Graphics2D g2, Rectangle2D plotArea,
                       PlotRenderingInfo info) {

    PiePlotState state = initialise(g2, plotArea, this, null, info);

    Rectangle2D[] areas = calculateLinkAndExplodeAreas(g2, plotArea);
    Rectangle2D linkArea = areas[0];
    Rectangle2D explodeArea = areas[1];
    state.setLinkArea(linkArea);
    state.setExplodedPieArea(explodeArea);

    // the pie area defines the circle/ellipse for regular pie sections.
    // it is defined by shrinking the explodeArea by the explodeMargin
    // factor.
    double maximumExplodePercent = getMaximumExplodePercent();
    double percent = maximumExplodePercent / (1.0 + maximumExplodePercent);

    double h1 = explodeArea.getWidth() * percent;
    double v1 = explodeArea.getHeight() * percent;
    Rectangle2D pieArea = new Rectangle2D.Double(explodeArea.getX()
            + h1 / 2.0, explodeArea.getY() + v1 / 2.0,
            explodeArea.getWidth() - h1, explodeArea.getHeight() - v1);

    if (DEBUG_DRAW_PIE_AREA) {
        g2.setPaint(Color.green);
        g2.draw(pieArea);
    }
    state.setPieArea(pieArea);
    state.setPieCenterX(pieArea.getCenterX());
    state.setPieCenterY(pieArea.getCenterY());
    state.setPieWRadius(pieArea.getWidth() / 2.0);
    state.setPieHRadius(pieArea.getHeight() / 2.0);

    // plot the data (unless the dataset is null)...
    if ((this.dataset != null) && (this.dataset.getKeys().size() > 0)) {

        PieDatasetSelectionState ss = findSelectionStateForDataset(
                this.dataset, state);
        List keys = this.dataset.getKeys();
        double totalValue = DatasetUtilities.calculatePieDatasetTotal(
                this.dataset);

        int passesRequired = state.getPassesRequired();
        for (int pass = 0; pass < passesRequired; pass++) {
            double runningTotal = 0.0;
            for (int section = 0; section < keys.size(); section++) {
                Number n = this.dataset.getValue(section);
                if (n != null) {
                    double value = n.doubleValue();
                    if (value > 0.0) {
                        runningTotal += value;
                        boolean selected = false;
                        if (ss != null) {
                            selected = ss.isSelected(this.dataset.getKey(section));
                        }
                        drawItem(g2, section, selected, explodeArea, state,
                                pass);
                    }
                }
            }
        }
        if (this.simpleLabels) {
            drawSimpleLabels(g2, keys, totalValue, plotArea, linkArea,
                    state);
        }
        else {
            drawLabels(g2, keys, totalValue, plotArea, linkArea, state);
        }

    }
    else {
        drawNoDataMessage(g2, plotArea);
    }
}
 
Example 11
Source File: PiePlot.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Selects a data item.
 * 
 * @param x
 * @param y
 * @param dataArea
 * @param source
 */
public void select(double x, double y, Rectangle2D dataArea,
        RenderingSource source) {

    System.out.println("select " + x + ", " + y);

    PieDatasetSelectionState state = findSelectionStateForDataset(
            dataset, source);
    if (state == null) {
        return;
    }

    Rectangle2D[] areas = calculateLinkAndExplodeAreas(null, dataArea);
    Rectangle2D linkArea = areas[0];
    Rectangle2D explodeArea = areas[1];

    // the pie area defines the circle/ellipse for regular pie sections.
    // it is defined by shrinking the explodeArea by the explodeMargin
    // factor.
    double maximumExplodePercent = getMaximumExplodePercent();
    double percent = maximumExplodePercent / (1.0 + maximumExplodePercent);

    double h1 = explodeArea.getWidth() * percent;
    double v1 = explodeArea.getHeight() * percent;
    Rectangle2D pieArea = new Rectangle2D.Double(explodeArea.getX()
            + h1 / 2.0, explodeArea.getY() + v1 / 2.0,
            explodeArea.getWidth() - h1, explodeArea.getHeight() - v1);

    // plot the data (unless the dataset is null)...
    if ((this.dataset != null) && (this.dataset.getKeys().size() > 0)) {


        List keys = this.dataset.getKeys();
        double total = DatasetUtilities.calculatePieDatasetTotal(
                this.dataset);
        double runningTotal = 0.0;
        for (int section = 0; section < keys.size(); section++) {
            Number n = this.dataset.getValue(section);
            if (n == null) {
                continue;
            }
            double value = n.doubleValue();
            if (value > 0.0) {
                double angle0 = calculateAngleForValue(runningTotal,
                        total);
                double angle1 = calculateAngleForValue(runningTotal
                        + value, total);
                runningTotal += value;
                System.out.println(this.dataset.getValue(section));
                System.out.println(angle0);
                System.out.println(angle1);
                double angle = (angle1 - angle0);
                if (Math.abs(angle) > getMinimumArcAngleToDraw()) {
                    double ep = 0.0;
                    double mep = getMaximumExplodePercent();
                    if (mep > 0.0) {
                        ep = getExplodePercent(getSectionKey(section)) / mep;
                    }
                    Rectangle2D arcBounds = getArcBounds(pieArea,
                            explodeArea, angle0, angle, ep);
                    Arc2D.Double arc = new Arc2D.Double(arcBounds,
                            angle0, angle, Arc2D.PIE);
                    if (arc.contains(x, y)) {
                        Comparable key = this.dataset.getKey(section);
                        state.setSelected(key, !state.isSelected(key));
                        System.out.println(key + " is " + state.isSelected(key));
                    }
                }
            }
        }
    }
}
 
Example 12
Source File: PiePlot.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a shape representing the hotspot for a pie section.
 *
 * @param g2  the graphics device.
 * @param dataArea  the area within which the data is being rendered.
 * @param selected  is the item selected?
 *
 * @return A shape equal to the hot spot for a data item.
 */
public Shape createHotSpotShape(Graphics2D g2, Rectangle2D dataArea,
        int section, boolean selected) {

    Number n = this.dataset.getValue(section);
    if (n == null) {
        return null;
    }
    double value = n.doubleValue();
    double angle1 = 0.0;
    double angle2 = 0.0;

    double total = DatasetUtilities.calculatePieDatasetTotal(this.dataset);
    double lead = 0.0;
    if (this.direction == Rotation.CLOCKWISE) {
        for (int i = 0; i < section; i++) {
            n = this.dataset.getValue(i);
            if (n != null) {
                value = n.doubleValue();
                if (value >= 0.0) {
                    lead = lead + value;
                }
            }
        }
        angle1 = getStartAngle() - lead / total * 360.0;
        angle2 = angle1 - value / total * 360.0;
    }
    else if (this.direction == Rotation.ANTICLOCKWISE) {
        angle1 = getStartAngle() + lead / total * 360.0;
        angle2 = angle1 + value / total * 360.0;
    }
    else {
        throw new IllegalStateException("Rotation type not recognised.");
    }

    double angle = (angle2 - angle1);
    if (Math.abs(angle) > getMinimumArcAngleToDraw()) {
        double ep = 0.0;
        double mep = getMaximumExplodePercent();
        if (mep > 0.0) {
            ep = getExplodePercent(getSectionKey(section)) / mep;
        }
        Rectangle2D arcBounds = getArcBounds(dataArea,
                dataArea, angle1, angle, ep);
        Arc2D.Double arc = new Arc2D.Double(arcBounds, angle1, angle,
                Arc2D.PIE);
        return arc;
    }
    return null;
}
 
Example 13
Source File: PiePlot.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the pie.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area.
 * @param info  chart rendering info.
 */
protected void drawPie(Graphics2D g2, Rectangle2D plotArea, 
                       PlotRenderingInfo info) {

    PiePlotState state = initialise(g2, plotArea, this, null, info);

    // adjust the plot area for interior spacing and labels...
    double labelWidth = 0.0;
    if (this.labelGenerator != null) {
        labelWidth = this.labelGap + this.maximumLabelWidth 
                     + this.labelLinkMargin;    
    }
    double gapHorizontal 
        = plotArea.getWidth() * (this.interiorGap + labelWidth);
    double gapVertical = plotArea.getHeight() * this.interiorGap;

    double linkX = plotArea.getX() + gapHorizontal / 2;
    double linkY = plotArea.getY() + gapVertical / 2;
    double linkW = plotArea.getWidth() - gapHorizontal;
    double linkH = plotArea.getHeight() - gapVertical;
    
    // make the link area a square if the pie chart is to be circular...
    if (this.circular) {
        double min = Math.min(linkW, linkH) / 2;
        linkX = (linkX + linkX + linkW) / 2 - min;
        linkY = (linkY + linkY + linkH) / 2 - min;
        linkW = 2 * min;
        linkH = 2 * min;
    }

    // the link area defines the dog leg points for the linking lines to 
    // the labels
    Rectangle2D linkArea = new Rectangle2D.Double(linkX, linkY, linkW, 
            linkH);
    state.setLinkArea(linkArea);
    
    // the explode area defines the max circle/ellipse for the exploded 
    // pie sections.  it is defined by shrinking the linkArea by the 
    // linkMargin factor.
    double hh = linkArea.getWidth() * this.labelLinkMargin;
    double vv = linkArea.getHeight() * this.labelLinkMargin;
    Rectangle2D explodeArea = new Rectangle2D.Double(linkX + hh / 2.0, 
            linkY + vv / 2.0, linkW - hh, linkH - vv);
   
    state.setExplodedPieArea(explodeArea);
    
    // the pie area defines the circle/ellipse for regular pie sections.
    // it is defined by shrinking the explodeArea by the explodeMargin 
    // factor. 
    double maximumExplodePercent = getMaximumExplodePercent();
    double percent = maximumExplodePercent / (1.0 + maximumExplodePercent);
    
    double h1 = explodeArea.getWidth() * percent;
    double v1 = explodeArea.getHeight() * percent;
    Rectangle2D pieArea = new Rectangle2D.Double(explodeArea.getX() 
            + h1 / 2.0, explodeArea.getY() + v1 / 2.0, 
            explodeArea.getWidth() - h1, explodeArea.getHeight() - v1);

    state.setPieArea(pieArea);
    state.setPieCenterX(pieArea.getCenterX());
    state.setPieCenterY(pieArea.getCenterY());
    state.setPieWRadius(pieArea.getWidth() / 2.0);
    state.setPieHRadius(pieArea.getHeight() / 2.0);
    // plot the data (unless the dataset is null)...
    if ((this.dataset != null) && (this.dataset.getKeys().size() > 0)) {

        List keys = this.dataset.getKeys();
        double totalValue 
            = DatasetUtilities.calculatePieDatasetTotal(this.dataset);

        int passesRequired = state.getPassesRequired();
        for (int pass = 0; pass < passesRequired; pass++) {
            double runningTotal = 0.0;
            for (int section = 0; section < keys.size(); section++) {
                Number n = this.dataset.getValue(section);
                if (n != null) {
                    double value = n.doubleValue();
                    if (value > 0.0) {
                        runningTotal += value;
                        drawItem(g2, section, explodeArea, state, pass);
                    }
                } 
            }
        }
        
        drawLabels(g2, keys, totalValue, plotArea, linkArea, state);

    }
    else {
        drawNoDataMessage(g2, plotArea);
    }
}