Example usage for org.jfree.chart.plot CategoryPlot getDataset

List of usage examples for org.jfree.chart.plot CategoryPlot getDataset

Introduction

In this page you can find the example usage for org.jfree.chart.plot CategoryPlot getDataset.

Prototype

public CategoryDataset getDataset(int index) 

Source Link

Document

Returns the dataset with the given index, or null if there is no dataset.

Usage

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

/**
 *
 * @param plot//from ww  w .ja v  a 2  s.  c o m
 * @param alpha
 */
public static void setSeriesColors(CategoryPlot plot, float alpha) {

    int datasets = plot.getDatasetCount();
    for (int i = 0; i < datasets; i++) {
        CategoryDataset ds = plot.getDataset(i);
        CategoryItemRenderer renderer = plot.getRenderer(i);
        //            System.out.println("Dataset has " + ds.getRowCount() + " rows");
        for (int j = 0; j < ds.getRowCount(); j++) {
            renderer.setSeriesPaint(j, withAlpha(plotColors[j % plotColors.length], alpha));
        }
    }
}

From source file:net.sf.maltcms.chromaui.charts.ChartCustomizer.java

/**
 *
 * @param plot//from www  .  j  a  v a 2  s .c  o m
 * @param alpha
 */
public static void setSeriesColors(CategoryPlot plot, float alpha) {

    int datasets = plot.getDatasetCount();
    for (int i = 0; i < datasets; i++) {
        CategoryDataset ds = plot.getDataset(i);
        CategoryItemRenderer renderer = plot.getRenderer(i);
        Logger.getLogger(ChartCustomizer.class.getName()).log(Level.INFO, "Dataset has {0} rows",
                ds.getRowCount());
        Logger.getLogger(ChartCustomizer.class.getName()).log(Level.INFO, "Dataset has {0} columns",
                ds.getColumnCount());
        for (int j = 0; j < ds.getRowCount(); j++) {
            renderer.setSeriesPaint(j, withAlpha(plotColors[j % plotColors.length], alpha));
        }
    }
}

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

/**
 *
 * @param plot/*from   w w w .j a v  a  2  s. c  om*/
 * @param alpha
 * @param colors
 */
public static void setSeriesColors(CategoryPlot plot, float alpha, List<Color> colors) {
    int datasets = plot.getDatasetCount();
    for (int i = 0; i < datasets; i++) {
        CategoryDataset ds = plot.getDataset(i);
        CategoryItemRenderer renderer = plot.getRenderer(i);
        //            System.out.println("Dataset has " + ds.getRowCount() + " rows");
        for (int j = 0; j < ds.getRowCount(); j++) {
            renderer.setSeriesPaint(j, withAlpha(colors.get(j), alpha));
        }
    }
}

From source file:net.sf.maltcms.chromaui.charts.ChartCustomizer.java

/**
 *
 * @param plot//from   w w w.j av a 2s . c  om
 * @param alpha
 * @param colors
 */
public static void setSeriesColors(CategoryPlot plot, float alpha, List<Color> colors) {
    int datasets = plot.getDatasetCount();
    for (int i = 0; i < datasets; i++) {
        CategoryDataset ds = plot.getDataset(i);
        CategoryItemRenderer renderer = plot.getRenderer(i);
        Logger.getLogger(ChartCustomizer.class.getName()).log(Level.INFO, "Dataset has {0} rows",
                ds.getRowCount());
        Logger.getLogger(ChartCustomizer.class.getName()).log(Level.INFO, "Dataset has {0} columns",
                ds.getColumnCount());
        for (int j = 0; j < ds.getRowCount(); j++) {
            //                if (ds.getRowCount() != colors.size()) {
            //                    throw new IllegalArgumentException("Number of datasets and number of colors must be equal!");
            //                }
            renderer.setSeriesPaint(j, withAlpha(colors.get(j), alpha));
        }
    }
}

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

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*from  w  w w .  j  a  v a  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 {/* w w w  .  j  av  a 2s  . 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:com.newatlanta.bluedragon.CustomBarRenderer.java

/**
 * Returns a legend item for a series.//w  ww. j a v a2s .c o m
 * 
 * @param datasetIndex
 *          the dataset index (zero-based).
 * @param series
 *          the series index (zero-based).
 * 
 * @return The legend item.
 */
public LegendItem getLegendItem(int datasetIndex, int series) {

    CategoryPlot cp = getPlot();
    if (cp == null) {
        return null;
    }

    CategoryDataset dataset;
    dataset = cp.getDataset(datasetIndex);
    String label = getLegendItemLabelGenerator().generateLabel(dataset, series);
    String description = label;
    String toolTipText = null;
    if (getLegendItemToolTipGenerator() != null) {
        toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
    }
    String urlText = null;
    if (getLegendItemURLGenerator() != null) {
        urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
    }
    Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0);
    Paint paint = getSeriesPaint(series);
    Paint outlinePaint = getSeriesOutlinePaint(series);
    Stroke outlineStroke = getSeriesOutlineStroke(series);

    // This is the fix for bug #2695
    if (paint instanceof java.awt.GradientPaint) {
        // When the paintstyle is "shade" use the lighter
        // color while with "light" use the darker color
        // NOTE: if we take the lighter color (Color2) and make it darker
        // and it equals the darker color (Color1) then the paintstyle
        // is "shade".
        GradientPaint gp = ((GradientPaint) paint);
        if (cfCHART.getDarkerColor(gp.getColor2()).equals(gp.getColor1()))
            paint = gp.getColor2(); // the lighter color
        else
            paint = gp.getColor1(); // the darker color
    }

    return new LegendItem(label, description, toolTipText, urlText, true, shape, true, paint,
            isDrawBarOutline(), outlinePaint, outlineStroke, false, new Line2D.Float(), new BasicStroke(1.0f),
            Color.black);
}

