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:org.exist.xquery.modules.jfreechart.JFreeChartFactory.java

/**
 *  Create JFreeChart graph using the supplied parameters.
 *
 * @param chartType One of the many chart types.
 * @param conf      Chart configuration//from  w  ww  .  j av  a 2s .com
 * @param is        Inputstream containing chart data
 * @return          Initialized chart or NULL in case of issues.
 * @throws IOException Thrown when a problem is reported while parsing XML data.
 */
public static JFreeChart createJFreeChart(String chartType, Configuration conf, InputStream is)
        throws XPathException {

    logger.debug("Generating " + chartType);

    // Currently two dataset types supported
    CategoryDataset categoryDataset = null;
    PieDataset pieDataset = null;

    try {
        if ("PieChart".equals(chartType) || "PieChart3D".equals(chartType) || "RingChart".equals(chartType)) {
            logger.debug("Reading XML PieDataset");
            pieDataset = DatasetReader.readPieDatasetFromXML(is);

        } else {
            logger.debug("Reading XML CategoryDataset");
            categoryDataset = DatasetReader.readCategoryDatasetFromXML(is);
        }

    } catch (IOException ex) {
        throw new XPathException(ex.getMessage());

    } finally {
        try {
            is.close();
        } catch (IOException ex) {
            //
        }
    }

    // Return chart
    JFreeChart chart = null;

    // Big chart type switch
    if ("AreaChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createAreaChart(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("BarChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createBarChart(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("BarChart3D".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createBarChart3D(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("LineChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createLineChart(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("LineChart3D".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createLineChart3D(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("MultiplePieChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createMultiplePieChart(conf.getTitle(), categoryDataset, conf.getOrder(),
                conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls());

        setPieChartParameters(chart, conf);

    } else if ("MultiplePieChart3D".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createMultiplePieChart3D(conf.getTitle(), categoryDataset, conf.getOrder(),
                conf.isGenerateLegend(), conf.isGenerateTooltips(), conf.isGenerateUrls());

        setPieChartParameters(chart, conf);

    } else if ("PieChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createPieChart(conf.getTitle(), pieDataset, conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setPieChartParameters(chart, conf);

    } else if ("PieChart3D".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createPieChart3D(conf.getTitle(), pieDataset, conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setPieChartParameters(chart, conf);

    } else if ("RingChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createRingChart(conf.getTitle(), pieDataset, conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setPieChartParameters(chart, conf);
    } else if ("SpiderWebChart".equalsIgnoreCase(chartType)) {
        SpiderWebPlot plot = new SpiderWebPlot(categoryDataset);
        if (conf.isGenerateTooltips()) {
            plot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        }
        chart = new JFreeChart(conf.getTitle(), JFreeChart.DEFAULT_TITLE_FONT, plot, false);

        if (conf.isGenerateLegend()) {
            LegendTitle legend = new LegendTitle(plot);
            legend.setPosition(RectangleEdge.BOTTOM);
            chart.addSubtitle(legend);
        } else {
            TextTitle subTitle = new TextTitle(" ");
            subTitle.setPosition(RectangleEdge.BOTTOM);
            chart.addSubtitle(subTitle);
        }

        setCategoryChartParameters(chart, conf);

    } else if ("StackedAreaChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createStackedAreaChart(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("StackedBarChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createStackedBarChart(conf.getTitle(), conf.getDomainAxisLabel(),
                conf.getRangeAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("StackedBarChart3D".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createStackedBarChart3D(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);

    } else if ("WaterfallChart".equalsIgnoreCase(chartType)) {
        chart = ChartFactory.createWaterfallChart(conf.getTitle(), conf.getCategoryAxisLabel(),
                conf.getValueAxisLabel(), categoryDataset, conf.getOrientation(), conf.isGenerateLegend(),
                conf.isGenerateTooltips(), conf.isGenerateUrls());

        setCategoryChartParameters(chart, conf);
    } else {
        logger.error("Illegal chartype. Choose one of " + "AreaChart BarChart BarChart3D LineChart LineChart3D "
                + "MultiplePieChart MultiplePieChart3D PieChart PieChart3D "
                + "RingChart SpiderWebChart StackedAreaChart StackedBarChart "
                + "StackedBarChart3D WaterfallChart");
    }

    setCommonParameters(chart, conf);

    return chart;
}

From source file:org.jreserve.gui.calculations.factor.editor.DevelopmentFactorPlot.java

private Component createPlotComponent() {

    boolean legend = true;
    boolean tooltips = false;
    boolean urls = false;
    chart = ChartFactory.createLineChart(null, null, null, dataSet, PlotOrientation.VERTICAL, legend, tooltips,
            urls);/*from w  w w .  j  a  va 2 s .c om*/

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.GRAY);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.WHITE);

    NumberAxis axis = (NumberAxis) plot.getRangeAxis();
    axis.setAutoRangeIncludesZero(false);
    axis.setAutoRangeStickyZero(true);

    renderer = plot.getRenderer();
    if (renderer instanceof LineAndShapeRenderer) {
        LineAndShapeRenderer lasr = (LineAndShapeRenderer) renderer;
        lasr.setBaseShapesVisible(true);
        lasr.setDrawOutlines(true);
        lasr.setUseFillPaint(true);
        lasr.setBaseStroke(new BasicStroke(2));

        int r = 3;
        Shape circle = new Ellipse2D.Float(-r, -r, 2 * r, 2 * r);
        int count = dataSet.getRowCount();
        for (int i = 0; i < count; i++) {
            PlotLabel label = (PlotLabel) dataSet.getRowKey(i);
            boolean isLr = label.getId() >= developments;

            Color color = isLr ? LINK_RATIO : FACTOR;
            lasr.setSeriesPaint(i, color);
            lasr.setSeriesFillPaint(i, color);
            lasr.setSeriesShape(i, circle);

            lasr.setSeriesShapesVisible(i, !isLr);
            lasr.setSeriesLinesVisible(i, isLr);
        }
    }

    return new ChartPanel(chart);
}

From source file:net.nosleep.superanalyzer.analysis.views.TimeView.java

private void createChart() {
    // create the chart...
    _chart = ChartFactory.createLineChart(Misc.getString("LISTENING_TIMES"), // chart
            // title
            Misc.getString("HOUR_OF_DAY"), // domain axis label
            Misc.getString("SONG_COUNT"), // range axis label
            _dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips
            false // urls
    );/* w  ww .  jav a2  s. c om*/

    CategoryPlot plot = (CategoryPlot) _chart.getPlot();
    plot.setRangePannable(true);
    plot.setRangeZeroBaselineVisible(false);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);

    _chart.addSubtitle(HomePanel.createSubtitle(Misc.getString("LISTENING_TIMES_TOOLTIP")));

    ChartUtilities.applyCurrentTheme(_chart);

    // format the renderer after applying the theme
    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setDrawOutlines(true);
    renderer.setUseFillPaint(true);
    renderer.setBaseFillPaint(Color.white);
    renderer.setSeriesStroke(0, new BasicStroke(3.0f));
    renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
    renderer.setSeriesShape(0, new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));

    // get rid of the little line above/next to the axis
    // plot.setRangeZeroBaselinePaint(Color.white);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(true);

    Misc.formatChart(plot);
    renderer.setSeriesPaint(0, Theme.getColorSet()[1]);

}

From source file:guineu.modules.filter.report.RTShift.ReportTask.java

/**
 * Create the chart and save it into a png file.
 * @param dataset/*from www .  j  a va2s  . c  o m*/
 * @param lipidName
 */
private void createChart(CategoryDataset dataset, String lipidName) {
    try {

        JFreeChart chart = ChartFactory.createLineChart("RT shift", "Samples", "RT", dataset,
                PlotOrientation.VERTICAL, false, false, false);

        // Chart characteristics
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        final NumberAxis axis = (NumberAxis) plot.getRangeAxis();
        axis.setAutoRangeIncludesZero(false);
        axis.setAutoRangeMinimumSize(1.0);
        LineAndShapeRenderer categoryRenderer = new LineAndShapeRenderer();
        categoryRenderer.setSeriesLinesVisible(0, false);
        categoryRenderer.setSeriesShapesVisible(0, true);
        plot.setRenderer(categoryRenderer);

        // Save all the charts in the folder choosen by the user
        ChartUtilities.saveChartAsPNG(new File(this.reportFileName + "/RT Shift:" + lipidName + ".png"), chart,
                1000, (500));
    } catch (IOException ex) {
        Logger.getLogger(ReportTask.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:intelligentWebAlgorithms.util.gui.XyGui.java

/**
 * @param title/*from w w  w  .jav  a2 s  .co m*/
 *            chart title
 * @param nameForData1
 *            identifier for a data group/series
 * @param nameForData2
 *            identifier for a data group/series
 * @param items
 *            values/categories that correspond to data values
 */
public XyGui(String title, String nameForData1, String nameForData2, String[] items, double[] data1,
        double[] data2) {

    super(title);
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int i = 0, n = items.length; i < n; i++) {
        dataset.addValue(data1[i], nameForData1, items[i]);
        dataset.addValue(data2[i], nameForData2, items[i]);
    }

    final JFreeChart chart = ChartFactory.createLineChart("User Similarity", "Items", "Rating", dataset,
            PlotOrientation.VERTICAL, true, true, false);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

From source file:guineu.modules.filter.report.areaVSheight.ReportTask.java

/**
 * Create the chart and save it into a png file.
 * @param dataset//  w  ww .  ja  v  a2s .c  om
 * @param lipidName
 */
private void createChart(CategoryDataset dataset, String lipidName) {
    try {

        JFreeChart chart = ChartFactory.createLineChart("Height/Area", "Samples", "Height/Area", dataset,
                PlotOrientation.VERTICAL, false, false, false);

        // Chart characteristics
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        final NumberAxis axis = (NumberAxis) plot.getRangeAxis();
        axis.setAutoRangeIncludesZero(false);
        LineAndShapeRenderer categoryRenderer = new LineAndShapeRenderer();
        categoryRenderer.setSeriesLinesVisible(0, false);
        categoryRenderer.setSeriesShapesVisible(0, true);
        plot.setRenderer(categoryRenderer);

        // Save all the charts in the folder choosen by the user
        ChartUtilities.saveChartAsPNG(new File(this.reportFileName + "/HeightvsArea:" + lipidName + ".png"),
                chart, 1000, 500);
    } catch (IOException ex) {
        Logger.getLogger(ReportTask.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:hudson.graph.jfreechart.JFreeChartSupport.java

public JFreeChart createChart() {

    if (chartType == Graph.TYPE_STACKED_AREA) {
        jFreeChart = ChartFactory.createStackedAreaChart(null, // chart
                chartTitle, // // title 
                xAxisLabel, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips
                false // urls
        );//from ww  w . j  a  v  a 2  s . co m
    } else if (chartType == Graph.TYPE_LINE) {
        jFreeChart = ChartFactory.createLineChart(null, // chart title
                chartTitle, // // title 
                xAxisLabel, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips
                false // urls
        );
    }

    jFreeChart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = jFreeChart.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);

    if (chartType == Graph.TYPE_LINE) {
        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseStroke(new BasicStroke(3));

        if (multiStageTimeSeries != null) {
            for (int i = 0; i < multiStageTimeSeries.size(); i++) {
                renderer.setSeriesPaint(i, multiStageTimeSeries.get(i).color);
            }
        }
    }

    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();
    Utils.adjustChebyshev(dataset, rangeAxis);
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    if (chartType == Graph.TYPE_STACKED_AREA) {
        StackedAreaRenderer ar = new StackedAreaRenderer2() {

            @Override
            public Paint getItemPaint(int row, int column) {
                if (row == 2) {
                    return ColorPalette.BLUE;
                }
                if (row == 1) {
                    return ColorPalette.YELLOW;
                }
                if (row == 0) {
                    return ColorPalette.RED;
                }
                ChartLabel key = (ChartLabel) dataset.getColumnKey(column);
                return key.getColor(row, column);
            }

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

            @Override
            public String generateToolTip(CategoryDataset dataset, int row, int column) {
                ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
                return label.getToolTip(row, column);
            }
        };
        plot.setRenderer(ar);
        ar.setSeriesPaint(0, ColorPalette.RED); // Failures.
        ar.setSeriesPaint(1, ColorPalette.YELLOW); // Skips.
        ar.setSeriesPaint(2, ColorPalette.BLUE); // Total.
    }

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

    return jFreeChart;
}

From source file:intelligentWebAlgorithms.util.gui.ScatterGui.java

/**
 * @param title/*from  www .  j  a  v a  2s .  c o m*/
 *            chart title
 * @param nameForData1
 *            identifier for a data group/series
 * @param nameForData2
 *            identifier for a data group/series
 * @param items
 *            values/categories that correspond to data values
 */
public ScatterGui(String title, String nameForData1, String nameForData2, String[] items, double[] data1,
        double[] data2) {

    super(title);
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int i = 0, n = items.length; i < n; i++) {
        dataset.addValue(data1[i], nameForData1, items[i]);
        dataset.addValue(data2[i], nameForData2, items[i]);
    }

    final JFreeChart chart = ChartFactory.createLineChart("User Similarity", "Items", "Rating", dataset,
            PlotOrientation.VERTICAL, true, true, false);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

From source file:org.bench4Q.console.ui.section.R_RealSessionShowSection.java

private JPanel printPic() throws IOException {
    double[][] value = new double[2][testduring];
    for (int i = 0; i < value[0].length; ++i) {
        value[0][i] = i;//ww w  .  j  a  v  a  2 s.c o  m
        value[1][i] = loadStart[i];
    }

    // calculate the avrg load start every second.
    int avrgLoad = 0;
    for (int i = 0; i < loadStart.length; i++) {
        avrgLoad += loadStart[i];
    }
    avrgLoad /= testduring;

    DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
    String series1 = "real";

    for (int i = 0; i < value[0].length; ++i) {
        defaultcategorydataset.addValue(value[1][i], series1, new Integer((int) value[0][i]));
    }

    JFreeChart chart = ChartFactory.createLineChart("REAL LOAD:" + avrgLoad, "time", "load",
            defaultcategorydataset, PlotOrientation.VERTICAL, true, true, false);
    chart.setBackgroundPaint(Color.white);
    CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setBackgroundPaint(Color.WHITE);
    categoryplot.setRangeGridlinePaint(Color.white);
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    numberaxis.setAutoRangeIncludesZero(true);
    LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
    lineandshaperenderer.setShapesVisible(false);
    lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 1F, 1F }, 0.0F));
    return new ChartPanel(chart);
}

From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java

public static void relatoriosFiliais() {
    String sql = "";

    try {/*from   w  ww .  ja v a 2  s  .c o m*/
        Connection conn = Conexoes.obterConexao();

        JDBCCategoryDataset ds = new JDBCCategoryDataset(conn, sql);

        ds.executeQuery(sql);

        JFreeChart grafico = ChartFactory.createLineChart("Grfico de Disponibilidade", "Filial", "Quantidade",
                ds, PlotOrientation.HORIZONTAL, true, true, true);

    } catch (SQLException | ClassNotFoundException ex) {
        System.err.println(ex.getMessage());
    }
}