Example usage for org.jfree.data.category CategoryDataset getValue

List of usage examples for org.jfree.data.category CategoryDataset getValue

Introduction

In this page you can find the example usage for org.jfree.data.category CategoryDataset getValue.

Prototype

public Number getValue(Comparable rowKey, Comparable columnKey);

Source Link

Document

Returns the value associated with the specified keys.

Usage

From source file:org.xwiki.rendering.internal.macro.chart.source.table.TableCategoryDatasetBuilderTest.java

@Test
public void testBuildCategoryDataset() throws Exception {
    String content = "| column 1 | column 2 | column 3 | column 4\n" + "| row 1 | 12 | 13 | 14 \n"
            + "| row 2 | 22 | 23 | 24 \n";
    setUpContentExpectation(content);/*from ww w .  j a va  2s  . com*/

    getDataSource().buildDataset(content, map("type", "line", "range", "B2-D3"), null);

    ChartModel chartModel = getDataSource().getChartModel();

    Dataset dataset = chartModel.getDataset();

    Assert.assertTrue(dataset instanceof CategoryDataset);
    Assert.assertTrue(chartModel.getAxis(0) instanceof CategoryAxis);
    Assert.assertTrue(chartModel.getAxis(1) instanceof ValueAxis);

    CategoryDataset categoryDataset = (CategoryDataset) dataset;

    Assert.assertTrue(categoryDataset.getRowKey(0).equals(" row 1 "));
    Assert.assertTrue(categoryDataset.getRowKey(1).equals(" row 2 "));

    Assert.assertTrue(categoryDataset.getColumnKey(0).equals(" column 2 "));
    Assert.assertTrue(categoryDataset.getColumnKey(1).equals(" column 3 "));
    Assert.assertTrue(categoryDataset.getColumnKey(2).equals(" column 4"));

    Assert.assertTrue(categoryDataset.getValue(0, 0).intValue() == 12);
    Assert.assertTrue(categoryDataset.getValue(0, 1).intValue() == 13);
    Assert.assertTrue(categoryDataset.getValue(0, 2).intValue() == 14);
    Assert.assertTrue(categoryDataset.getValue(1, 0).intValue() == 22);
    Assert.assertTrue(categoryDataset.getValue(1, 1).intValue() == 23);
    Assert.assertTrue(categoryDataset.getValue(1, 2).intValue() == 24);
}

From source file:nl.strohalm.cyclos.utils.jfreeAsymmetric.AsymmetricStatisticalLineAndShapeRenderer.java

/**
 * Draw a single data item.//  ww  w  . j  a  va2s . co m
 * 
 * @param g2 the graphics device.
 * @param state the renderer state.
 * @param dataArea the area in which the data is drawn.
 * @param plot the plot.
 * @param domainAxis the domain axis.
 * @param rangeAxis the range axis.
 * @param dataset the dataset (a {@link StatisticalCategoryDataset} is required).
 * @param row the row index (zero-based).
 * @param column the column index (zero-based).
 * @param pass the pass.
 */
