Example usage for org.jfree.chart JFreeChart JFreeChart

List of usage examples for org.jfree.chart JFreeChart JFreeChart

Introduction

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

Prototype

public JFreeChart(String title, Font titleFont, Plot plot, boolean createLegend) 

Source Link

Document

Creates a new chart with the given title and plot.

Usage

From source file:weka.classifiers.timeseries.eval.graph.JFreeChartDriver.java

protected JFreeChart getPredictedTargetsChart(TSForecaster forecaster, ErrorModule preds,
        List<String> targetNames, int stepNumber, int instanceNumOffset, Instances data) {

    if (forecaster instanceof TSLagUser && data != null) {
        TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
        if (lagMaker.getAdjustForTrends() && !lagMaker.isUsingAnArtificialTimeIndex()) {

            // fill in any missing time stamps only
            data = new Instances(data);
            data = weka.classifiers.timeseries.core.Utils.replaceMissing(data, null,
                    lagMaker.getTimeStampField(), true, lagMaker.getPeriodicity(), lagMaker.getSkipEntries());
        }/*from   w w w .j a  v  a  2 s .  c  o  m*/
    }

    // set up a collection of predicted and actual series
    XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection();
    for (String target : targetNames) {
        XYIntervalSeries targetSeries = new XYIntervalSeries(target + "-actual", false, false);
        xyDataset.addSeries(targetSeries);
        targetSeries = new XYIntervalSeries(target + "-predicted", false, false);
        xyDataset.addSeries(targetSeries);

    }

    ValueAxis timeAxis = null;
    NumberAxis valueAxis = new NumberAxis("");
    valueAxis.setAutoRangeIncludesZero(false);
    int timeIndex = -1;
    boolean timeAxisIsDate = false;
    if (forecaster instanceof TSLagUser && data != null) {
        TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
        if (!lagMaker.isUsingAnArtificialTimeIndex() && lagMaker.getAdjustForTrends()) {
            String timeName = lagMaker.getTimeStampField();
            if (data.attribute(timeName).isDate()) {
                timeAxis = new DateAxis("");
                timeAxisIsDate = true;
                timeIndex = data.attribute(timeName).index();
            }
        }
    }

    if (timeAxis == null) {
        timeAxis = new NumberAxis("");
        ((NumberAxis) timeAxis).setAutoRangeIncludesZero(false);
    }

    // now populate the series
    boolean hasConfidenceIntervals = false;
    for (int i = 0; i < targetNames.size(); i++) {
        String targetName = targetNames.get(i);
        List<NumericPrediction> predsForI = preds.getPredictionsForTarget(targetName);
        int predIndex = xyDataset.indexOf(targetName + "-predicted");
        int actualIndex = xyDataset.indexOf(targetName + "-actual");
        XYIntervalSeries predSeries = xyDataset.getSeries(predIndex);
        XYIntervalSeries actualSeries = xyDataset.getSeries(actualIndex);

        for (int j = 0; j < predsForI.size(); j++) {
            double x = Utils.missingValue();
            if (timeAxisIsDate) {
                if (instanceNumOffset + j + stepNumber - 1 < data.numInstances()) {
                    x = data.instance(instanceNumOffset + j + stepNumber - 1).value(timeIndex);
                }
            } else {
                x = instanceNumOffset + j + stepNumber;
            }

            double yPredicted = predsForI.get(j).predicted();
            double yHigh = yPredicted;
            double yLow = yPredicted;
            double[][] conf = predsForI.get(j).predictionIntervals();
            if (conf.length > 0) {
                yLow = conf[0][0];
                yHigh = conf[0][1];
                hasConfidenceIntervals = true;
            }
            if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yPredicted)) {
                if (predSeries != null) {
                    predSeries.add(x, x, x, yPredicted, yLow, yHigh);
                }
                // System.err.println("* " + yPredicted + " " + x);
            }

            double yActual = predsForI.get(j).actual();
            if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yActual)) {
                if (actualSeries != null) {
                    actualSeries.add(x, x, x, yActual, yActual, yActual);
                }
            }
        }
    }

    // set up the chart
    String title = "" + stepNumber + " step-ahead predictions for: ";
    for (String s : targetNames) {
        title += s + ",";
    }
    title = title.substring(0, title.lastIndexOf(","));

    /*
     * String algoSpec = forecaster.getAlgorithmName(); title += " (" + algoSpec
     * + ")";
     */

    if (forecaster instanceof WekaForecaster && hasConfidenceIntervals) {
        double confPerc = ((WekaForecaster) forecaster).getConfidenceLevel() * 100.0;
        title += " [" + Utils.doubleToString(confPerc, 0) + "% conf. intervals]";
    }

    XYErrorRenderer renderer = new XYErrorRenderer();
    renderer.setBaseLinesVisible(true);
    renderer.setDrawXError(false);
    renderer.setDrawYError(true);
    // renderer.setShapesFilled(true);
    XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    chart.setBackgroundPaint(java.awt.Color.white);
    TextTitle chartTitle = chart.getTitle();
    String fontName = chartTitle.getFont().getFontName();
    java.awt.Font newFont = new java.awt.Font(fontName, java.awt.Font.PLAIN, 12);
    chartTitle.setFont(newFont);

    return chart;
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.XYBarChartExpression.java

