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

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

Introduction

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

Prototype

public Comparable getColumnKey(int column);

Source Link

Document

Returns the column key for a given index.

Usage

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

private static GridPointInfo getGridPointInfo(CategoryItemEntity en, Chart chart) {
    ChartModel model = chart.getModel();
    if (model == null)
        return null;

    GridPointInfo info = new GridPointInfoImpl();
    info.setSeries(new SeriesInfoImpl());
    info.getSeries().setIndex(en.getSeries());

    ModelInfo modelInfo = new ModelInfo(model);

    if (!modelInfo.isDataEmpty()) {
        CategoryDataset ds = ModelConverter.toCategoryDataset(modelInfo);
        Comparable seriesKey = ds.getRowKey(en.getSeries());
        Comparable tupleKey = ds.getColumnKey(en.getCategoryIndex());
        Object tupleValue = ds.getValue(seriesKey, tupleKey);
        info.setIndex(en.getCategoryIndex());
        info.setKey(tupleKey);/*from w  w  w  .j  a va  2 s . co  m*/
        info.setValue(tupleValue);
        info.getSeries().setKey(seriesKey);
    }

    return info;
}

From source file:soap.ui.stats.OverLaidBarChartFactory.java

public static JFreeChart createDualBarAndLineDiagram(String title, String categoryAxisLabel,
        String valueAxisLabel, CategoryDataset barDiagramDataset, LinkedList lineDiagramDatasets) {
    CategoryPlot plot = new CategoryPlot();
    int inc = 0;/*from   w ww.  j  a  va  2s  .  c o m*/
    BarRenderer barRenderer = null;
    if (barDiagramDataset != null) {
        plot.setDataset(barDiagramDataset);
        barRenderer = new BarRenderer3D();
        barRenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        plot.setRenderer(barRenderer);
        inc = 1;
        plot.setDomainAxis(new CategoryAxis3D(categoryAxisLabel));
        plot.setRangeAxis(new NumberAxis3D(valueAxisLabel));
    } else {
        plot.setDomainAxis(new CategoryAxis(categoryAxisLabel));
        plot.setRangeAxis(new NumberAxis(valueAxisLabel));
    }
    plot.mapDatasetToRangeAxis(0, 0);

    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

    CategoryItemRenderer renderers[] = new LineAndShapeRenderer[lineDiagramDatasets.size()];

    for (int i = 0; i < lineDiagramDatasets.size(); i++) {
        CategoryDataset lineDiagramDataset = (CategoryDataset) lineDiagramDatasets.get(i);
        plot.setDataset(i + inc, lineDiagramDataset);
        plot.mapDatasetToRangeAxis(i + inc, 0);
        if (barDiagramDataset != null && i < barDiagramDataset.getRowCount()) {
            renderers[i] = new LineAndShapeRendererMapToBar(barRenderer, i);
        } else {
            renderers[i] = new LineAndShapeRenderer();
        }
        renderers[i].setToolTipGenerator(new StandardCategoryToolTipGenerator());
        renderers[i].setSeriesStroke(0, new BasicStroke(1.5f));
        renderers[i].setSeriesStroke(1, new BasicStroke(1.5f));
        plot.setRenderer(i + inc, renderers[i]);
    }

    CategoryDataset dataset;
    if (lineDiagramDatasets.size() > 0)
        dataset = (CategoryDataset) lineDiagramDatasets.get(0);
    else
        dataset = barDiagramDataset;
    for (int i = 0; i < dataset.getColumnCount(); i++)
        plot.getDomainAxis().addCategoryLabelToolTip(dataset.getColumnKey(i), (String) dataset.getColumnKey(i));

    plot.setRangeGridlinePaint(Color.black);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    JFreeChart chart = new JFreeChart(plot);
    chart.setTitle(title);
    chart.setLegend(new StandardLegend());

    return chart;
}

From source file:hudson.graph.jfreechart.Utils.java

/**
 * Adjusts the Y-axis so that abnormally large value won't spoil the whole chart
 * by making everything look virtually 0.
 *
 * <p>//from w w w.  jav  a 2  s . com
 * The algorithm is based on <a href="http://en.wikipedia.org/wiki/Chebyshev%27s_inequality">Chebyshev's inequality</a>,
 * which states that given any number sequence, nore more than 1/(N^2) values are more than N x stddev away
 * from the average.
 *
 * <p>
 * So the algorithm is to set Y-axis range so that we can see all data points that are within N x stddev
 * of the average. Most of the time, Cebyshev's inequality is very conservative, so it shouldn't do
 * much harm.
 *
 * <p>
 * When the algorithm does kick in, however, we can kick out at most 1 in N^2 data points.
 * (So for example if N=3 then we can "fix" the graph as long as we only have less than 1/(3*3)=11.111...% bad data.
 *
 * <p>
 * Also see issue #1246.
 */
