XY chart using the data in .csv file

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
raybrown2
Posts: 4
Joined: Wed Sep 05, 2012 5:47 pm
antibot: No, of course not.

XY chart using the data in .csv file

Post by raybrown2 » Wed Sep 05, 2012 6:15 pm

Now i can only single line from XY chart. How can I more other line from .csv file?
Code

Code: Select all

import java.io.*;
import java.util.StringTokenizer;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

/**
 * This class creates an XY chart using the data in gc.csv.
 *
 * @author R. Mark Volkmann, Object Computing, Inc.
 */
public class ChartCreator {

    private static final boolean SHOW_LEGEND = false;
    private static final boolean SHOW_TOOLTIPS = false;
    private static final boolean GENERATE_URLS = false;

    public static void main(String[] args) throws IOException {
        FileReader fr;
        fr = new FileReader("D:/gc.csv");
        BufferedReader br = new BufferedReader(fr);

        // Get the x-axis label from the first token in the first line
        // and the y-axis label from the last token in the first line.
        String line = br.readLine();
        StringTokenizer st = new StringTokenizer(line, ",");
        String xLabel = st.nextToken();
        String yLabel = st.nextToken();
        while (st.hasMoreTokens()) {
            yLabel = st.nextToken();
        }

        String title = yLabel + " by " + xLabel;

        // Get the data to plot from the remaining lines.
        float minY = Float.MAX_VALUE;
        float maxY = -Float.MAX_VALUE;
        XYSeries series = new XYSeries("?");
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            st = new StringTokenizer(line, ",");

            // The first token is the x value.
            String xValue = st.nextToken();

            // The last token is the y value.
            String yValue = "";
            while (st.hasMoreTokens()) {
                yValue = st.nextToken();
            }

            float x = Float.parseFloat(xValue);
            float y = Float.parseFloat(yValue);
            series.add(x, y);

            minY = Math.min(y, minY);
            maxY = Math.max(y, maxY);
        }        

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);       

        JFreeChart chart = ChartFactory.createXYLineChart(
            title, xLabel, yLabel, dataset,
            PlotOrientation.VERTICAL,
            SHOW_LEGEND, SHOW_TOOLTIPS, GENERATE_URLS);
        
        XYPlot plot = chart.getXYPlot();
        plot.getRangeAxis().setRange(minY, maxY);
        
        int width = 1000;
        int height = 600;
        ChartUtilities.saveChartAsPNG(
            new File("D:/gc.png"), chart, width, height);
    }
}
gc.csv

Code: Select all

Max Pause Goal,Minor Collections,Major Collections,Pause Count,Max Pause,GC Time,Total Time, Throughput
0,49,0,49,0.005,0.081,1.831,95.599
100,49,0,49,0.005,0.077,1.828,95.785
200,49,0,49,0.005,0.081,1.829,95.550
300,47,0,47,0.009,0.089,1.837,95.145
400,48,0,48,0.005,0.081,1.835,95.598
500,48,0,48,0.005,0.078,1.825,95.729
600,49,0,49,0.005,0.081,1.830,95.600
700,48,0,48,0.005,0.081,1.828,95.564
800,44,0,44,0.017,0.094,1.857,94.919
900,49,0,49,0.006,0.082,1.833,95.533
1000,49,0,49,0.005,0.088,1.840,95.224
Picture, Now only "Max Pause Goal". How can I get "Minor Collections,Major Collections,Pause Count,Max Pause,GC Time,Total Time"?
Image

my DataSet, I need "Time" is X. "Value 1", "Value 2", "Value 3" and "Value 4" is Y (multiple line).

Code: Select all

Time,Value 1,Value 2,Value 3,Value 4
1,0.357824926,0.356253751,0.356965197,0.351903118
2,0.35439012,0.347749191,0.357049848,0.356777756
3,0.355389286,0.353831366,0.354736911,0.350102632
4,0.356416076,0.356777756,0.356253751,0.353831366
Thank you.
Ray

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: XY chart using the data in .csv file

