Example usage for org.jfree.chart ChartFactory createLineChart

List of usage examples for org.jfree.chart ChartFactory createLineChart

Introduction

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

Prototype

public static JFreeChart createLineChart(String title, String categoryAxisLabel, String valueAxisLabel,
        CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 

Source Link

Document

Creates a line chart with default settings.

Usage

From source file:com.liferay.samplestruts.struts.action.ViewChartAction.java

@Override
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    try {// ww  w  .jav  a 2 s  .c  om
        if (_log.isInfoEnabled()) {
            _log.info("execute");
        }

        String attrName = "chart_name";

        // Application scoped session attributes can be fetched from the
        // servlet directly. Portlet scoped session attributes can be
        // fetched from Sun's PortletSessionUtil.

        HttpSession session = request.getSession();

        String chartName = (String) session.getAttribute(attrName);
        //(String)_getAttribute(request, attrName);

        // Chart

        String chartType = request.getParameter("chart_type");

        CategoryDataset dataset = _getDataset();

        String xName = "Soda";
        String yName = "Votes";

        JFreeChart chart = null;

        if (chartType.equals("area")) {
            chart = ChartFactory.createAreaChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("horizontal_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.HORIZONTAL,
                    true, false, false);
        } else if (chartType.equals("line")) {
            chart = ChartFactory.createLineChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else if (chartType.equals("vertical_bar")) {
            chart = ChartFactory.createBarChart(chartName, xName, yName, dataset, PlotOrientation.VERTICAL,
                    true, false, false);
        } else {
            PieDataset pieData = DatasetUtilities.createPieDatasetForRow(dataset, 0);

            chart = ChartFactory.createPieChart(chartName, pieData, true, false, false);
        }

        response.setContentType("image/jpeg");

        OutputStream out = response.getOutputStream();

        ChartUtilities.writeChartAsJPEG(out, chart, 400, 400);

        return actionMapping.findForward("/common/null.jsp");
    } catch (Exception e) {
        request.setAttribute(PageContext.EXCEPTION, e);

        return actionMapping.findForward("/common/error.jsp");
    }
}

From source file:jenkins.plugins.livingdoc.chart.ProjectSummaryChart.java

private JFreeChart createChart(DefaultCategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createLineChart("", "Build ID", "# Tests", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    customizeChart(chart);//from  w w  w  .j  a  v a 2s . c om

    return chart;
}

From source file:edu.coeia.charts.LineChartPanel.java

/**
 * Creates a sample chart.//from ww  w .j  av a2  s. co  m
 *
 * @param dataset  a dataset.
 *
 * @return The chart.
 */
private static JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createLineChart("Frequency of Messages", // chart title
            "Time of Messages", // domain axis label
            "Number of Messages", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);

    // customise the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);

    // customise the renderer...
    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setSeriesStroke(0, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 10.0f, 6.0f }, 0.0f));

    renderer.setSeriesStroke(1, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 6.0f, 6.0f }, 0.0f));

    renderer.setSeriesStroke(2, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 2.0f, 6.0f }, 0.0f));

    return chart;
}

From source file:org.jenkinsci.plugins.autozoil.graph.AutozoilGraph.java

/**
 * Creates a Autozoil trend graph//  w  ww  .ja v  a 2 s  .  c o  m
 *
 * @return the JFreeChart graph object
 */
protected JFreeChart createGraph() {

    final JFreeChart chart = ChartFactory.createLineChart(null, // chart title
            null, // unused
            yLabel, // range axis label
            categoryDataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    final LegendTitle legend = chart.getLegend();
    legend.setPosition(RectangleEdge.RIGHT);

    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.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();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setLowerBound(0);
    rangeAxis.setAutoRange(true);

    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseStroke(new BasicStroke(2.0f));
    ColorPalette.apply(renderer);

    // crop extra space around the graph
    plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0));

    return chart;
}

From source file:org.matsim.counts.algorithms.graphs.BiasErrorGraph.java