public static void adjustChebyshev(CategoryDataset dataset, NumberAxis yAxis) {
    // first compute E(X) and Var(X)
    double sum = 0, sum2 = 0;

    final int nColumns = dataset.getColumnCount();
    final int nRows = dataset.getRowCount();
    for (int i = 0; i < nRows; i++) {
        Comparable rowKey = dataset.getRowKey(i);
        for (int j = 0; j < nColumns; j++) {
            Comparable columnKey = dataset.getColumnKey(j);

            double n = dataset.getValue(rowKey, columnKey).doubleValue();
            sum += n;
            sum2 += n * n;
        }
    }

    double average = sum / (nColumns * nRows);
    double stddev = Math.sqrt(sum2 / (nColumns * nRows) - average * average);

    double rangeMin = average - stddev * CHEBYSHEV_N;
    double rangeMax = average + stddev * CHEBYSHEV_N;

    // now see if there are any data points that fall outside (rangeMin,rangeMax)
    boolean found = false;
    double min = 0, max = 0;
    for (int i = 0; i < nRows; i++) {
        Comparable rowKey = dataset.getRowKey(i);
        for (int j = 0; j < nColumns; j++) {
            Comparable columnKey = dataset.getColumnKey(j);

            double n = dataset.getValue(rowKey, columnKey).doubleValue();
            if (n < rangeMin || rangeMax < n) {
                found = true;
                continue; // ignore this value
            }

            min = Math.min(min, n);
            max = Math.max(max, n);
        }
    }

    if (!found)
        return; // no adjustment was necessary

    // some values fell outside the range, so adjust the Y-axis

    // if we are ever to extend this method to handle negative value ranges correctly,
    // the code after this needs modifications

    min = Math.min(0, min); // always include 0 in the graph
    max += yAxis.getUpperMargin() * (max - min);

    yAxis.setRange(min, max);
}

