Search in sources :

Example 41 with NumberAxis

use of org.jfree.chart.axis.NumberAxis in project hudson-2.x by hudson.

the class Job method getBuildTimeGraph.

public Graph getBuildTimeGraph() {
    return new Graph(getLastBuild().getTimestamp(), 500, 400) {

        @Override
        protected JFreeChart createGraph() {
            class ChartLabel implements Comparable<ChartLabel> {

                final Run run;

                public ChartLabel(Run r) {
                    this.run = r;
                }

                public int compareTo(ChartLabel that) {
                    return Run.ORDER_BY_DATE.compare(that.run, run);
                }

                @Override
                public boolean equals(Object o) {
                    // on (c instanceof ChartLabel)
                    if (o == null || !ChartLabel.class.isAssignableFrom(o.getClass())) {
                        return false;
                    }
                    ChartLabel that = (ChartLabel) o;
                    return run == that.run;
                }

                public Color getColor() {
                    // TODO: consider gradation. See
                    // http://www.javadrive.jp/java2d/shape/index9.html
                    Result r = run.getResult();
                    if (r == Result.FAILURE)
                        return ColorPalette.RED;
                    else if (r == Result.UNSTABLE)
                        return ColorPalette.YELLOW;
                    else if (r == Result.ABORTED || r == Result.NOT_BUILT)
                        return ColorPalette.GREY;
                    else
                        return ColorPalette.BLUE;
                }

                @Override
                public int hashCode() {
                    return run.hashCode();
                }

                @Override
                public String toString() {
                    String l = run.getDisplayName();
                    if (run instanceof Build) {
                        String s = ((Build) run).getBuiltOnStr();
                        if (s != null)
                            l += ' ' + s;
                    }
                    return l;
                }
            }
            DataSetBuilder<String, ChartLabel> data = new DataSetBuilder<String, ChartLabel>();
            for (Run r : getBuilds()) {
                if (r.isBuilding())
                    continue;
                data.add(((double) r.getDuration()) / (1000 * 60), "min", new ChartLabel(r));
            }
            final CategoryDataset dataset = data.build();
            final JFreeChart chart = // chart
            ChartFactory.createStackedAreaChart(// chart
            null, // unused
            null, // range axis label
            Messages.Job_minutes(), // data
            dataset, // orientation
            PlotOrientation.VERTICAL, // include legend
            false, // tooltips
            true, // urls
            false);
            chart.setBackgroundPaint(Color.white);
            final CategoryPlot plot = chart.getCategoryPlot();
            // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
            plot.setBackgroundPaint(Color.WHITE);
            plot.setOutlinePaint(null);
            plot.setForegroundAlpha(0.8f);
            // plot.setDomainGridlinesVisible(true);
            // plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinesVisible(true);
            plot.setRangeGridlinePaint(Color.black);
            CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
            plot.setDomainAxis(domainAxis);
            domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
            domainAxis.setLowerMargin(0.0);
            domainAxis.setUpperMargin(0.0);
            domainAxis.setCategoryMargin(0.0);
            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            ChartUtil.adjustChebyshev(dataset, rangeAxis);
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            StackedAreaRenderer ar = new StackedAreaRenderer2() {

                @Override
                public Paint getItemPaint(int row, int column) {
                    ChartLabel key = (ChartLabel) dataset.getColumnKey(column);
                    return key.getColor();
                }

                @Override
                public String generateURL(CategoryDataset dataset, int row, int column) {
                    ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
                    return String.valueOf(label.run.number);
                }

                @Override
                public String generateToolTip(CategoryDataset dataset, int row, int column) {
                    ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
                    return label.run.getDisplayName() + " : " + label.run.getDurationString();
                }
            };
            plot.setRenderer(ar);
            // crop extra space around the graph
            plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));
            return chart;
        }
    };
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) DataSetBuilder(hudson.util.DataSetBuilder) StackedAreaRenderer2(hudson.util.StackedAreaRenderer2) JFreeChart(org.jfree.chart.JFreeChart) CategoryPlot(org.jfree.chart.plot.CategoryPlot) StackedAreaRenderer(org.jfree.chart.renderer.category.StackedAreaRenderer) ShiftedCategoryAxis(hudson.util.ShiftedCategoryAxis) Graph(hudson.util.Graph) CategoryAxis(org.jfree.chart.axis.CategoryAxis) ShiftedCategoryAxis(hudson.util.ShiftedCategoryAxis) CategoryDataset(org.jfree.data.category.CategoryDataset) RectangleInsets(org.jfree.ui.RectangleInsets) JSONObject(net.sf.json.JSONObject)

Example 42 with NumberAxis

use of org.jfree.chart.axis.NumberAxis in project series-rest-api by 52North.

the class ChartIoHandler method createRangeAxis.