From source file:be.ugent.maf.cellmissy.gui.view.renderer.jfreechart.ExtendedBoxAndWhiskerRenderer.java

/**
 * Returns a legend item for a series./*from   w ww. j  av a 2s . co m*/
 *
 * @param datasetIndex the dataset index (zero-based).
 * @param series the series index (zero-based).
 *
 * @return The legend item.
 */
@Override
public LegendItem getLegendItem(int datasetIndex, int series) {

    CategoryPlot cp = getPlot();
    if (cp == null) {
        return null;
    }

    CategoryDataset dataset;
    dataset = cp.getDataset(datasetIndex);
    dataset.getRowCount();

    String label = getLegendItemLabelGenerator().generateLabel(dataset, series);
    String toolTipText = null;
    if (getLegendItemToolTipGenerator() != null) {
        toolTipText = getLegendItemToolTipGenerator().generateLabel(dataset, series);
    }
    String urlText = null;
    if (getLegendItemURLGenerator() != null) {
        urlText = getLegendItemURLGenerator().generateLabel(dataset, series);
    }
    Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0);

    // set the paint according to the right technical replicate
    int length = GuiUtils.getAvailableColors().length;
    int colorIndex = series % length;

    Paint paint = GuiUtils.getAvailableColors()[colorIndex];
    Stroke outlineStroke = getItemOutlineStroke(datasetIndex, series);

    return new LegendItem(label, label, toolTipText, urlText, shape, paint, outlineStroke, paint);
}

From source file:org.jfree.eastwood.ChartEngine.java

/**
 * The 'ewtr' tag is an Eastwood extension that draws a trend line over a
 * chart, using data that has been added to a secondary dataset using the
 * 'ewd2' tag.//from  w  ww. j av a2  s .  c  o m
 *
 * @param params  the chart parameters;
 * @param chart  the chart under construction (will be updated by this
 *         method if necessary).
 */
public static void processEWTR(Map params, JFreeChart chart) {
    // the 'ewtr' arguments are:
    // - <seriesIndex> : the index of the series in the secondary dataset;
    // - <colour> : the colour;
    // - <lineThickness> : the line thickness;
    String[] ewtrStr = (String[]) params.get("ewtr");
    if (ewtrStr != null) {
        String[] atts = ewtrStr[0].split(",");
        int series = Integer.parseInt(atts[0]);
        Color color = parseColor(atts[1]);
        float lineWidth = Float.parseFloat(atts[2]);
        Plot plot = chart.getPlot();
        if (plot instanceof CategoryPlot) {
            CategoryPlot cp = (CategoryPlot) plot;
            if (cp.getDataset(1) != null) {
                LineAndShapeRenderer r = new LineAndShapeRenderer(true, false);
                r.setBaseSeriesVisible(false);
                r.setSeriesVisible(series, Boolean.TRUE);
                r.setSeriesPaint(series, color);
                r.setSeriesStroke(series, new BasicStroke(lineWidth));
                cp.setRenderer(1, r);

                cp.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
            }
        } else if (plot instanceof XYPlot) {
            XYPlot xp = (XYPlot) plot;
            if (xp.getDataset(1) != null) {
                XYLineAndShapeRenderer r = new XYLineAndShapeRenderer(true, false);
                r.setBaseSeriesVisible(false);
                r.setSeriesVisible(series, Boolean.TRUE);
                r.setSeriesPaint(series, color);
                r.setSeriesStroke(series, new BasicStroke(lineWidth));
                xp.setRenderer(1, r);
                xp.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
            }
        }
    }

}