/**
 * Creates and returns a default instance of an XY bar chart.
 * <p/>/* ww w  . ja v  a 2 s  .c  o  m*/
 * The chart object returned by this method uses an {@link XYPlot} instance as the plot, with a {@link
 * org.jfree.chart.axis.DateAxis} for the domain axis, a {@link org.jfree.chart.axis.NumberAxis} as the range axis,
 * and a {@link XYBarRenderer} as the renderer.
 *
 * @param title       the chart title (<code>null</code> permitted).
 * @param xAxisLabel  a label for the X-axis (<code>null</code> permitted).
 * @param dateAxis    make the domain axis display dates?
 * @param yAxisLabel  a label for the Y-axis (<code>null</code> permitted).
 * @param dataset     the dataset for the chart (<code>null</code> permitted).
 * @param orientation the orientation (horizontal or vertical) (<code>null</code> NOT permitted).
 * @param legend      a flag specifying whether or not a legend is required.
 * @param tooltips    configure chart to generate tool tips?
 * @param urls        configure chart to generate URLs?
 * @return An XY bar chart.
 */
public static JFreeChart createXYBarChart(final String title, final String xAxisLabel, final boolean dateAxis,
        final String yAxisLabel, final XYDataset dataset, final PlotOrientation orientation,
        final boolean legend, final boolean tooltips, final boolean urls) {

    if (orientation == null) {
        throw new IllegalArgumentException("Null 'orientation' argument.");
    }
    ValueAxis domainAxis = null;
    if (dateAxis) {
        domainAxis = new DateAxis(xAxisLabel);
    } else {
        final NumberAxis axis = new NumberAxis(xAxisLabel);
        axis.setAutoRangeIncludesZero(false);
        domainAxis = axis;
    }
    final ValueAxis valueAxis = new NumberAxis(yAxisLabel);

    final XYBarRenderer renderer = new XYBarRenderer();
    renderer.setUseYInterval(true);
    if (tooltips) {
        final XYToolTipGenerator tt;
        if (dateAxis) {
            tt = StandardXYToolTipGenerator.getTimeSeriesInstance();
        } else {
            tt = new StandardXYToolTipGenerator();
        }
        renderer.setBaseToolTipGenerator(tt);
    }
    if (urls) {
        renderer.setURLGenerator(new StandardXYURLGenerator());
    }

    final XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis, renderer);
    plot.setOrientation(orientation);

    return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
}

From source file:org.adempiere.apps.graph.PerformanceIndicator.java

private JFreeChart createChart() {
    JFreeChart chart = null;//from   ww  w  .j  av a 2  s. c  o m

    //   Set Text
    StringBuffer text = new StringBuffer(m_goal.getName());
    if (m_goal.isTarget())
        text.append(": ").append(m_goal.getPercent()).append("%");
    else
        text.append(": ").append(s_format.format(m_goal.getMeasureActual()));

    m_text = text.toString();

    //   ToolTip
    text = new StringBuffer();
    if (m_goal.getDescription() != null)
        text.append(m_goal.getDescription()).append(": ");
    text.append(s_format.format(m_goal.getMeasureActual()));
    if (m_goal.isTarget())
        text.append(" ").append(Msg.getMsg(Env.getCtx(), "of")).append(" ")
                .append(s_format.format(m_goal.getMeasureTarget()));
    setToolTipText(text.toString());
    //
    //setBackground(m_goal.getColor());
    setForeground(GraphUtil.getForeground(getBackground()));
    //   Performance Line
    int percent = m_goal.getPercent();
    if (percent > 100) //   draw 100% line
        m_line = s_width100;
    else //   draw Performance Line
        m_line = s_width100 * m_goal.getGoalPerformanceDouble();

    String title = m_text;
    DefaultValueDataset data = new DefaultValueDataset((float) m_goal.getPercent());
    MeterPlot plot = new MeterPlot(data);

    MColorSchema colorSchema = m_goal.getColorSchema();
    int rangeLo = 0;
    int rangeHi = 0;
    for (int i = 1; i <= 4; i++) {
        switch (i) {
        case 1:
            rangeHi = colorSchema.getMark1Percent();
            break;
        case 2:
            rangeHi = colorSchema.getMark2Percent();
            break;
        case 3:
            rangeHi = colorSchema.getMark3Percent();
            break;
        case 4:
            rangeHi = colorSchema.getMark4Percent();
            break;
        }
        if (rangeHi == 9999)
            rangeHi = (int) Math.floor(rangeLo * 1.5);
        if (rangeLo < rangeHi) {
            plot.addInterval(new MeterInterval("Normal", //label
                    new Range(rangeLo, rangeHi), //range
                    colorSchema.getColor(rangeHi), new BasicStroke(7.0f),
                    //Color.lightGray
                    new Color(-13091716)
            //Color.gray 
            ));
            rangeLo = rangeHi;
        }
    }
    plot.setRange(new Range(0, rangeLo));

    plot.setDialBackgroundPaint(new Color(-13091716));//Color.GRAY);
    plot.setUnits(m_goal.getName());
    plot.setDialShape(DialShape.CHORD);//CIRCLE);        
    //plot.setDialBackgroundPaint(new GradientPaint(0, 0, m_goal.getColor(), 0, 1000, Color.black));
    plot.setNeedlePaint(Color.white);
    plot.setTickSize(2000);
    plot.setTickLabelFont(new Font("SansSerif", Font.BOLD, 12));
    plot.setTickLabelPaint(Color.white);
    plot.setInsets(new RectangleInsets(1.0, 2.0, 3.0, 4.0));

    chart = new JFreeChart(m_text, new Font("SansSerif", Font.BOLD, 15), plot, false);

    return chart;
}

From source file:ucar.unidata.idv.control.chart.ChartHolder.java

/**
 * init// w w  w .  j a v  a 2 s.  c  om
 */
protected void init() {
    if (plot == null) {
        plot = chartManager.doMakePlot();
        chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        if (backgroundColor != null) {
            chart.setBackgroundPaint(backgroundColor);
        }

        applyPlotProperties();
    }
}