Can anyone provide step by step instructions for using jfreechart in IntelliJ project

I cannot get JFreeChart to work in IntelliJ.

What I have done so far:

  • Using Win 7, IntelliJ 13.1.4 and Java 1.7
  • I am relatively new to IntelliJ
  • Started a new project in IntelliJ which creates the default root \ out and root \ src directories
  • Added own directory root \ lib
  • Place external libraries there as .jar and .zip files (jcommon-1.0.23.zip, jfreechart-1.0.19.zip, junit-4.7.jar)
  • The Project Structure Dialog Box Opened in IntelliJ (Ctrl + Alt + Shift + S)
  • In the left pane select "Modules"
  • This project is selected in the middle pane (the only one that was there)
  • In the right pane, under the Sources tab, select the src directory
  • Switched to the "Dependencies" tab
  • Click the green "+" button on the right.
  • Selected "2 Library ...> Java"
  • Goed to root \ lib and selected one of the .zip / .jar files.
  • The Revealed Roots dialog box appears - I just clicked OK
  • The Library Setup dialog box appears - I gave the library a sensible name and clicked OK
  • The library appears in the Dependencies tab next to it, which looks like some books and is the same symbol as the one next to the External Libraries node in the project tool window (so I assume it is the symbol for the library)
  • Repeat for the other two libraries
  • In the source file for the project, I tried to type "import org.jfree. *;" but when I type "jfree" the bit is red and when I hit enter at the end of the line the line disappears.
  • I also tried to type "JFreeChart jFreeChart = new JFreeChart ();" but JFreeChart turns red and when I click on it I have no way to import; only create class, interface, enum, etc.
  • By the way, running jUnit tests works fine
  • Also by the way, the "External Libraries" node in the project tool window shows <1.7> (ie JDK) but does not show jUnit, jCommon or jFreeChart.

I've seen similar questions and the answers seem to indicate what I did correctly. I also checked the IntelliJ documentation and also indicated that what I did is correct. However, this clearly doesn't work for me. Perhaps I misunderstood something.

Can anyone help me get JFreeChart working?

thank

EDIT: jUnit worked, but now it doesn't. D'o!

+3


source to share


2 answers


I haven't used IntelliJ before, but I downloaded it and immediately created a new project called JFreeChartExample File -> New Project

. Then I clicked File -> Project Structure...

and selected entry Libraries

, then clicked the icon +

to add both jcommon-1.0.23.jar

and jfreechart-1.0.19.jar

(which I had on my local filesystem after unzipping the JFreeChart distribution). Then in the directory src/

I created a new Java file BarChartDemo.java

(see below). Now my project looks like this:

enter image description here

BarChartDemo1.java looks like this:



/* ==================
 * BarChartDemo1.java
 * ==================
 *
 * Copyright (c) 2005-2014, Object Refinery Limited.
 * All rights reserved.
 *
 * http://www.jfree.org/jfreechart/index.html
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the Object Refinery Limited nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * Changes
 * -------
 * 09-Mar-2005 : Version 1 (DG);
 * 11-Mar-2014 : Use new ChartFactory method (DG);
 * 25-Jun-2014 : Update to use real data (DG);
 * 
 */

import java.awt.Color;
import java.awt.Dimension;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a bar chart.
 */
public class BarChartDemo extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    static {
        // set a theme using the new shadow generator feature available in
        // 1.0.14 - for backwards compatibility it is not enabled by default
        ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
                true));
    }

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public BarChartDemo(String title) {
        super(title);
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setBackground(null);
        chartPanel.setFillZoomRectangle(true);
        chartPanel.setMouseWheelEnabled(true);
        chartPanel.setDismissDelay(Integer.MAX_VALUE);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Returns a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(7445, "JFreeSVG", "Warm-up");
        dataset.addValue(24448, "Batik", "Warm-up");
        dataset.addValue(4297, "JFreeSVG", "Test");
        dataset.addValue(21022, "Batik", "Test");
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createBarChart(
                "Performance: JFreeSVG vs Batik", null /* x-axis label*/,
                "Milliseconds" /* y-axis label */, dataset);
        chart.addSubtitle(new TextTitle("Time to generate 1000 charts in SVG "
                + "format (lower bars = better performance)"));
        chart.setBackgroundPaint(null);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(null);

        // ******************************************************************
        //  More than 150 demo applications are included with the JFreeChart
        //  Developer Guide...for more information, see:
        //
        //  >   http://www.object-refinery.com/jfreechart/guide.html
        //
        // ******************************************************************

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        chart.getLegend().setFrame(BlockBorder.NONE);
        return chart;
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        BarChartDemo demo = new BarChartDemo("JFreeChart: BarChartDemo1.java");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

      

I can right click on this file in the project and run it to get this:

enter image description here

+8


source


I spent a couple of hours figuring out this issue and I finally got it to work.

You need to download the .jar files You download the ZIP files from the JFreeChart website.



the link of both files is given below.

http://mvnrepository.com/artifact/org.jfree/jcommon/1.0.23 http://mvnrepository.com/artifact/org.jfree/jfreechart/1.0.19

0


source







All Articles