Example usage for org.jfree.data DataUtilities calculateColumnTotal

List of usage examples for org.jfree.data DataUtilities calculateColumnTotal

Introduction

In this page you can find the example usage for org.jfree.data DataUtilities calculateColumnTotal.

Prototype

public static double calculateColumnTotal(Values2D data, int column) 

Source Link

Document

Returns the total of the values in one column of the supplied data table.

Usage

From source file:org.talend.dataprofiler.chart.preview.DQRuleItemLabelGenerator.java

@Override
protected Object[] createItemArray(CategoryDataset dataset, int row, int column) {

    Object[] result = new Object[4];
    result[0] = dataset.getRowKey(row).toString();
    result[1] = dataset.getColumnKey(column).toString();
    Number value = dataset.getValue(row, column);
    if (value != null && !value.equals(Double.NaN)) {
        if (super.getNumberFormat() != null) {
            result[2] = super.getNumberFormat().format(value);
        } else if (super.getDateFormat() != null) {
            result[2] = super.getDateFormat().format(value);
        }//from  w w w  .j av a 2 s. com

        double total = DataUtilities.calculateColumnTotal(dataset, column);
        double percent = value.doubleValue() / total;
        // MOD qiongli bug 21589,override for just changeing this line.avoid 99.99% to show 100%
        // result[3] = this.percentFormat.format(percent);
        result[3] = stringformat(percent, 0).toString();
    } else {
        result[2] = "-"; //$NON-NLS-1$
        result[3] = "-"; //$NON-NLS-1$
    }

    return result;

}

From source file:org.pentaho.plugin.jfreereport.reportcharts.backport.StackedAreaRenderer.java

/**
 * Draw a single data item.//from   w  w  w.j a  va 2  s.c om
 *
 * @param g2         the graphics device.
 * @param state      the renderer state.
 * @param dataArea   the data plot area.
 * @param plot       the plot.
 * @param domainAxis the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset    the data.
 * @param row        the row index (zero-based).
 * @param column     the column index (zero-based).
 * @param pass       the pass index.
 */
public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea,
        final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis,
        final CategoryDataset dataset, final int row, final int column, final int pass) {

    if (!isSeriesVisible(row)) {
        return;
    }

    if ((pass == 1) && !isItemLabelVisible(row, column)) {
        return;
    }

    // setup for collecting optional entity info...
    Shape entityArea = null;
    final EntityCollection entities = state.getEntityCollection();

    double y1 = 0.0;
    Number n = dataset.getValue(row, column);
    if (n != null) {
        y1 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset, column);
            y1 = y1 / total;
        }
    }
    final double[] stack1 = getStackValues(dataset, row, column);

    // leave the y values (y1, y0) untranslated as it is going to be be
    // stacked up later by previous series values, after this it will be
    // translated.
    double xx1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    // get the previous point and the next point so we can calculate a
    // "hot spot" for the area (used by the chart entity)...
    double y0 = 0.0;
    n = dataset.getValue(row, Math.max(column - 1, 0));
    if (n != null) {
        y0 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset, Math.max(column - 1, 0));
            y0 = y0 / total;
        }
    }
    final double[] stack0 = getStackValues(dataset, row, Math.max(column - 1, 0));

    // FIXME: calculate xx0
    double xx0 = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    final int itemCount = dataset.getColumnCount();
    double y2 = 0.0;
    n = dataset.getValue(row, Math.min(column + 1, itemCount - 1));
    if (n != null) {
        y2 = n.doubleValue();
        if (this.renderAsPercentages) {
            final double total = DataUtilities.calculateColumnTotal(dataset,
                    Math.min(column + 1, itemCount - 1));
            y2 = y2 / total;
        }
    }
    final double[] stack2 = getStackValues(dataset, row, Math.min(column + 1, itemCount - 1));

    double xx2 = domainAxis.getCategoryEnd(column, getColumnCount(), dataArea, plot.getDomainAxisEdge());

    // This gets rid of the white lines between most category values
    // Doug Moran - Hitachi Vantara
    xx0 = Math.round(xx0);
    xx1 = Math.round(xx1);
    xx2 = Math.round(xx2);

    // FIXME: calculate xxLeft and xxRight
    final double xxLeft = xx0;
    final double xxRight = xx2;

    final double[] stackLeft = averageStackValues(stack0, stack1);
    final double[] stackRight = averageStackValues(stack1, stack2);
    final double[] adjStackLeft = adjustedStackValues(stack0, stack1);
    final double[] adjStackRight = adjustedStackValues(stack1, stack2);

    final float transY1;

    final RectangleEdge edge1 = plot.getRangeAxisEdge();

    final GeneralPath left = new GeneralPath();
    final GeneralPath right = new GeneralPath();
    if (y1 >= 0.0) { // handle positive value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[1], dataArea, edge1);
        final float transStack1 = (float) rangeAxis.valueToJava2D(stack1[1], dataArea, edge1);
        final float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[1], dataArea, edge1);

        // LEFT POLYGON
        if (y0 >= 0.0) {
            final double yleft = (y0 + y1) / 2.0 + stackLeft[1];
            final float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo((float) xx1, transY1);
            left.lineTo((float) xx1, transStack1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.lineTo((float) xxLeft, transYLeft);
            left.closePath();
        } else {
            left.moveTo((float) xx1, transStack1);
            left.lineTo((float) xx1, transY1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.closePath();
        }

        final float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[1], dataArea, edge1);
        // RIGHT POLYGON
        if (y2 >= 0.0) {
            final double yright = (y1 + y2) / 2.0 + stackRight[1];
            final float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transYRight);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        } else {
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        }
    } else { // handle negative value
        transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[0], dataArea, edge1);
        final float transStack1 = (float) rangeAxis.valueToJava2D(stack1[0], dataArea, edge1);
        final float transStackLeft = (float) rangeAxis.valueToJava2D(adjStackLeft[0], dataArea, edge1);

        // LEFT POLYGON
        if (y0 >= 0.0) {
            left.moveTo((float) xx1, transStack1);
            left.lineTo((float) xx1, transY1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.clone();
        } else {
            final double yleft = (y0 + y1) / 2.0 + stackLeft[0];
            final float transYLeft = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
            left.moveTo((float) xx1, transY1);
            left.lineTo((float) xx1, transStack1);
            left.lineTo((float) xxLeft, transStackLeft);
            left.lineTo((float) xxLeft, transYLeft);
            left.closePath();
        }
        final float transStackRight = (float) rangeAxis.valueToJava2D(adjStackRight[0], dataArea, edge1);

        // RIGHT POLYGON
        if (y2 >= 0.0) {
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        } else {
            final double yright = (y1 + y2) / 2.0 + stackRight[0];
            final float transYRight = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
            right.moveTo((float) xx1, transStack1);
            right.lineTo((float) xx1, transY1);
            right.lineTo((float) xxRight, transYRight);
            right.lineTo((float) xxRight, transStackRight);
            right.closePath();
        }
    }

    if (pass == 0) {
        final Paint itemPaint = getItemPaint(row, column);
        g2.setPaint(itemPaint);
        g2.fill(left);
        g2.fill(right);

        // add an entity for the item...
        if (entities != null) {
            final GeneralPath gp = new GeneralPath(left);
            gp.append(right, false);
            entityArea = gp;
            addItemEntity(entities, dataset, row, column, entityArea);
        }
    } else if (pass == 1) {
        drawItemLabel(g2, plot.getOrientation(), dataset, row, column, xx1, transY1, y1 < 0.0);
    }

}