Post by david.gilbert » Tue Sep 11, 2012 8:14 pm

I think if you just step through the code you'll see the problem. For each line in the file, you fetch the first value into xLabel, then you loop over the remaining values assigning them to yLabel. Only when you get to the last value do you add the (x, y) pair to the series. So the series that you add to the chart only has the initial value (x) and last value (y) from each line.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

raybrown2
Posts: 4
Joined: Wed Sep 05, 2012 5:47 pm
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by raybrown2 » Wed Sep 12, 2012 7:56 pm

Have other series that can help my problem?

raybrown2
Posts: 4
Joined: Wed Sep 05, 2012 5:47 pm
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by raybrown2 » Fri Sep 14, 2012 9:43 am

This is my new code.

Code: Select all

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class LineChartDemo2 extends ApplicationFrame
{

        public LineChartDemo2(String s) throws FileNotFoundException, IOException
        {
                super(s);
                JPanel jpanel = createDemoPanel();
                jpanel.setPreferredSize(new Dimension(500, 270));
                setContentPane(jpanel);
        }

        private static XYDataset createDataset() throws FileNotFoundException, IOException
        {
        
        //Line 1     
        FileReader fr;
        fr = new FileReader("1.csv");
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        StringTokenizer st = new StringTokenizer(line, ",");
            // Get the data to plot from the remaining lines.
        float minY = Float.MAX_VALUE;
        float maxY = -Float.MAX_VALUE;                           
        XYSeries series = new XYSeries("First");
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            st = new StringTokenizer(line, ",");
            // The first token is the x value.
            String xValue = st.nextToken();
            // The last token is the y value.
            String yValue = "";
            while (st.hasMoreTokens()) {
                yValue = st.nextToken();
            }
            float x = Float.parseFloat(xValue);
            float y = Float.parseFloat(yValue);
            series.add(x, y);                        
            minY = Math.min(y, minY);
            maxY = Math.max(y, maxY);
        }
        
        //Line 2
        FileReader fr2;
        fr2 = new FileReader("2.csv");
        BufferedReader br2 = new BufferedReader(fr2);
        String line2 = br2.readLine();
        StringTokenizer st2 = new StringTokenizer(line2, ",");
            // Get the data to plot from the remaining lines.
        float minY2 = Float.MAX_VALUE;
        float maxY2 = -Float.MAX_VALUE;                            
        XYSeries series2 = new XYSeries("Second");
        while (true) {
            line2 = br2.readLine();
            if (line2 == null) {
                break;
            }
            st2 = new StringTokenizer(line2, ",");
            // The first token is the x value.
            String xValue2 = st2.nextToken();
            // The last token is the y value.
            String yValue2 = "";
            while (st2.hasMoreTokens()) {
                yValue2 = st2.nextToken();
            }
            float x = Float.parseFloat(xValue2);
            float y = Float.parseFloat(yValue2);
            series2.add(x, y);                        
            minY = Math.min(y, minY);
            maxY = Math.max(y, maxY);
        }
                //Add Series
                XYSeriesCollection xyseriescollection = new XYSeriesCollection();
                xyseriescollection.addSeries(series);
                xyseriescollection.addSeries(series2);
                //xyseriescollection.addSeries(series3);
                return xyseriescollection;                                 
        }

        private static JFreeChart createChart(XYDataset xydataset)
        {
                JFreeChart jfreechart = ChartFactory.createXYLineChart("LineChart", "Time", "Value", xydataset, PlotOrientation.VERTICAL, true, true, false);
                XYPlot xyplot = (XYPlot)jfreechart.getPlot();
                xyplot.setDomainPannable(true);
                xyplot.setRangePannable(true);
                XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
                xylineandshaperenderer.setBaseShapesVisible(false);
                xylineandshaperenderer.setBaseShapesFilled(true);
                NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
        numberaxis.setRange(0.0, 1.0); 
        numberaxis.setTickUnit(new NumberTickUnit(0.1));         
                return jfreechart;
        }

        public static JPanel createDemoPanel() throws FileNotFoundException, IOException
        {
                JFreeChart jfreechart = createChart(createDataset());
                ChartPanel chartpanel = new ChartPanel(jfreechart);
                chartpanel.setMouseWheelEnabled(true);
                return chartpanel;
        }

        public static void main(String args[]) throws FileNotFoundException, IOException
        {
                LineChartDemo2 linechartdemo2 = new LineChartDemo2("JFreeChart: LineChartDemo2.java");
                linechartdemo2.pack();
                RefineryUtilities.centerFrameOnScreen(linechartdemo2);
                linechartdemo2.setVisible(true);
        }
}
This is my CSV files, 1.csv and 2.csv

