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

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

Introduction

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

Prototype

public int getColumnCount();

Source Link

Document

Returns the number of columns in the table.

Usage

From source file:net.sourceforge.processdash.ui.web.reports.DiscChart.java

public static JFreeChart createDiscChart(ResultSet data, Map parameters) {
    // data.sortBy(1, true);
    CategoryDataset catData = data.catDataSource();
    PieDataset pieData = null;//from  w w w. j  av a  2 s  .  co m
    if (catData.getColumnCount() == 1)
        pieData = DatasetUtilities.createPieDatasetForColumn(catData, 0);
    else
        pieData = DatasetUtilities.createPieDatasetForRow(catData, 0);

    DiscPlot plot = new DiscPlot(pieData);
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
    plot.setDrawingSupplier(DRAWING_SUPPLIER_FACTORY.newDrawingSupplier());
    JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    if (parameters.get("skipItemLabels") != null || parameters.get("skipDiscLabels") != null)
        plot.setLabelGenerator(null);
    else if (parameters.get("discLabelFontSize") != null)
        try {
            float fontSize = Float.parseFloat((String) parameters.get("discLabelFontSize"));
            plot.setLabelFont(plot.getLabelFont().deriveFont(fontSize));
        } catch (Exception lfe) {
        }
    if (parameters.get("ellipse") != null)
        ((StandardDiscItemDistributor) plot.getDiscDistributor()).setCircular(false);

    String interiorGap = (String) parameters.get("interiorGap");
    if (interiorGap != null)
        try {
            plot.setInteriorGap(Integer.parseInt(interiorGap) / 100.0);
        } catch (NumberFormatException e) {
        }
    String interiorSpacing = (String) parameters.get("interiorSpacing");
    if (interiorSpacing != null)
        try {
            plot.setInteriorGap(Integer.parseInt(interiorSpacing) / 200.0);
        } catch (NumberFormatException e) {
        }

    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>/*  w  w  w . ja va 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: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;//ww  w . j a  v a  2 s  .  c om
    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:playground.benjamin.scenarios.zurich.analysis.charts.BkChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*from  ww  w  .  j a  va  2s.  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  . 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();
            //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.chromaui.charts.ChartCustomizer.java

/**
 *
 * @param plot//w ww.  jav  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.chromaui.charts.ChartCustomizer.java

/**
 *
 * @param plot//ww  w . ja  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);
        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.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;
    }/* ww w . ja v a 2  s  .  c  om*/
    return itemCount;
}

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

private static double sumGroupCount(CategoryDataset categorydataset) {
    double groupCount = 0.0;
    for (int i = 0; i < categorydataset.getColumnCount(); i++) {
        groupCount += categorydataset.getValue(0, i).doubleValue();
    }//from  ww  w  . j a  v a2 s  .c  om
    return groupCount;
}

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

/**
 * Sets the colours and gradients to be used when painting the given JFreeChart.
 * /* w w w  . j  a  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));
}