From source file:playground.benjamin.scenarios.zurich.analysis.charts.BkChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*from www.  j  ava 2 s  .  c  o m*/
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                for (int seriesIndex = 0; seriesIndex < xyds.getSeriesCount(); seriesIndex++) {
                    writer.newLine();
                    writer.write("Series " + "'" + xyds.getSeriesKey(seriesIndex).toString());
                    writer.write("'");
                    writer.newLine();
                    int items = xyds.getItemCount(seriesIndex);
                    for (int itemsIndex = 0; itemsIndex < items; itemsIndex++) {
                        Number xValue = xyds.getX(seriesIndex, itemsIndex);
                        Number yValue = xyds.getY(seriesIndex, itemsIndex);
                        writer.write(xValue.toString());
                        writer.write("\t");
                        writer.write(yValue.toString());
                        writer.newLine();
                    }
                }
            }
            System.out.println("Table written to : " + filename + "\n"
                    + "==================================================");

        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("caught class cast exception, trying to write CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:playground.dgrether.analysis.charts.utils.DgChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {//from w  w  w.  ja  v a  2 s. com
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = "#" + xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            //write the header
            writer.write("#");
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                int seriesIndex = 0;
                int maxItems = 0;
                int seriesCount = xyds.getSeriesCount();
                while (seriesIndex < seriesCount) {
                    writer.write("Series " + xyds.getSeriesKey(seriesIndex).toString());
                    if (seriesIndex < seriesCount - 1) {
                        writer.write("\t \t");
                    }
                    if (xyds.getItemCount(seriesIndex) > maxItems) {
                        maxItems = xyds.getItemCount(seriesIndex);
                    }
                    seriesIndex++;
                }
                writer.newLine();

                //write the data
                Number xValue, yValue = null;
                for (int itemsIndex = 0; itemsIndex < maxItems; itemsIndex++) {
                    for (int seriesIdx = 0; seriesIdx < seriesCount; seriesIdx++) {
                        if (seriesIdx < xyds.getSeriesCount() && itemsIndex < xyds.getItemCount(seriesIdx)) {
                            xValue = xyds.getX(seriesIdx, itemsIndex);
                            yValue = xyds.getY(seriesIdx, itemsIndex);
                            if (xValue != null && yValue != null) {
                                writer.write(xValue.toString());
                                writer.write("\t");
                                writer.write(yValue.toString());
                                if (seriesIdx < seriesCount - 1) {
                                    writer.write("\t");
                                }
                            }
                        }
                    }
                    writer.newLine();
                }
            }
        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("Due to a caught class cast exception, it should be a CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "# CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
        log.info("Chart data written to: " + filename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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

/**
 *
 * @param chartPanel/*from  w w w.  j  av a  2  s  . c  o m*/
 * @param category
 * @param y
 * @return
 */
public static AffineTransform getModelToViewTransformCategory(ChartPanel chartPanel, int category, double y) {
    double zoomX = chartPanel.getScaleX();
    double zoomY = chartPanel.getScaleY();
    Insets insets = chartPanel.getInsets();
    AffineTransform at = getTranslateInstance(insets.left, insets.top);
    at.concatenate(getScaleInstance(zoomX, zoomY));
    Plot plot = chartPanel.getChart().getPlot();
    if (plot instanceof CategoryPlot) {
        CategoryPlot xyp = (CategoryPlot) plot;
        CategoryDataset cds = xyp.getDataset();
        RectangleEdge xAxisLocation = xyp.getDomainAxisEdge();
        RectangleEdge yAxisLocation = xyp.getRangeAxisEdge();
        PlotOrientation orientation = xyp.getOrientation();
        Comparable<?> categoryKey = cds.getColumnKey(category);
        Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
        double transX = xyp.getDomainAxis().getCategoryMiddle(categoryKey, cds.getColumnKeys(), dataArea,
                xAxisLocation);
        double transY = xyp.getRangeAxis().valueToJava2D(y, dataArea, yAxisLocation);
        if (orientation == PlotOrientation.HORIZONTAL) {
            double tmp = transX;
            transX = transY;
            transY = tmp;
        }
        at.concatenate(getTranslateInstance(transX, transY));
        return at;
    }
    throw new IllegalArgumentException("Unsupported plot type: " + plot.getClass());
}

From source file:org.talend.dataprofiler.chart.util.TopChartFactory.java

private static double sumItemCount(CategoryDataset categorydataset) {
    double itemCount = 0;
    for (int i = 0; i < categorydataset.getColumnCount(); i++) {
        int columnKey = Integer.valueOf(categorydataset.getColumnKey(i).toString());
        itemCount += categorydataset.getValue(0, i).intValue() * columnKey;
    }//from   w  w w .ja v a2s .co  m
    return itemCount;
}

From source file:ca.sqlpower.wabit.swingui.chart.ChartSwingUtil.java

/**
 * Sets the colours and gradients to be used when painting the given JFreeChart.
 * /*  ww  w . ja  v a 2  s.c  o m*/
 * @param chart
 *          The JFreeChart to make nice.
 */
public static void makeChartNice(JFreeChart chart) {
    Plot plot = chart.getPlot();
    chart.setBackgroundPaint(null);
    chart.setBorderStroke(new BasicStroke(1f));
    chart.setBorderPaint(new Color(0xDDDDDD));
    chart.setBorderVisible(true);

    // TODO Should we add an option for subtitles, this is where it would go.
    //        TextTitle subTitle = new TextTitle("What's up doc?",
    //             new Font("SansSerif", Font.BOLD, 8));
    //       chart.addSubtitle(subTitle);

    // overall plot
    plot.setOutlinePaint(null);
    plot.setInsets(new RectangleInsets(0, 5, 0, 5)); // also the overall chart panel
    plot.setBackgroundPaint(null);
    plot.setDrawingSupplier(new WabitDrawingSupplier());

    // legend
    LegendTitle legend = chart.getLegend();
    if (legend != null) {
        legend.setBorder(0, 0, 0, 0);
        legend.setBackgroundPaint(null);
        legend.setPadding(2, 2, 2, 2);
    }

    if (plot instanceof CategoryPlot) {
        CategoryPlot cplot = (CategoryPlot) plot;

        CategoryItemRenderer renderer = cplot.getRenderer();
        if (renderer instanceof BarRenderer) {
            BarRenderer brenderer = (BarRenderer) renderer;

            brenderer.setBarPainter(new StandardBarPainter());
            brenderer.setDrawBarOutline(false);
            brenderer.setShadowVisible(false);

            brenderer.setGradientPaintTransformer(
                    new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL));

        } else if (renderer instanceof LineAndShapeRenderer) {
            // it's all taken care of by WabitDrawingSupplier

        } else {
            logger.warn("I don't know how to make " + renderer + " pretty. Leaving ugly.");
        }

        cplot.setRangeGridlinePaint(Color.BLACK);
        cplot.setRangeGridlineStroke(GRIDLINE_STROKE);

        // axes
        for (int i = 0; i < cplot.getDomainAxisCount(); i++) {
            CategoryAxis axis = cplot.getDomainAxis(i);
            axis.setAxisLineVisible(false);
        }

        for (int i = 0; i < cplot.getRangeAxisCount(); i++) {
            ValueAxis axis = cplot.getRangeAxis(i);
            axis.setAxisLineVisible(false);
        }
    }

    if (plot instanceof MultiplePiePlot) {
        MultiplePiePlot mpplot = (MultiplePiePlot) plot;
        JFreeChart pchart = mpplot.getPieChart();
        PiePlot3DGradient pplot = (PiePlot3DGradient) pchart.getPlot();
        pplot.setBackgroundPaint(null);
        pplot.setOutlinePaint(null);

        pplot.setFaceGradientPaintTransformer(
                new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL));
        pplot.setSideGradientPaintTransformer(
                new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL));

        CategoryDataset data = mpplot.getDataset();
        Color[][] colours = WabitDrawingSupplier.SERIES_COLOURS;

        //Set all colours
        for (int i = 0; i < colours.length; i++) {
            if (data.getColumnCount() >= i + 1) {
                pplot.setSectionOutlinePaint(data.getColumnKey(i), null);
                GradientPaint gradient = new GradientPaint(0, 0f, colours[i][0], 100, 0f, colours[i][1]);
                pplot.setSectionPaint(data.getColumnKey(i), gradient);
                gradient = new GradientPaint(0, 0f, colours[i][1], 100, 0f, colours[i][0]);
                pplot.setSidePaint(data.getColumnKey(i), gradient);
            }
        }
    }

    // Tweak the title font size
    chart.getTitle().setFont(chart.getTitle().getFont().deriveFont(14f));
    chart.getTitle().setPadding(5, 0, 5, 0);
    chart.setAntiAlias(true);

    // shrink padding
    chart.setPadding(new RectangleInsets(0, 0, 0, 0));
}

