2013-10-29 51 views
0

下面的代码在没有数据序列图结果被描绘,被装载海军报不显示GSON数据

[email protected]:~$ cat /path/to/files/data/myFile.txt 
1,1 
2,1 
3,1 

的Java 实际数据..

import java.util.Vector; 

    public class Dataset { 

    private String name; 
    private Vector<GraphPoint> points; 

    public Dataset(String nameTemp){ 

     this.name = nameTemp; 
     this.points = new Vector<GraphPoint>(); 
    } 
} 

从servlet的发送Vector<Dataset>使用AJAX的JavaScript ,使用数据序列化response.getWriter().write(new Gson().toJson(datasets));

javascript ..

$(".body").append('<div id="content"><div class="demo-container"><div id="placeholder" class="demo-placeholder"></div></div></div>'); 
var datasets = JSON.parse(xmlhttp.responseText); 
//alert(xmlhttp.responseText);   
var plotarea = $("#placeholder"); 
$.plot(plotarea, [datasets[0].points, datasets[1].points]); 

输出..

[ 
    {"name":"myFile.txt", 
    "points":[ 
     {"timestamp":30,"value":100}, 
     {"timestamp":31,"value":101}, 
     {"timestamp":32,"value":110} 
    ]}, 
    {"name":"anotherFile.txt", 
    "points":[ 
     {"timestamp":1382987630,"value":200}, 
     {"timestamp":1382987631,"value":201}, 
     {"timestamp":1382987632,"value":205} 
    ]} 
] 
+0

你的数据不正确的格式看这里:https://github.com/flot/flot/blob/master/API .MD#数据格式。 – Mark

+0

hm,所以'Dataset'需要重新定义?也许'私人向量分;'不是一个很好的方法来表示数据系列 – bobbyrne01

+0

不,GraphPoint是一些特定的java对象。它看起来像JSON转换器做的最好,并将其转换为一个JavaScript对象,属性为“timestamp”和“value”,但flot不会理解这一点。尝试'私人向量>',你需要将某些东西分解为'[[x1,y1],[x2,y2]等等] – Mark

回答

0

得到正确的格式,对于图表中的每个系列创建Dataset对象

public class Dataset { 

    private String name; 
    private Vector<Vector<Integer>> points; 

读系列从每个文件中的数据到Vector<Dataset>

File[] files = finder("/path/to/files/" + request.getParameter("data") + "/"); 

    Vector<Dataset> datasets = new Vector<Dataset>(); 

    for (int i = 0; i < files.length; i++){ 

     datasets.add(readData(files[i])); 
    } 

发送json数据给客户端

response.setContentType("application/json"); 
response.getWriter().write(new Gson().toJson(datasets)); 

System.out.println(new Gson().toJson(datasets)); 

系统输出..

[{"name":"myFile.txt","points":[[1,1],[2,1],[3,1]]},{"name":"anotherFile.txt","points":[[0,10],[2,20],[5,50]]}]

private Dataset readData(File file){ 

    Dataset dataset = new Dataset(file.getName()); 

    try{ 
     FileInputStream fstream = new FileInputStream(file); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 

     while ((strLine = br.readLine()) != null){ 

      Vector<Integer> points = new Vector<Integer>(); 

      points.add(Integer.parseInt(strLine.split(",")[0])); 
      points.add(Integer.parseInt(strLine.split(",")[1])); 

      dataset.addset(points); 
     } 

     in.close(); 

    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return dataset; 
} 

private File[] finder(String dirName) { 

    File dir = new File(dirName); 

    return dir.listFiles(new FilenameFilter() { 
     public boolean accept(File dir, String filename) { 
      return filename.endsWith(".txt"); 
     } 
    }); 
}