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

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

Introduction

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

Prototype

public int getRowCount();

Source Link

Document

Returns the number of rows in the table.

Usage

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

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {//from  ww  w .ja  va2 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  ww  .  jav a 2  s  . c  om*/
        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:storybook.ui.chart.jfreechart.ChartUtil.java

public static void setNiceSeriesColors(CategoryDataset paramCategoryDataset,
        AbstractRenderer paramAbstractRenderer) {
    Color[] arrayOfColor = ColorUtil.getDarkColors(ColorUtil.getPastelColors(), 0.35D);
    for (int i = 0; i < paramCategoryDataset.getRowCount(); i++) {
        Color localColor = arrayOfColor[(i % arrayOfColor.length)];
        paramAbstractRenderer.setSeriesPaint(i, localColor);
    }/*from ww w.  j av a2  s . c om*/
}

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>/* w  w  w .j a  v  a 2 s.c  o  m*/
 * 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:net.sf.maltcms.common.charts.api.ChartCustomizer.java

/**
 *
 * @param plot//  w  w w .  j a v a2  s. com
 * @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.common.charts.api.ChartCustomizer.java

/**
 *
 * @param plot//from w  ww .java2  s. c om
 * @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 w  ww .  java2  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.chromaui.charts.ChartCustomizer.java

/**
 *
 * @param plot/*from   ww  w .j av  a 2 s.  c o  m*/
 * @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:org.talend.dataprofiler.chart.ChartDecorator.java

/**
 * DOC bZhou Comment method "decorate".//from ww  w  .j  a  v a2 s  . c o m
 * 
 * @param chart
 */
public static void decorate(JFreeChart chart, PlotOrientation orientation) {
    if (chart != null) {
        // TDQ-11522: Set white background on charts in the editors
        chart.setBackgroundPaint(Color.white);
        // TDQ-11522~
        Plot plot = chart.getPlot();
        if (plot instanceof CategoryPlot) {
            decorateCategoryPlot(chart, orientation);

            CategoryDataset dataset = chart.getCategoryPlot().getDataset();
            int rowCount = dataset != null ? dataset.getRowCount() : 20;
            for (int i = 0; i < rowCount; i++) {
                // by zshen bug 14173 add the color in the colorList when chart need more the color than 8.
                if (i >= COLOR_LIST.size()) {
                    COLOR_LIST.add(generateRandomColor(COLOR_LIST));
                }
                // ~14173
                ((CategoryPlot) plot).getRenderer().setSeriesPaint(i, COLOR_LIST.get(i));
            }

        } else if (plot instanceof XYPlot) {
            decorateXYPlot(chart);

            int count = chart.getXYPlot().getDataset().getSeriesCount();
            for (int i = 0; i < count; i++) {
                // by zshen bug 14173 add the color in the colorList when chart need the colors more than 8.
                if (i >= COLOR_LIST.size()) {
                    COLOR_LIST.add(generateRandomColor(COLOR_LIST));
                }
                // ~14173
                ((XYPlot) plot).getRenderer().setSeriesPaint(i, COLOR_LIST.get(i));
            }
        } else if (plot instanceof PiePlot) {
            decoratePiePlot(chart);

            // ADD msjian TDQ-8046 2013-10-17: add the color's control for pie chart
            PieDataset piedataset = ((PiePlot) plot).getDataset();
            for (int i = 0; i < piedataset.getItemCount(); i++) {
                if (i >= PIE_COLOR_LIST.size()) {
                    PIE_COLOR_LIST.add(generateRandomColor(PIE_COLOR_LIST));
                }
                Comparable<?> key = piedataset.getKey(i);
                ((PiePlot) plot).setSectionPaint(key, PIE_COLOR_LIST.get(i));
            }
            // TDQ-8046~
        }
    }
}

From source file:org.talend.dataprofiler.chart.ChartDecorator.java

/**
 * DOC bZhou Comment method "decorateCategoryPlot".
 * /*from   ww  w . jav a  2 s .co  m*/
 * @param chart
 */
public static void decorateCategoryPlot(JFreeChart chart, PlotOrientation orientation) {

    CategoryPlot plot = chart.getCategoryPlot();
    CategoryItemRenderer render = plot.getRenderer();
    CategoryAxis domainAxis = plot.getDomainAxis();
    // ADD msjian TDQ-5111 2012-4-9: set something look it well
    domainAxis.setCategoryMargin(0.1);
    domainAxis.setUpperMargin(0.05);
    domainAxis.setLowerMargin(0.05);
    domainAxis.setCategoryLabelPositionOffset(10);
    // TDQ-5111~

    ValueAxis valueAxis = plot.getRangeAxis();

    Font font = new Font("Tahoma", Font.BOLD, BASE_ITEM_LABEL_SIZE);//$NON-NLS-1$

    render.setBaseItemLabelFont(font);
    // MOD zshen 10998: change the font name 2010-01-16
    font = new Font("sans-serif", Font.BOLD, BASE_LABEL_SIZE);//$NON-NLS-1$
    domainAxis.setLabelFont(font);

    font = new Font("sans-serif", Font.BOLD, BASE_LABEL_SIZE);//$NON-NLS-1$
    valueAxis.setLabelFont(font);

    font = new Font("sans-serif", Font.PLAIN, BASE_TICK_LABEL_SIZE);//$NON-NLS-1$
    domainAxis.setTickLabelFont(font);
    valueAxis.setTickLabelFont(font);

    setLegendFont(chart);

    font = new Font("sans-serif", Font.BOLD, BASE_TITLE_LABEL_SIZE);//$NON-NLS-1$
    TextTitle title = chart.getTitle();
    if (title != null) {
        title.setFont(font);
    }

    font = null;

    if (render instanceof BarRenderer) {
        CategoryDataset dataset = chart.getCategoryPlot().getDataset();
        if (dataset != null) {
            int rowCount = dataset.getRowCount();
            List<?> columnKeys = dataset.getColumnKeys();
            if (!isContainCJKCharacter(columnKeys.toArray())) {
                domainAxis.setTickLabelFont(new Font("Tahoma", Font.PLAIN, 10));//$NON-NLS-1$
            }
            ((BarRenderer) render).setItemMargin(-0.40 * rowCount);

            // TDQ-12621 add Tooltip for Lable
            for (Object colKey : columnKeys) {
                domainAxis.addCategoryLabelToolTip(colKey.toString(), colKey.toString());
            }
        }
        domainAxis.setUpperMargin(0.1);
        // TDQ-12621 Only display in 1 line for the label, other chars will be displayed as "..."
        domainAxis.setMaximumCategoryLabelLines(1);

        // ADD msjian TDQ-5111 2012-4-9: set Bar Width and let it look well
        // not do this when the bar is horizontal Orientation
        if (orientation == null) {
            ((BarRenderer) render).setMaximumBarWidth(0.2);
        }
        // TDQ-5111~
    }
    // ~10998
}