@Override
public JFreeChart createChart(final int nbr) {
    DefaultCategoryDataset dataset0 = new DefaultCategoryDataset();
    DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();

    this.errorStats = new ComparisonErrorStatsCalculator(this.ccl_);

    double[] meanRelError = errorStats.getMeanRelError();
    //      double[] meanAbsError = errorStats.getMeanAbsError();
    double[] meanAbsBias = errorStats.getMeanAbsBias();

    for (int h = 0; h < 24; h++) {
        dataset0.addValue(meanRelError[h], "Mean rel error", Integer.toString(h + 1));
        //         dataset1.addValue(meanAbsError[h], "Mean abs error", Integer.toString(h + 1));
        dataset1.addValue(meanAbsBias[h], "Mean abs bias", Integer.toString(h + 1));
    }// w w  w.j av  a2s  .  c o m

    this.chart_ = ChartFactory.createLineChart("", "Hour", "Mean rel error [%]", dataset0,
            PlotOrientation.VERTICAL, true, // legend?
            true, // tooltips?
            false // URLs?
    );
    CategoryPlot plot = this.chart_.getCategoryPlot();
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    plot.setDataset(1, dataset1);
    plot.mapDatasetToRangeAxis(1, 1);

    final LineAndShapeRenderer renderer = new LineAndShapeRenderer();
    renderer.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator());
    plot.setRenderer(0, renderer);

    final CategoryAxis axis1 = new CategoryAxis("Hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(axis1);

    //      final ValueAxis axis2 = new NumberAxis("Mean abs {bias, error} [veh/h]");
    final ValueAxis axis2 = new NumberAxis("Mean abs bias [veh/h]");
    plot.setRangeAxis(1, axis2);

    final ValueAxis axis3 = plot.getRangeAxis(0);
    axis3.setRange(0.0, 100.0);

    final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator());
    renderer2.setSeriesToolTipGenerator(1, new StandardCategoryToolTipGenerator());
    //      renderer2.setSeriesPaint(0, Color.black);
    plot.setRenderer(1, renderer2);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);

    return this.chart_;
}

From source file:sipl.recursos.Graficar.java

public void Prestamos(int[] values, int[] fecha, int n, String direccion, String tiempo, String titulo) {
    try {//from  ww w  .j  a v  a 2s  .com
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int j = 0; j < n; j++) {
            dataset.addValue(values[j], "Cantidad de Prstamos", "" + fecha[j]);
        }
        JFreeChart chart = ChartFactory.createLineChart(titulo, tiempo, "Cantidad", dataset,
                PlotOrientation.VERTICAL, true, true, true);
        try {
            ChartUtilities.saveChartAsJPEG(new File(direccion), chart, 700, 500);
        } catch (IOException e) {
            System.out.println("Error al abrir el archivo");
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:smarthouse_server.LineChart.java

/**
 * Creates a sample chart.//from w ww  .  java  2s.  c  o  m
 * 
 * @param dataset  a dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(CategoryDataset dataset) {

    // create the chart...
    chart = ChartFactory.createLineChart("Room Monitor #", // chart title
            "Time", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    //        StandardLegend legend = (StandardLegend) chart.getLegend();
    //      legend.setDisplaySeriesShapes(true);
    //    legend.setShapeScaleX(1.5);
    //  legend.setShapeScaleY(1.5);
    //legend.setDisplaySeriesLines(true);

    chart.setBackgroundPaint(Color.white);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);
    plot.getDomainAxis().setVisible(false);
    // customise the range axis...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    // customise the renderer...
    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    //        renderer.setDrawShapes(true);

    renderer.setSeriesStroke(0, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 10.0f, 6.0f }, 0.0f));
    renderer.setSeriesStroke(1, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 6.0f, 6.0f }, 0.0f));
    /*        renderer.setSeriesStroke(
    2, new BasicStroke(
        2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
        1.0f, new float[] {2.0f, 6.0f}, 0.0f
    )
            );*/
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;
}

From source file:org.matsim.counts.algorithms.graphs.BiasNormalizedErrorGraph.java

@Override
public JFreeChart createChart(final int nbr) {
    DefaultCategoryDataset dataset0 = new DefaultCategoryDataset();
    DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();

    this.errorStats = new ComparisonErrorStatsCalculator(this.ccl_);

    double[] meanNormRelError = this.errorStats.getMeanNormRelError();
    //      double[] meanAbsError = this.errorStats.getMeanAbsError();
    double[] meanBias = this.errorStats.getMeanBias();

    for (int h = 0; h < 24; h++) {
        meanNormRelError[h] *= 100;//from  ww  w .ja v  a 2s  .c  om
        dataset0.addValue(meanNormRelError[h], "Mean norm rel error", Integer.toString(h + 1));
        //         dataset1.addValue(meanAbsError[h], "Mean abs error", Integer.toString(h + 1));
        dataset1.addValue(meanBias[h], "Mean bias", Integer.toString(h + 1));
    }

    this.chart_ = ChartFactory.createLineChart("", "Hour", "Mean norm rel error [%]", dataset0,
            PlotOrientation.VERTICAL, true, // legend?
            true, // tooltips?
            false // URLs?
    );
    CategoryPlot plot = this.chart_.getCategoryPlot();
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    plot.setDataset(1, dataset1);
    plot.mapDatasetToRangeAxis(1, 1);

    final LineAndShapeRenderer renderer = new LineAndShapeRenderer();
    renderer.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator());
    plot.setRenderer(0, renderer);

    final CategoryAxis axis1 = new CategoryAxis("Hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(axis1);

    //      final ValueAxis axis2 = new NumberAxis("Mean abs {bias, error} [veh/h]");
    final ValueAxis axis2 = new NumberAxis("Mean bias [veh/h]");
    plot.setRangeAxis(1, axis2);

    final ValueAxis axis3 = plot.getRangeAxis(0);
    axis3.setRange(0.0, 100.0);

    final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator());
    renderer2.setSeriesToolTipGenerator(1, new StandardCategoryToolTipGenerator());
    //      renderer2.setSeriesPaint(0, Color.black);
    plot.setRenderer(1, renderer2);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);

    return this.chart_;
}