Code: Select all

1, 0.5001170002830332
2, 0.48986439934622067
3, 0.4119907876937371
4, 0.37489872690608866
5, 0.36404831177215796
6, 0.3608101803629192
7, 0.35947516760068965
8, 0.3588008603068371
9, 0.35866558484522504
10, 0.3587927341425264
11, 0.35896093298651927
12, 0.3591503330290219
13, 0.35936574987163256
14, 0.3595196500032803
15, 0.3595440705720756
16, 0.35946444729109256
17, 0.3593289983679418
18, 0.35917679427877613
19, 0.35903951451092325
20, 0.35894685753777933
21, 0.35892459144673516
22, 0.3589806743849561
23, 0.35909881967462315
24, 0.35925437638285995
25, 0.35942936584134855
26, 0.3596130143491107
27, 0.35979802561173213
28, 0.3599786250183287
29, 0.3601501561878679
30, 0.36030913803666587
31, 0.36045325277033624
32, 0.3605811815766637
33, 0.3606923639787578
34, 0.36078676092125544
35, 0.36086466284261365
36, 0.36092655127581497
37, 0.3609730064537472
38, 0.36100464946531263
39, 0.3610221092135064
40, 0.36102600763786885
41, 0.3610169595699019
42, 0.3609955856000196
43, 0.3609625373144113
44, 0.3609185343024693
45, 0.3608644116012621
46, 0.36080117355285335
47, 0.36073003962747263
48, 0.36065244223590065
49, 0.36056991262852656
50, 0.36048382867302076
51, 0.3603951182920898
52, 0.36030409082383136
53, 0.36021048573489295
54, 0.36011367785079207
55, 0.3600129198099386
56, 0.35990754601108765
57, 0.3597971286488036
58, 0.3596816189696653
59, 0.3595615256319339
60, 0.35943818950444795
61, 0.35931421066301916
62, 0.3591940392789901
63, 0.35908459047045027
64, 0.3589955083495894
65, 0.3589391632529959
66, 0.35893342155669317
67, 0.3590100143848961
68, 0.3592068669785042
69, 0.3595216199610354
70, 0.35989685422248424
71, 0.3602682219752656
72, 0.36060202757868426
73, 0.3608930896342382
74, 0.3611486162146957
75, 0.36137763968099706
76, 0.3615872271176274
77, 0.3617820666236098
78, 0.3619650827153795
79, 0.3621381061514978
80, 0.36230234482141965
81, 0.3624586629492411
82, 0.36260773206931607
83, 0.3627501083940586
84, 0.36288627154107894
85, 0.3630166442328104
86, 0.36314160311197896
87, 0.3632614855873626
88, 0.36337659493292107
89, 0.36348720455999656
90, 0.36359356179494207
91, 0.36369589125719864
92, 0.3637943978553482
93, 0.36388926940663746
94, 0.3639806788948467
95, 0.3640687863935358
96, 0.3641537406901677
97, 0.36423568065027295
98, 0.3643147363605762
99, 0.36439103008721124
100, 0.3644646770810134