public ValueAxis createRangeAxis(DatasetOutput<?> metadata) {
    NumberAxis axis = new NumberAxis(createRangeLabel(metadata));
    axis.setTickLabelFont(LabelConstants.FONT_LABEL);
    axis.setLabelFont(LabelConstants.FONT_LABEL);
    axis.setTickLabelPaint(LabelConstants.COLOR);
    axis.setLabelPaint(LabelConstants.COLOR);
    return axis;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis)

Example 43 with NumberAxis

use of org.jfree.chart.axis.NumberAxis in project dhis2-core by dhis2.

the class DefaultChartService method getJFreeChart.

/**
 * Returns a JFreeChart of type defined in the chart argument.
 */
private JFreeChart getJFreeChart(PlotData plotData) {
    final CategoryDataset[] dataSets = getCategoryDataSet(plotData);
    final CategoryDataset dataSet = dataSets[0];
    final BarRenderer barRenderer = getBarRenderer();
    final LineAndShapeRenderer lineRenderer = getLineRenderer();
    // ---------------------------------------------------------------------
    // Plot
    // ---------------------------------------------------------------------
    CategoryPlot plot;
    if (plotData.isType(VisualizationType.LINE.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), lineRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (plotData.isType(VisualizationType.COLUMN.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (plotData.isType(VisualizationType.BAR.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.HORIZONTAL);
    } else if (plotData.isType(VisualizationType.AREA.name())) {
        return getStackedAreaChart(plotData, dataSet);
    } else if (plotData.isType(VisualizationType.PIE.name())) {
        return getMultiplePieChart(plotData, dataSets);
    } else if (plotData.isType(VisualizationType.STACKED_COLUMN.name())) {
        return getStackedBarChart(plotData, dataSet, false);
    } else if (plotData.isType(VisualizationType.STACKED_BAR.name())) {
        return getStackedBarChart(plotData, dataSet, true);
    } else if (plotData.isType(VisualizationType.RADAR.name())) {
        return getRadarChart(plotData, dataSet);
    } else if (plotData.isType(VisualizationType.GAUGE.name())) {
        Number number = dataSet.getValue(0, 0);
        ValueDataset valueDataSet = new DefaultValueDataset(number);
        return getGaugeChart(plotData, valueDataSet);
    } else {
        throw new IllegalArgumentException("Illegal or no chart type: " + plotData.getType());
    }
    if (plotData.isRegression()) {
        plot.setDataset(1, dataSets[1]);
        plot.setRenderer(1, lineRenderer);
    }
    JFreeChart jFreeChart = new JFreeChart(plotData.getName(), TITLE_FONT, plot, !plotData.isHideLegend());
    setBasicConfig(jFreeChart, plotData);
    if (plotData.isTargetLine()) {
        plot.addRangeMarker(getMarker(plotData.getTargetLineValue(), plotData.getTargetLineLabel()));
    }
    if (plotData.isBaseLine()) {
        plot.addRangeMarker(getMarker(plotData.getBaseLineValue(), plotData.getBaseLineLabel()));
    }
    if (plotData.isHideSubtitle()) {
        jFreeChart.addSubtitle(getSubTitle(plotData));
    }
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    // ---------------------------------------------------------------------
    // Category label positions
    // ---------------------------------------------------------------------
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLabel(plotData.getDomainAxisLabel());
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabel(plotData.getRangeAxisLabel());
    return jFreeChart;
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) LineAndShapeRenderer(org.jfree.chart.renderer.category.LineAndShapeRenderer) NumberAxis(org.jfree.chart.axis.NumberAxis) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) ValueDataset(org.jfree.data.general.ValueDataset) DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) CategoryPlot(org.jfree.chart.plot.CategoryPlot) JFreeChart(org.jfree.chart.JFreeChart) CategoryAxis(org.jfree.chart.axis.CategoryAxis) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) ValueAxis(org.jfree.chart.axis.ValueAxis)

Example 44 with NumberAxis

use of org.jfree.chart.axis.NumberAxis in project dhis2-core by dhis2.

the class DefaultChartService method getCategoryPlot.

/**
 * Returns a CategoryPlot.
 */
private CategoryPlot getCategoryPlot(CategoryDataset dataSet, CategoryItemRenderer renderer, PlotOrientation orientation, CategoryLabelPositions labelPositions) {
    CategoryPlot plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), renderer);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    plot.setOrientation(orientation);
    CategoryAxis xAxis = plot.getDomainAxis();
    xAxis.setCategoryLabelPositions(labelPositions);
    return plot;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) CategoryPlot(org.jfree.chart.plot.CategoryPlot)

Example 45 with NumberAxis

use of org.jfree.chart.axis.NumberAxis in project n2a by frothga.

the class Plot method updateChart.