From source file:de.fhbingen.wbs.wpOverview.tabs.AvailabilityGraphAction.java

/**
 * Add the action listener to the graph.
 */// www .  j  a  v a2 s .  c  o  m
private void setAction() {
    gui.pnlGraph.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseClicked(final ChartMouseEvent e) {
            if (e.getEntity() instanceof PlotEntity) {
                Point2D p = gui.pnlGraph.translateScreenToJava2D(e.getTrigger().getPoint());
                CategoryPlot plot = (CategoryPlot) gui.pnlGraph.getChart().getPlot();
                Rectangle2D plotArea = gui.pnlGraph.getScreenDataArea();

                DateAxis rangeAxis = (DateAxis) plot.getRangeAxis();
                RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();

                CategoryAxis catAxis = plot.getDomainAxis();
                RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();

                double chartY = rangeAxis.java2DToValue(p.getX(), plotArea, rangeAxisEdge);

                CategoryDataset categories = (CategoryDataset) plot.getDataset(0);

                int categoryCount = categories.getColumnCount();

                for (int i = 0; i < categoryCount; i++) {
                    double catStart = catAxis.getCategoryStart(i, categoryCount, plotArea, domainAxisEdge);
                    double catEnd = catAxis.getCategoryEnd(i, categoryCount, plotArea, domainAxisEdge);

                    if (e.getTrigger().getY() >= catStart && e.getTrigger().getY() < catEnd) {
                        new EditAvailabilityController(gui.function, gui.function.getWorkers().get(i),
                                new Day(new Date((long) chartY)), parent);
                    }

                }
            } else {

                CategoryItemEntity item = (CategoryItemEntity) e.getEntity();
                CategoryPlot plot = (CategoryPlot) gui.pnlGraph.getChart().getPlot();
                Rectangle2D plotArea = gui.pnlGraph.getScreenDataArea();
                RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();

                DateAxis dateAxis = (DateAxis) plot.getRangeAxis();

                double d = dateAxis.java2DToValue(e.getEntity().getArea().getBounds2D().getX(), plotArea,
                        rangeAxisEdge);
                double d2 = dateAxis.java2DToValue(e.getEntity().getArea().getBounds2D().getX()
                        + e.getEntity().getArea().getBounds2D().getWidth(), plotArea, rangeAxisEdge);

                Date startDate = new Date((long) d);
                Date endDate = new Date((long) d2);

                CategoryDataset categories = (CategoryDataset) plot.getDataset(0);
                int workerIndex = categories.getColumnIndex(item.getColumnKey());
                Worker worker = gui.function.getWorkers().get(workerIndex);

                Set<Availability> found = CalendarService.getAllWorkerAvailability(worker.getId(), startDate,
                        endDate);
                Availability foundAv = found.toArray(new Availability[1])[0];

                if (foundAv != null) {
                    new EditAvailabilityController(gui.function, foundAv, parent);
                } else {
                    found = CalendarService.getProjectAvailability(startDate, endDate);
                    foundAv = found.toArray(new Availability[1])[0];
                    if (foundAv != null) {
                        new EditAvailabilityController(gui.function, foundAv, parent);
                    } else {
                        JOptionPane.showMessageDialog(
                                new JFrame(LocalizedStrings.getGeneralStrings().warning()),
                                LocalizedStrings.getErrorMessages().availabilityCanNotBeChanged());
                    }
                }
            }

        }

        @Override
        public void chartMouseMoved(final ChartMouseEvent arg0) {

        }

    });
    /**
     * ActionListener
     */
    gui.btnNext.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            gui.function.increment();
        }

    });
    /**
     * ActionListener
     */
    gui.btnPrev.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            gui.function.decrement();
        }

    });

    for (int i = 0; i < gui.buttons.length; i++) {
        addButtonListener(i);
    }
    /**
     * ActionListener
     */
    gui.btnManualAv.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                gui.function.setManualAv(true);
            } else {
                gui.function.setManualAv(false);
            }
        }

    });

}