2012-05-27 2 views
0

나는 오랫동안 GNUPLOT을 사용하고 있지만, 지금은 클래스 JFreeChart의를 사용하여 구현할 수있어로 된 JFreeChart를 사용합니다. 내가 파일 "data.txt로"이 데이터를 가지고방법의 gnuplot-형식의 텍스트 파일

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

내가 JFreeChart의를 사용하여 차트를 인쇄하고 다른 파일 f.e.에 저장하려면 : 나는 텍스트 파일 형식의 일종처럼 GNUPLOT있다 "result.png".

당신은 나에게 코드의 일부 조각을 줄 수 있을까요?

때문에 유용, 나는 guava lib를 사용하여 작성, 미리 예를 들어

+0

당신은 무엇을 시도? 당신은 어떤 [예] 봤어 (http://stackoverflow.com/tags/jfreechart/info)? – trashgod

답변

1

에 감사드립니다.

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); 
    } 
}