@Override
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) {

    // nothing is drawn for null...
    final Number v = dataset.getValue(row, column);
    if (v == null) {
        return;
    }
    // *************** This line was changed relative to StatisticalLineAndShapeRenderer*****
    final AsymmetricStatisticalCategoryDataset statData = (AsymmetricStatisticalCategoryDataset) dataset;
    // *************** end of changed line **********************************************

    final Number meanValue = statData.getMeanValue(row, column);

    final PlotOrientation orientation = plot.getOrientation();

    // current data point...
    final double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea,
            plot.getDomainAxisEdge());

    final double y1 = rangeAxis.valueToJava2D(meanValue.doubleValue(), dataArea, plot.getRangeAxisEdge());

    Shape shape = getItemShape(row, column);
    if (orientation == PlotOrientation.HORIZONTAL) {
        shape = ShapeUtilities.createTranslatedShape(shape, y1, x1);
    } else if (orientation == PlotOrientation.VERTICAL) {
        shape = ShapeUtilities.createTranslatedShape(shape, x1, y1);
    }
    if (getItemShapeVisible(row, column)) {

        if (getItemShapeFilled(row, column)) {
            g2.setPaint(getItemPaint(row, column));
            g2.fill(shape);
        } else {
            if (getUseOutlinePaint()) {
                g2.setPaint(getItemOutlinePaint(row, column));
            } else {
                g2.setPaint(getItemPaint(row, column));
            }
            g2.setStroke(getItemOutlineStroke(row, column));
            g2.draw(shape);
        }
    }

    if (getItemLineVisible(row, column)) {
        if (column != 0) {

            final Number previousValue = statData.getValue(row, column - 1);
            if (previousValue != null) {

                // previous data point...
                final double previous = previousValue.doubleValue();
                final double x0 = domainAxis.getCategoryMiddle(column - 1, getColumnCount(), dataArea,
                        plot.getDomainAxisEdge());
                final double y0 = rangeAxis.valueToJava2D(previous, dataArea, plot.getRangeAxisEdge());

                Line2D line = null;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    line = new Line2D.Double(y0, x0, y1, x1);
                } else if (orientation == PlotOrientation.VERTICAL) {
                    line = new Line2D.Double(x0, y0, x1, y1);
                }
                g2.setPaint(getItemPaint(row, column));
                g2.setStroke(getItemStroke(row, column));
                g2.draw(line);
            }
        }
    }

    final RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    final RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    double rectX = domainAxis.getCategoryStart(column, getColumnCount(), dataArea, xAxisLocation);

    rectX = rectX + row * state.getBarWidth();

    g2.setPaint(getItemPaint(row, column));
    // ************* This is the block with changes relative to StatisticalLineAndShapeRenderer *********
    // standard deviation lines
    final Number highValObj = statData.getUpperValue(row, column);
    final Number lowValObj = statData.getLowerValue(row, column);

    if (highValObj != null && lowValObj != null) { // rinke added this test
        double highVal = highValObj.doubleValue();
        double lowVal = lowValObj.doubleValue();
        if (highVal > rangeAxis.getRange().getUpperBound()) {
            highVal = rangeAxis.valueToJava2D(rangeAxis.getRange().getUpperBound(), dataArea, yAxisLocation);
        } else {
            highVal = rangeAxis.valueToJava2D(highVal, dataArea, yAxisLocation);
        }

        if (lowVal < rangeAxis.getRange().getLowerBound()) {
            lowVal = rangeAxis.valueToJava2D(rangeAxis.getRange().getLowerBound(), dataArea, yAxisLocation);
        } else {
            lowVal = rangeAxis.valueToJava2D(lowVal, dataArea, yAxisLocation);
        }
        // ****************** end of changed block **********************************

        if (errorIndicatorPaint != null) {
            g2.setPaint(errorIndicatorPaint);
        } else {
            g2.setPaint(getItemPaint(row, column));
        }
        final Line2D line = new Line2D.Double();
        if (orientation == PlotOrientation.HORIZONTAL) {
            line.setLine(lowVal, x1, highVal, x1);
            g2.draw(line);
            line.setLine(lowVal, x1 - 5.0d, lowVal, x1 + 5.0d);
            g2.draw(line);
            line.setLine(highVal, x1 - 5.0d, highVal, x1 + 5.0d);
            g2.draw(line);
        } else { // PlotOrientation.VERTICAL
            line.setLine(x1, lowVal, x1, highVal);
            g2.draw(line);
            line.setLine(x1 - 5.0d, highVal, x1 + 5.0d, highVal);
            g2.draw(line);
            line.setLine(x1 - 5.0d, lowVal, x1 + 5.0d, lowVal);
            g2.draw(line);
        }

    }

    // draw the item label if there is one...
    if (isItemLabelVisible(row, column)) {
        if (orientation == PlotOrientation.HORIZONTAL) {
            drawItemLabel(g2, orientation, dataset, row, column, y1, x1, (meanValue.doubleValue() < 0.0));
        } else if (orientation == PlotOrientation.VERTICAL) {
            drawItemLabel(g2, orientation, dataset, row, column, x1, y1, (meanValue.doubleValue() < 0.0));
        }
    }

    // collect entity and tool tip information...
    if (state.getInfo() != null) {
        final EntityCollection entities = state.getEntityCollection();
        if (entities != null && shape != null) {
            String tip = null;
            final CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
            if (tipster != null) {
                tip = tipster.generateToolTip(dataset, row, column);
            }
            String url = null;
            if (getItemURLGenerator(row, column) != null) {
                url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
            }
            final CategoryItemEntity entity = new CategoryItemEntity(shape, tip, url, dataset, row,
                    dataset.getColumnKey(column), column);
            entities.add(entity);

        }

    }

}

