flot 不显示 gson 数据
flot not displaying gson data
下面的代码导致图形没有绘制数据系列,正在加载的实际数据
rob@workLaptop:~$ cat /path/to/files/data/myFile.txt
1,1
2,1
3,1
爪哇岛。。
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>();
}
}
使用 AJAX 将Vector<Dataset>
从 servlet 发送到 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]);
alert(xmlhttp.responseText);
输出..
[
{"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}
]}
]
获得正确的格式,对于图形中的每个系列创建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");
}
});
}
相关文章:
- 用程序搜索JQuery数据表中的文本
- 要求输入在数据列表中
- 正在将数据主题添加到所有项目
- 函数参数中的数据与指定变量之间的任何性能差异
- 在VanillaJS中模拟模型双向数据绑定
- CSS-如何定位内容数据标题
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 序列化数据属性中对象的最可靠方法
- 如何将JSON数据导入我的ejs模板
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 画布数据到图像
- 使用jquery将mysql数据获取到新的表行中
- 使用html中的外部javascript进行数据验证
- 有时数据是't显示在浏览器中
- React中的数据集表示
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- flot 不显示 gson 数据