1, 0.4944893337363993
2, 0.422388812664403
3, 0.3782959220557828
4, 0.363438885135979
5, 0.35915066679945556
6, 0.3585752108855908
7, 0.35844490267241147
8, 0.35798727374723177
9, 0.3575662897221727
10, 0.3573610830139213
11, 0.35724718781667625
12, 0.35719370302970493
13, 0.3572490567177562
14, 0.35736916990455686
15, 0.3574808613641326
16, 0.3575680783073925
17, 0.35764650593522357
18, 0.3577311068853596
19, 0.3578252802056498
20, 0.3579236623364854
21, 0.35801864509156317
22, 0.35810400774295764
23, 0.3581764275472208
24, 0.3582362711801129
25, 0.35828740422716376
26, 0.35833583396500107
27, 0.3583877521615969
28, 0.3584478782208889
29, 0.3585186952040938
30, 0.35860057335905116
31, 0.3586923904560336
32, 0.3587922478628278
33, 0.3588980599017537
34, 0.3590079540638527
35, 0.35912049722117556
36, 0.3592347849708367
37, 0.3593504328491989
38, 0.3594675052915705
39, 0.359586413951627
40, 0.35970781105519406
41, 0.3598324959168679
42, 0.359961344467775
43, 0.36009526395840125
44, 0.3602351691500808
45, 0.36038197293390806
46, 0.3605365833105896
47, 0.3606998994263636
48, 0.3608728011978151
49, 0.3610561294840467
50, 0.3612506565376518
51, 0.3614570494157674
52, 0.36167583177203994
53, 0.36190735115392153
54, 0.3621517583925216
55, 0.3624090018033262
56, 0.3626788315512948
57, 0.3629608001132235
58, 0.36325423643831695
59, 0.3635581687054828
60, 0.3638711793727613
61, 0.3641912033269957
62, 0.3645153285852318
63, 0.3648397152235342
64, 0.3651597658842063
65, 0.36547060053386843
66, 0.3657677143300035
67, 0.3660475584132722
68, 0.36630782173963067
69, 0.36654737864347753
70, 0.3667660283426788
71, 0.36696418377588436
72, 0.36714260782219643
73, 0.36730222684750063
74, 0.3674440130402615
75, 0.36756891556873805
76, 0.3676778224102859
77, 0.36777154005289886
78, 0.3678507830605441
79, 0.3679161687888708
80, 0.3679682145450885
81, 0.3680073356308951
82, 0.3680338433376086
83, 0.3680479423052109
84, 0.36804972684803255
85, 0.3680391759709146
86, 0.3680161469065206
87, 0.3679803671485396
88, 0.3679314252047341
89, 0.3678687607593355
90, 0.36779165581378553
91, 0.3676992300286817
92, 0.36759044657643825
93, 0.3674641405214266
94, 0.3673190921410722
95, 0.36715418601063754
96, 0.36696872741587294
97, 0.3667630326056783
98, 0.3665394535264218
99, 0.3663039661508706
100, 0.3660681268084698
Have any anyone can improve it?
And I want add more series(csv files, file 1 - n) line by adjust variable loop(i ++).

Thank you.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: XY chart using the data in .csv file

Post by John Matthews » Fri Sep 14, 2012 9:35 pm

You may want to factor out a separate method and use readLine() in a more idiomatic way. Note also that the returned XYSeries knows its own min/max.

Code: Select all

private static XYSeries readSeries(String name) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(name));
    String line;
    XYSeries series = new XYSeries(name);
    while ((line = br.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(line, ",");
        // The first token is the x value.
        String xValue = st.nextToken();
        // The last token is the y value.
        String yValue = "";
        while (st.hasMoreTokens()) {
            yValue = st.nextToken();
        }
        float x = Float.parseFloat(xValue);
        float y = Float.parseFloat(yValue);
        series.add(x, y);
    }
    return series;
}
...
xyseriescollection.addSeries(readSeries("1.csv"));
xyseriescollection.addSeries(readSeries("2.csv"));