From source file:org.pentaho.plugin.jfreereport.reportcharts.backport.StackedAreaRenderer.java

/**
 * Calculates the stacked values (one positive and one negative) of all series up to, but not including,
 * <code>series</code> for the specified item. It returns [0.0, 0.0] if <code>series</code> is the first series.
 *
 * @param dataset the dataset (<code>null</code> not permitted).
 * @param series  the series index./* ww w .  j ava 2 s  .  c o  m*/
 * @param index   the item index.
 * @return An array containing the cumulative negative and positive values for all series values up to but excluding
 * <code>series</code> for <code>index</code>.
 */
protected double[] getStackValues(final CategoryDataset dataset, final int series, final int index) {
    final double[] result = new double[2];
    double total = 0.0;
    if (this.renderAsPercentages) {
        total = DataUtilities.calculateColumnTotal(dataset, index);
    }
    for (int i = 0; i < series; i++) {
        if (isSeriesVisible(i)) {
            double v = 0.0;
            final Number n = dataset.getValue(i, index);
            if (n != null) {
                v = n.doubleValue();
                if (this.renderAsPercentages) {
                    v = v / total;
                }
            }
            if (!Double.isNaN(v)) {
                if (v >= 0.0) {
                    result[1] += v;
                } else {
                    result[0] += v;
                }
            }
        }
    }
    return result;
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.backport.StackedAreaRenderer.java

/**
 * Calculates the stacked value of the all series up to, but not including <code>series</code> for the specified
 * category, <code>category</code>. It returns 0.0 if <code>series</code> is the first series, i.e. 0.
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param series   the series.//from ww  w.  j a va 2s  . c o m
 * @param category the category.
 * @return double returns a cumulative value for all series' values up to but excluding <code>series</code> for Object
 * <code>category</code>.
 * @deprecated As of 1.0.13, as the method is never used internally.
 */
protected double getPreviousHeight(final CategoryDataset dataset, final int series, final int category) {

    double result = 0.0;
    Number n;
    double total = 0.0;
    if (this.renderAsPercentages) {
        total = DataUtilities.calculateColumnTotal(dataset, category);
    }
    for (int i = 0; i < series; i++) {
        n = dataset.getValue(i, category);
        if (n != null) {
            double v = n.doubleValue();
            if (this.renderAsPercentages) {
                v = v / total;
            }
            result += v;
        }
    }
    return result;

}