Data Not Displaying Correctly On JFreeChart

Loading data from a text file and displaying it on a JFreeChart. However, the data is not plotted on the graph and the Y Axis is not labelled correctly using the highest and lowest values from the dataset of the text file. Would be hugely appreciated if someone could add the code to correct this problem! Text File is formatted as follows:

Title: Effect of Age on Ability
Xlabel: Age
Ylabel: Ability
start: 30
interval: 20
0, 7, 9, 10, 14, 15, 20

Code for the whole program below:

package test;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.nio.charset.Charset;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

public class DataVisualisation extends JFrame {

public DataVisualisation() {
setTitle("Data Visualisation Tool"); // Sets Title to Display at Top of Program
setSize(900, 600); // Sets Default Size Program Should Display

// Creating Main Menu Bar
final JMenuBar mainmenu = new JMenuBar(); // Creates menu bar for top of form
JMenu file = new JMenu("File"); // Creates File option
JMenu help = new JMenu("Help"); // Creates Help option
mainmenu.add(file); // Adds File option to menu bar
mainmenu.add(help); // Adds Help option to menu bar

// Creating Menu Bar Items

final JMenuItem open = new JMenuItem("Open"); // Creates Open option
final JMenuItem save = new JMenuItem("Save"); // Creates Save option
final JMenuItem exit = new JMenuItem("Exit"); // Creates Exit option
final JMenuItem about = new JMenuItem("About"); // Creates About option

// Creating Text Area

final JTextArea maintextarea = new JTextArea(8, 16); // Creates Main Text Area

JScrollPane maintextscrollpane = new JScrollPane(maintextarea); // Adds Scroll Pane to Main Text Area

// Creates Area to Display Graph Graphic

ChartPanel graphpanel = new ChartPanel(null); // Creates Container for Graph

// Setting up Main SplitPane on the Form

JSplitPane mainsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, maintextscrollpane, graphpanel);
add(mainsplitpane, BorderLayout.LINE_END);

// Setting up Open File Option in Main Menu

 file.add(open); // Adds Open Option to File Menu Within Main Menu Bar
 open.addActionListener(new ActionListener() {

 public void actionPerformed(ActionEvent arg0) {
 if (arg0.getSource().equals(open)) {
JFileChooser chooser = new JFileChooser(new File(".")); // Creates FileChooser for Open Option
            if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile(); // FileChooser Gets File and Stores in File
            graphpanel.setChart(createChart(file)); // Setting Panel to display Data from File

// Creating Variables to Store Data Read by BufferedReader

 private JFreeChart createChart(File file) {
       String title = null;
       String xAxisLabel = null;
       String yAxisLabel = null;
       BufferedReader reader = null;
       int start = 0;
       int interval = 0;
       String data = null;
       String line = null;

// Reading and Splitting Text File     

 try {
    reader = new BufferedReader(new FileReader(file));
      while ((line = reader.readLine()) != null) {
          maintextarea.append(line + "n");
              if (line.startsWith("Title")) { // If line begins with "Title"
                   title = line.split(":")[1].trim(); // line should be split by the : between Heading and Value
                 if (!line.contains(":")) {
                        data = line;
                 if (!line.startsWith("XLabel")) {
                        xAxisLabel = line;
            } catch (IOException ex) {

// Collecting Data Set

 XYSeries dataset = new XYSeries(file.getName());
       for (String s : data.split(",")) {
             dataset.add(start, Double.valueOf(s));
             start += interval;

// Displays Chart with Labelled Axis

       return ChartFactory.createXYLineChart(title, 
                xAxisLabel, yAxisLabel, new XYSeriesCollection(dataset));


 // Adding Save and Exit Options to File Menu within Main Menu


// Setting up Exit Button

  exit.addActionListener(new ActionListener() { 

 public void actionPerformed(ActionEvent arg0) {
       if (arg0.getSource().equals(exit)) {

 // Adding Final Options to Main Menu

 help.add(about); // Adding Help Button To Display About Page
 about.addActionListener(new ActionListener() {

 // Creating Pane Displaying Text About the Program

public void actionPerformed(ActionEvent arg0) {
"Tool used to display data from text file as graph.",
"Help", JOptionPane.PLAIN_MESSAGE); // Displays the message on the pane

public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        new DataVisualisation(); // Executes Before Processing Rest of Code

Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.