Example usage for org.jfree.chart.axis CategoryAxis CategoryAxis

List of usage examples for org.jfree.chart.axis CategoryAxis CategoryAxis

Introduction

In this page you can find the example usage for org.jfree.chart.axis CategoryAxis CategoryAxis.

Prototype

public CategoryAxis() 

Source Link

Document

Creates a new category axis with no label.

Usage

From source file:weka.core.ChartUtils.java

/**
 * Create a box plot from summary data (mean, median, q1, q3, min, max,
 * minOutlier, maxOutlier, list of outliers)
 * /*from   w  ww  . ja  va2  s.c o m*/
 * @param summary summary data
 * @param outliers list of outlier values
 * @param additionalArgs additional options to the renderer
 * @return a box plot chart
 * @throws Exception if a problem occurs
 */
protected static JFreeChart getBoxPlotFromSummaryData(List<Double> summary, List<Double> outliers,
        List<String> additionalArgs) throws Exception {

    if (summary.size() != 8) {
        throw new Exception("Expected 8 values in the summary argument: mean, median, "
                + "q1, q3, min, max, minOutlier, maxOutlier");
    }

    String plotTitle = "Box Plog";
    String userTitle = getOption(additionalArgs, "-title");
    plotTitle = (userTitle != null) ? userTitle : plotTitle;
    String xLabel = getOption(additionalArgs, "-x-label");
    xLabel = xLabel == null ? "" : xLabel;
    String yLabel = getOption(additionalArgs, "-y-label");
    yLabel = yLabel == null ? "" : yLabel;

    DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();

    Double mean = summary.get(0);
    Double median = summary.get(1);
    Double q1 = summary.get(2);
    Double q3 = summary.get(3);
    Double min = summary.get(4);
    Double max = summary.get(5);
    Double minOutlier = summary.get(6);
    Double maxOutlier = summary.get(7);

    if (mean.isNaN() || median.isNaN() || min.isNaN() || max.isNaN() || q1.isNaN() || q3.isNaN()) {
        throw new Exception("NaN in summary data - can't generate box plot");
    }

    BoxAndWhiskerItem item = new BoxAndWhiskerItem(mean, median, q1, q3, min, max, minOutlier, maxOutlier,
            outliers);

    dataset.add(item, "", "");

    CategoryAxis xAxis = new CategoryAxis();
    NumberAxis yAxis = new NumberAxis();
    yAxis.setAutoRangeIncludesZero(false);
    BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    renderer.setFillBox(false);
    renderer.setMaximumBarWidth(0.15);

    CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

    JFreeChart chart = new JFreeChart(plotTitle, new Font("SansSerif", Font.BOLD, 12), plot, false);

    return chart;
}

From source file:org.jax.pubarray.server.restful.GraphingResource.java

/**
 * Create a graph for the given configuration
 * @param graphConfiguration//from w w w . ja  v a 2 s.  com
 *          the key
 * @return
 *          the graph
 */