From source file:edu.ucla.stat.SOCR.chart.SuperCategoryChart_vertical.java

protected JFreeChart createLegend(CategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createLineChart(chartTitle, // chart title
            domainLabel, // domain axis label
            rangeLabel, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );/*  ww  w  .ja  va2 s  . c  o m*/

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.white);
    CategoryPlot plot = chart.getCategoryPlot();

    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setDrawOutlines(true);
    // renderer.setUseFillPaint(true);
    // renderer.setFillPaint(Color.white);
    renderer.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator());
    return chart;

}

From source file:hudson.plugins.testlink.TestLinkGraph.java

/**
 * Creates TestLink trend graph.//from   www.  j  a v  a  2  s  .  c o  m
 * 
 * @return the JFreeChart graph object.
 */
protected JFreeChart createGraph() {
    final JFreeChart chart = ChartFactory.createLineChart(null, null, yLabel, categoryDataset,
            PlotOrientation.VERTICAL, true, true, false);

    final LegendTitle legend = chart.getLegend();
    legend.setPosition(RectangleEdge.RIGHT);

    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setForegroundAlpha(0.8f);
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.darkGray);

    final CategoryAxis domainAxis = new ShiftedCategoryAxis(xLabel);
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    domainAxis.setCategoryMargin(0.0);
    plot.setDomainAxis(domainAxis);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRange(true);
    rangeAxis.setAutoRangeMinimumSize(5);
    rangeAxis.setLowerBound(0);

    final CategoryItemRenderer renderer = plot.getRenderer();

    renderer.setSeriesStroke(0, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(0, new Color(35, 20, 89));

    renderer.setSeriesStroke(1, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(1, new Color(0, 145, 0));

    renderer.setSeriesStroke(2, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(2, new Color(207, 69, 21));

    renderer.setSeriesStroke(3, new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.CAP_BUTT, 1.0f,
            new float[] { 1.0f, 1.0f }, 0.0f));
    renderer.setSeriesPaint(3, Color.orange);

    plot.setInsets(new RectangleInsets(5.0, 0, 0, 5.0));

    return chart;
}