From source file:org.pau.assetmanager.viewmodel.chart.PrepareChart.java

public static void prepareJFreeBarChart(JFreeChart jfchart, List<PropertyBook> listOfPropertties,
        CategoryDataset categoryModel) {

    CategoryPlot categoryPlot = ((CategoryPlot) jfchart.getPlot());
    categoryPlot.getRangeAxis().resizeRange(1.2);
    categoryPlot.setBackgroundPaint(Color.WHITE);
    categoryPlot.setDomainGridlinePaint(Color.WHITE);
    categoryPlot.setRangeMinorGridlinePaint(Color.WHITE);
    categoryPlot.setRangeGridlinePaint(Color.BLACK);
    BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();

    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseItemLabelsVisible(false);
    for (int i = 0; i < listOfPropertties.size(); i++) {
        renderer.setSeriesStroke(i, new BasicStroke(1));
    }/*  w  w w . j  a va  2s .c o m*/
    for (int i = 0; i < categoryModel.getColumnKeys().size(); i++) {
        String label = (String) categoryModel.getColumnKey(i);
        CategoryMarker marker = new CategoryMarker(label);
        marker.setLabel("");
        marker.setPaint(Color.cyan);
        marker.setOutlinePaint(Color.cyan);
        marker.setAlpha(0.1f);
        marker.setLabelAnchor(RectangleAnchor.TOP);
        marker.setLabelTextAnchor(TextAnchor.TOP_CENTER);
        marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);
        categoryPlot.addDomainMarker(marker, Layer.BACKGROUND);
    }
    renderer.setDrawBarOutline(false);
    renderer.setShadowVisible(false);
    renderer.setItemMargin(.1);
    renderer.setBarPainter(new StandardBarPainter());

}

From source file:com.googlecode.logVisualizer.chart.perDayConsumption.ConsumptionLableGenerator.java

public String generateColumnLabel(final CategoryDataset dataset, final int column) {
    return dataset.getColumnKey(column).toString();
}