raybrown2
Posts: 4
Joined: Wed Sep 05, 2012 5:47 pm
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by raybrown2 » Sun Sep 16, 2012 4:44 am

Perfect!
thank you very much.

regard
ray

sethsaurabh
Posts: 1
Joined: Thu May 26, 2016 10:04 am
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by sethsaurabh » Thu May 26, 2016 10:17 am

Hi Ray
\
Can you share you final Code?

ranjeet
Posts: 3
Joined: Sat May 14, 2016 11:39 am
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by ranjeet » Sat Aug 20, 2016 2:00 pm

Here is my code, It works fine but I am not sure about efficiency.

Code: Select all

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a line chart using
 * data from an {@link XYDataset}.
 *
 */
public class LineChartDemo extends ApplicationFrame {

	/**
	 * Creates a new demo.
	 *
	 * @param title
	 *            the frame title.
	 * @throws IOException
	 */
	public LineChartDemo(final String title, String data, String delim) throws IOException {

		super(title);

		final XYDataset dataset = createDataset(data, delim);
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);

	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return a sample dataset.
	 */
	private XYDataset createDataset(String data, String delim) throws IOException {

		BufferedReader br = new BufferedReader(new FileReader(data));
		String line;
		int lineNumber = 1;
		int tokencount = 0;
		ArrayList<String> label = new ArrayList<>();
		while ((line = br.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(line, delim);
			if (lineNumber == 1) {
				while (st.hasMoreTokens()) {
					label.add(st.nextToken());
					tokencount++;
				}
				lineNumber++;
			}
		}
		double ys[] = new double[tokencount];
		;

		XYSeries seriesa[] = new XYSeries[ys.length];
		for (int i = 0; i < ys.length; i++) {
			seriesa[i] = new XYSeries(label.get(i));
		}
		lineNumber = 1;

		br = new BufferedReader(new FileReader(data));
		while ((line = br.readLine()) != null) {
			if (lineNumber == 1) {// Don't read first line
				lineNumber++;
			} else {
				StringTokenizer st = new StringTokenizer(line, delim);
				int k = 0;
				while (st.hasMoreTokens()) {
					ys[k] = Double.parseDouble(st.nextToken());
					k++;
				}
				for (int j = 0; j < ys.length - 1; j++) {
					seriesa[j].add(ys[0], ys[j + 1]);
					// System.out.println(ys[j]);
				}
			}
		}
		br.close();
		XYSeriesCollection dataset = new XYSeriesCollection();
		for (int i = 0; i < ys.length; i++) {
			dataset.addSeries(seriesa[i]);
		}
		return dataset;

	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            the data for the chart.
	 * 
	 * @return a chart.
	 */
	private JFreeChart createChart(final XYDataset dataset) {

		// create the chart...
		final JFreeChart chart = ChartFactory.createXYLineChart("Line Chart Demo", // chart
																					// title
				"X", // x axis label
				"Y", // y axis label
				dataset, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
		);

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

		// final StandardLegend legend = (StandardLegend) chart.getLegend();
		// legend.setDisplaySeriesShapes(true);

		// get a reference to the plot for further customisation...
		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.lightGray);
		// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);

		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, false);
		renderer.setSeriesShapesVisible(1, false);
		plot.setRenderer(renderer);

		// change the auto tick unit selection to integer units only...
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}

	public static void main(final String[] args) throws IOException {

		final LineChartDemo demo = new LineChartDemo("Line Chart Demo", "2.csv", ",");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}

}
Output: Image

ranjeet
Posts: 3
Joined: Sat May 14, 2016 11:39 am
antibot: No, of course not.

Re: XY chart using the data in .csv file

Post by ranjeet » Sat Aug 20, 2016 2:04 pm

sethsaurabh wrote:Hi Ray
\
Can you share you final Code?
you can see my answer

Locked