From source file:com.pureinfo.srm.reports.impl.CategoryChartBuilder.java

/**
 * @param _format/*from   w  ww .  j a  v a  2  s  .  c  om*/
 * @param _dataset
 */
private void fillChartInfo(CategoryDataset _dataset, DecimalFormat _format) {
    int n = _dataset.getColumnCount();
    m_ChartInfo = new ChartInfo();
    m_ChartInfo.setChartTitle(m_sTitle);
    String[] labels = new String[n];
    for (int i = 0; i < n; i++) {
        labels[i] = "" + _dataset.getColumnKey(i);
        labels[i] += " = ";
        labels[i] += _format.format(_dataset.getValue(0, i));
    }
    m_ChartInfo.setLabels(labels);
    Point size = new Point(n > 20 ? ChartInfo.SIZE_WIDE : ChartInfo.SIZE_NORROW);
    if (n > 20)
        size.x += 40 * (n - 20);
    m_ChartInfo.setChartSize(size);
    m_ChartInfo.setLengedPosition(n > 20 ? ChartInfo.LENGEND_POSITION_BUTTOM : ChartInfo.LENGEND_POSITION_LEFT);
}

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

@Override
protected void chartDataTest(String name, int index, String[] categories, String[] series, Number[][] values) {
    CategoryDataset dataset = getChart(name, index).getCategoryPlot().getDataset();
    for (int i = 0; i < categories.length; i++) {
        for (int j = 0; j < series.length; j++) {
            Assert.assertEquals("chart data", values[i][j], dataset.getValue(j, i));
        }//from   w  w  w. ja  v a2s  .  co m
    }
}

From source file:net.sf.maltcms.common.charts.api.overlay.SelectionOverlay.java

private void updateCrosshairs(final Dataset ds, final int seriesIndex, final int itemIndex) {
    if (ds instanceof XYDataset) {
        XYDataset xyds = (XYDataset) ds;
        double x = xyds.getXValue(seriesIndex, itemIndex);
        double y = xyds.getYValue(seriesIndex, itemIndex);
        domainCrosshair.setValue(x);/*from   w  w  w  .  j av a  2 s .co m*/
        rangeCrosshair.setValue(y);
    } else if (ds instanceof CategoryDataset) {
        CategoryDataset cds = (CategoryDataset) ds;
        double y = cds.getValue(seriesIndex, itemIndex).doubleValue();
        domainCrosshair.setValue(itemIndex);
        rangeCrosshair.setValue(y);
    }
}

From source file:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot,
        CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataSet, int row, int column, int pass) {
    if (!getItemVisible(row, column)) {
        return;/*  ww  w.  j a  va2s .  com*/
    }

    Number value = dataSet.getValue(row, column);
    int visibleRow = state.getVisibleSeriesIndex(row);
    if ((value == null || visibleRow < 0)) {
        return;
    }

    LineFillItemRendererState rendererState = (LineFillItemRendererState) state;
    double currentValue = value.doubleValue();
    double currentItemXPoint = calculateItemXPoint(plot, dataSet, domainAxis, dataArea, column, visibleRow,
            state.getVisibleSeriesCount());
    double currentItemYPoint = calculateItemYPoint(plot, rangeAxis, dataArea, currentValue);

    if (isAreaAndLinePass(pass)) {
        processAreaAndLine(dataArea, plot, domainAxis, rangeAxis, dataSet, rendererState, row, column,
                currentValue, currentItemXPoint, currentItemYPoint, visibleRow);
    } else if (isShapesAndLabelsPass(pass)) {
        Shape entityArea = renderItemShapeAndLabel(g2, dataSet, row, column, plot.getOrientation(),
                currentValue, currentItemXPoint, currentItemYPoint);

        int dataSetIndex = plot.indexOf(dataSet);
        updateCrosshairValues(state.getCrosshairState(), dataSet.getRowKey(row), dataSet.getColumnKey(column),
                currentValue, dataSetIndex, currentItemXPoint, currentItemYPoint, plot.getOrientation());

        EntityCollection entities = state.getEntityCollection();
        if (entities != null) {
            addItemEntity(entities, dataSet, row, column, entityArea);
        }
    }

}

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

private int countPriorNonNullValues(CategoryDataset dataset, int column, int row) {
    if (row == 0) {
        return 0;
    }/* w  ww.ja  va 2s  .  c o m*/
    int count = 0;
    for (int r = 0; r < row; r++) {
        if (dataset.getValue(r, column) != null) {
            count++;
        }
    }
    return count;
}