@SuppressWarnings("unchecked")
private JFreeChart createProbeIntensityGraph(ProbeIntensityGraphConfiguration graphConfiguration) {
    try {
        Connection connection = this.getConnection();

        String[] probeIds = graphConfiguration.getProbeIds();
        double[][] probeDataRows = new double[probeIds.length][];
        for (int i = 0; i < probeIds.length; i++) {
            probeDataRows[i] = this.persistenceManager.getDataRowForProbeID(connection, probeIds[i]);
        }

        TableColumnMetadata orderBy = graphConfiguration.getOrderProbesBy();
        final List<Comparable> orderByItems;
        if (orderBy != null) {
            LOG.info("We are ordering by: " + orderBy);
            orderByItems = this.persistenceManager.getDesignDataColumn(connection, orderBy);
        } else {
            orderByItems = null;
        }

        TableMetadata metadata = this.persistenceManager.getDataTableMetadata(connection);
        final CategoryDataset categoryDataset;
        if (graphConfiguration.getGroupReplicates()) {
            switch (graphConfiguration.getGroupedGraphType()) {
            case BOX_PLOT: {
                categoryDataset = new DefaultBoxAndWhiskerCategoryDataset();
            }
                break;

            case SCATTER_PLOT: {
                categoryDataset = new DefaultMultiValueCategoryDataset();
            }
                break;

            default:
                throw new IllegalArgumentException(
                        "don't know how to deal with plot type: " + graphConfiguration.getGroupedGraphType());
            }
        } else {
            categoryDataset = new DefaultCategoryDataset();
        }

        // iterate through all of the selected probesets
        List<QualifiedColumnMetadata> termsOfInterest = Arrays.asList(graphConfiguration.getTermsOfInterest());
        for (int rowIndex = 0; rowIndex < probeDataRows.length; rowIndex++) {
            double[] currRow = probeDataRows[rowIndex];
            assert currRow.length == metadata.getColumnMetadata().length - 1;

            // should we log2 transform the data?
            if (graphConfiguration.getLog2TransformData()) {
                for (int i = 0; i < currRow.length; i++) {
                    currRow[i] = Math.log(currRow[i]) / LOG2_FACTOR;
                }
            }

            // iterate through the columns in the data table (each column
            // represents a different array)
            List<ComparableContainer<Double, Comparable>> rowElemList = new ArrayList<ComparableContainer<Double, Comparable>>();
            for (int colIndex = 0; colIndex < currRow.length; colIndex++) {
                // we use +1 indexing here because we want to skip over
                // the probesetId metadata and get right to the
                // array intensity metadata
                TableColumnMetadata colMeta = metadata.getColumnMetadata()[colIndex + 1];

                // check to see if we need to skip this data
                if (!graphConfiguration.getIncludeDataFromAllArrays()) {
                    // if it's one of the "terms of interest" we will keep
                    // it. we're using a brute force search here
                    boolean keepThisOne = false;
                    for (QualifiedColumnMetadata termOfInterest : termsOfInterest) {
                        if (termOfInterest.getTableName().equals(metadata.getTableName())
                                && termOfInterest.getName().equals(colMeta.getName())) {
                            keepThisOne = true;
                            break;
                        }
                    }

                    if (!keepThisOne) {
                        continue;
                    }
                }

                final String columnName = colMeta.getName();
                final Comparable columnKey;
                if (orderByItems == null) {
                    columnKey = columnName;
                } else {
                    // the ordering will be done on the selected
                    // "order by" design criteria
                    columnKey = new ComparableContainer<String, Comparable>(columnName,
                            orderByItems.get(colIndex), !graphConfiguration.getGroupReplicates());

                    // TODO remove me!!!!
                    System.out.println("For array " + columnName + " the order by " + "value is: "
                            + orderByItems.get(colIndex));
                    // end of remove me
                }

                rowElemList
                        .add(new ComparableContainer<Double, Comparable>(currRow[colIndex], columnKey, false));
            }

            Collections.sort(rowElemList);

            if (graphConfiguration.getGroupReplicates()) {
                switch (graphConfiguration.getGroupedGraphType()) {
                case BOX_PLOT: {
                    DefaultBoxAndWhiskerCategoryDataset dataset = (DefaultBoxAndWhiskerCategoryDataset) categoryDataset;
                    for (int i = 0; i < rowElemList.size(); i++) {
                        List<Double> groupList = new ArrayList<Double>();
                        groupList.add(rowElemList.get(i).getElement());
                        Comparable colKey = rowElemList.get(i).getComparable();

                        i++;
                        for (; i < rowElemList.size()
                                && rowElemList.get(i).getComparable().equals(colKey); i++) {
                            groupList.add(rowElemList.get(i).getElement());
                        }
                        i--;

                        dataset.add(groupList, probeIds[rowIndex], colKey);
                    }
                }
                    break;

                case SCATTER_PLOT: {
                    DefaultMultiValueCategoryDataset dataset = (DefaultMultiValueCategoryDataset) categoryDataset;
                    for (int i = 0; i < rowElemList.size(); i++) {
                        List<Double> groupList = new ArrayList<Double>();
                        groupList.add(rowElemList.get(i).getElement());
                        Comparable colKey = rowElemList.get(i).getComparable();

                        i++;
                        for (; i < rowElemList.size()
                                && rowElemList.get(i).getComparable().equals(colKey); i++) {
                            groupList.add(rowElemList.get(i).getElement());
                        }
                        i--;

                        dataset.add(groupList, probeIds[rowIndex], colKey);
                    }
                }
                    break;
                }
            } else {
                DefaultCategoryDataset dataset = (DefaultCategoryDataset) categoryDataset;
                for (ComparableContainer<Double, Comparable> rowElem : rowElemList) {
                    dataset.addValue(rowElem.getElement(), probeIds[rowIndex], rowElem.getComparable());
                }
            }
        }

        CategoryAxis xAxis = new CategoryAxis();
        if (graphConfiguration.getGroupReplicates() && orderBy != null) {
            xAxis.setLabel(orderBy.getName());
        } else {
            if (orderBy != null) {
                xAxis.setLabel("Arrays (Ordered By " + orderBy.getName() + ")");
            } else {
                xAxis.setLabel("Arrays");
            }
        }
        xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

        final NumberAxis yAxis;
        if (graphConfiguration.getLog2TransformData()) {
            yAxis = new NumberAxis("log2(Intensity)");
        } else {
            yAxis = new NumberAxis("Intensity");
        }
        yAxis.setAutoRange(true);
        yAxis.setAutoRangeIncludesZero(false);

        // TODO: this is a HACK to deal with auto-range bug in JFreeChart
        //       which occurs when doing the grouped scatter plot
        if (graphConfiguration.getGroupReplicates()
                && graphConfiguration.getGroupedGraphType() == GroupedGraphType.SCATTER_PLOT) {
            double minVal = Double.POSITIVE_INFINITY;
            double maxVal = Double.NEGATIVE_INFINITY;
            for (double[] dataRow : probeDataRows) {
                for (double datum : dataRow) {
                    if (datum > maxVal) {
                        maxVal = datum;
                    }

                    if (datum < minVal) {
                        minVal = datum;
                    }
                }

                if (minVal != Double.POSITIVE_INFINITY && maxVal != Double.NEGATIVE_INFINITY
                        && minVal != maxVal) {
                    yAxis.setAutoRange(false);

                    double margin = (maxVal - minVal) * 0.02;
                    Range yRange = new Range(minVal - margin, maxVal + margin);
                    yAxis.setRange(yRange);
                }
            }
        }
        // END HACK

        final CategoryItemRenderer renderer;
        if (graphConfiguration.getGroupReplicates()) {
            switch (graphConfiguration.getGroupedGraphType()) {
            case BOX_PLOT: {
                BoxAndWhiskerRenderer boxRenderer = new BoxAndWhiskerRenderer();
                boxRenderer.setMaximumBarWidth(0.03);
                renderer = boxRenderer;
            }
                break;

            case SCATTER_PLOT: {
                renderer = new ScatterRenderer();
            }
                break;

            default:
                throw new IllegalArgumentException(
                        "don't know how to deal with plot type: " + graphConfiguration.getGroupedGraphType());
            }
        } else {
            renderer = new LineAndShapeRenderer();
        }
        Plot plot = new CategoryPlot(categoryDataset, xAxis, yAxis, renderer);

        return new JFreeChart("Intensity Values", plot);
    } catch (SQLException ex) {
        LOG.log(Level.SEVERE, "failed to generate image", ex);
        return null;
    }
}