public void updateChart(JFreeChart chart) {
    XYPlot plot = chart.getXYPlot();
    plot.setNotify(false);
    updateDatasets();
    LegendTitle legend = chart.getLegend();
    legend.setVisible(columnCount <= 10);
    NumberAxis axis0 = (NumberAxis) plot.getRangeAxis();
    axis0.setAutoRangeIncludesZero(false);
    if (range0 > 0)
        axis0.setAutoRangeMinimumSize(range0 / 2);
    else
        axis0.setAutoRangeMinimumSize(1);
    // range locked
    if (!Double.isNaN(ymin))
        axis0.setRange(ymin, ymax);
    if (!Double.isNaN(xmin)) {
        ValueAxis x = plot.getDomainAxis();
        x.setRange(xmin, xmax);
    }
    int count = dataset0.getSeriesCount();
    float shift = 0;
    if (dataset1 != null) {
        // So we use only half of the color range
        count *= 2;
        shift = 0.75f + 0.5f / count;
    }
    XYLineAndShapeRenderer renderer;
    XYItemRenderer ir = plot.getRenderer();
    if (ir instanceof XYLineAndShapeRenderer) {
        renderer = (XYLineAndShapeRenderer) ir;
    } else {
        renderer = new XYLineAndShapeRenderer();
        plot.setRenderer(renderer);
    }
    for (int i = 0; i < dataset0.getSeriesCount(); i++) {
        Column column = left.get(i);
        // does not fire renderer change event
        styleSeries(renderer, i, column, count, shift);
    }
    // fires renderer change event
    renderer.setDrawSeriesLineAsPath(true);
    if (dataset1 == null) {
        axis0.setTickMarkPaint(Color.black);
        axis0.setTickLabelPaint(Color.black);
        axis0.setAxisLinePaint(Color.black);
        plot.setDataset(1, null);
        plot.setRangeAxis(1, null);
        plot.setRenderer(1, null);
    } else {
        Color color0 = Color.getHSBColor(0.0f, 1.0f, 0.8f);
        axis0.setTickMarkPaint(color0);
        axis0.setTickLabelPaint(color0);
        axis0.setAxisLinePaint(color0);
        plot.setDataset(1, dataset1);
        plot.mapDatasetToRangeAxis(1, 1);
        NumberAxis axis1 = (NumberAxis) plot.getRangeAxis(1);
        if (axis1 == null) {
            axis1 = new NumberAxis();
            axis1.setAutoRangeIncludesZero(false);
            axis1.setTickLabelFont(axis0.getTickLabelFont());
            Color color1 = Color.getHSBColor(0.5f, 1.0f, 0.8f);
            axis1.setTickMarkPaint(color1);
            axis1.setTickLabelPaint(color1);
            axis1.setAxisLinePaint(color1);
            plot.setRangeAxis(1, axis1);
        }
        // else we created axis1, so all the initial settings are correct
        if (range1 > 0)
            axis1.setAutoRangeMinimumSize(range1 / 2);
        else
            axis1.setAutoRangeMinimumSize(1);
        count = dataset1.getSeriesCount() * 2;
        shift = 0.25f + 0.5f / count;
        ir = plot.getRenderer(1);
        if (ir instanceof XYLineAndShapeRenderer) {
            renderer = (XYLineAndShapeRenderer) ir;
        } else {
            renderer = new XYLineAndShapeRenderer();
            plot.setRenderer(1, renderer);
        }
        for (int i = 0; i < dataset1.getSeriesCount(); i++) {
            Column column = right.get(i);
            styleSeries(renderer, i, column, count, shift);
        }
        renderer.setDrawSeriesLineAsPath(true);
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
    }
    plot.setNotify(true);
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) Color(java.awt.Color) LegendTitle(org.jfree.chart.title.LegendTitle) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Aggregations

NumberAxis (org.jfree.chart.axis.NumberAxis)260 JFreeChart (org.jfree.chart.JFreeChart)147 Test (org.junit.Test)118 XYPlot (org.jfree.chart.plot.XYPlot)99 CategoryAxis (org.jfree.chart.axis.CategoryAxis)95 CategoryPlot (org.jfree.chart.plot.CategoryPlot)80 ValueAxis (org.jfree.chart.axis.ValueAxis)46 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)44 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)40 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)39 XYSeries (org.jfree.data.xy.XYSeries)38 BarRenderer (org.jfree.chart.renderer.category.BarRenderer)37 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)30 XYDataset (org.jfree.data.xy.XYDataset)30 Color (java.awt.Color)28 DateAxis (org.jfree.chart.axis.DateAxis)27 StandardXYToolTipGenerator (org.jfree.chart.labels.StandardXYToolTipGenerator)27 BasicStroke (java.awt.BasicStroke)25 StandardXYItemRenderer (org.jfree.chart.renderer.xy.StandardXYItemRenderer)24 CategoryDataset (org.jfree.data.category.CategoryDataset)21