0

I've been using GNUPLOT for a long time but now I've got to implement class using JFreeChart. I have gnuplot text file formated kind like:

Name_1 Name_2
1 4
2 4
3 1
4 5
5 3

I've got this data in file "data.txt" and I want to print chart using JFreeChart and save it to another file f.e. "result.png".

Could you give me some pieces of code?

Thank you in advance

1
  • What have you tried? Have you looked at any examples? Commented May 27, 2012 at 14:44

1 Answer 1

1

for example, I wrote using the guava lib, so useful.

import java.io.File;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

public class App {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Function<String, double[]> lineToXy = new Function<String, double[]>() {
            @Override
            public double[] apply(String line) {
                String[] s = line.split(" ", 0);
                double x = Double.parseDouble(s[0]);
                double y = Double.parseDouble(s[1]);
                return new double[] { x, y };
            }
        };

        try {
            //input file name is data.txt
            File file = new File("./data.txt");
            List<String> lines = Files.readLines(file, Charsets.UTF_8);
            List<double[]> xyList = Lists.transform(lines, lineToXy);
            XYSeriesCollection data = new XYSeriesCollection();
            XYSeries series = new XYSeries("XY Series");
            for (double[] xy : xyList) {
                series.add(xy[0], xy[1]);
            }
            data.addSeries(series);

            JFreeChart chart = ChartFactory.createXYLineChart("Tilte",
                "xLabel", "yLabel", data, PlotOrientation.VERTICAL, true,
                false, false);
            //output png file name is graph.png
            File png = new File("./graph.png");
            ChartUtilities.saveChartAsPNG(png, chart, 400, 300);
        } catch (Exception e) {
            //error handling
        } finally {
            //file close,and so on.
        }
        System.exit(0);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.