From source file:net.sf.maltcms.common.charts.api.overlay.SelectionOverlay.java

private Shape generate(Dataset ds, int seriesIndex, int itemIndex) {
    if (ds instanceof XYDataset) {
        XYDataset xyds = (XYDataset) ds;
        double width = 10.0d;
        double height = 10.0d;
        double x = xyds.getXValue(seriesIndex, itemIndex) - (width / 2.0d);
        double y = xyds.getYValue(seriesIndex, itemIndex);
        Ellipse2D.Double e = new Ellipse2D.Double(x, y, width, height);
        return e;
    } else if (ds instanceof CategoryDataset) {
        CategoryDataset cds = (CategoryDataset) ds;
        double width = 10.0d;
        double height = 10.0d;
        double y = cds.getValue(seriesIndex, itemIndex).doubleValue();
        Ellipse2D.Double e = new Ellipse2D.Double(itemIndex, y, width, height);
        return e;
    }/*w ww  . j a  v  a 2  s. c  o m*/
    throw new IllegalArgumentException("Unsupported dataset type: " + ds.getClass());
}

From source file:uk.ac.lkl.cram.ui.chart.LearningExperienceChartMaker.java

/**
 * Create a chart from the provide category dataset
 * @return a Chart that can be rendered in a ChartPanel
 */// w  ww. j av a 2  s .  c o  m
@Override
protected JFreeChart createChart() {
    //Create a horizontal stacked bar chart from the chart factory, with no title, no axis labels, a legend, tooltips but no URLs
    JFreeChart chart = ChartFactory.createStackedBarChart(null, null, null, (CategoryDataset) dataset,
            PlotOrientation.HORIZONTAL, true, true, false);
    //Get the plot from the chart
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    //Remove offsets from the plot
    plot.setInsets(RectangleInsets.ZERO_INSETS);
    plot.setAxisOffset(RectangleInsets.ZERO_INSETS);
    //Hide the range lines
    plot.setRangeGridlinesVisible(false);
    //Get the renderer for the plot
    StackedBarRenderer sbRenderer = (StackedBarRenderer) plot.getRenderer();
    //Set the painter for the renderer (nothing fancy)
    sbRenderer.setBarPainter(new StandardBarPainter());
    //sbRenderer.setItemMargin(0.5); //Makes no difference
    //reduces width of bar as proportion of overall width
    sbRenderer.setMaximumBarWidth(0.5);
    //Render the bars as percentages
    sbRenderer.setRenderAsPercentages(true);
    //Set the colours for the bars
    sbRenderer.setSeriesPaint(0, PERSONALISED_COLOR);
    sbRenderer.setSeriesPaint(1, SOCIAL_COLOR);
    sbRenderer.setSeriesPaint(2, ONE_SIZE_FOR_ALL_COLOR);
    //Set the tooltips to render percentages
    sbRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator() {

        @Override
        public String generateToolTip(CategoryDataset cd, int row, int column) {
            //Only interested in row, as there's only one column
            //TODO--really inefficient
            @SuppressWarnings("unchecked")
            List<Comparable> rows = cd.getRowKeys();
            Comparable columnKey = cd.getColumnKey(column);
            //Sum running total
            int total = 0;
            for (Comparable comparable : rows) {
                total += cd.getValue(comparable, columnKey).intValue();
            }
            //Get the value for the row (in our case the learning type)
            Comparable rowKey = cd.getRowKey(row);
            float value = cd.getValue(rowKey, columnKey).floatValue();
            //The tooltip is the value of the learning type divided by the total, expressed as a percentage
            @SuppressWarnings("StringBufferWithoutInitialCapacity")
            StringBuilder builder = new StringBuilder();
            builder.append("<html><center>");
            builder.append(cd.getRowKey(row));
            builder.append(" (");
            builder.append(FORMATTER.format(value / total));
            builder.append(")<br/>");
            builder.append("Double-click for more");
            return builder.toString();
        }
    });
    //Hide both axes
    CategoryAxis categoryAxis = plot.getDomainAxis();
    //categoryAxis.setCategoryMargin(0.5D);//Makes no difference
    categoryAxis.setVisible(false);
    NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
    numberAxis.setVisible(false);
    return chart;
}

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.//ww w.  j a  v a 2s .co 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;

}