在echarts官网中下载的模板数据都是固定的,而我的项目需要数据是动态改变的,所以我试了很多方法来解决这个问题,其中成功的一种方法是在Servlet中先生成json数据格式,之后再把内容写到一个json文件中,在HTML中在调用这个json文件就可以了。
先生成json数据格式
JSONObject q1 = new JSONObject();
JSONObject q2 = new JSONObject();
JSONObject q3 = new JSONObject();
JSONObject q4 = new JSONObject();
JSONArray array = new JSONArray();
JSONArray array2 = new JSONArray();
JSONArray array3 = new JSONArray();
JSONObject w1 = new JSONObject();
JSONObject w2 = new JSONObject();
JSONObject s = new JSONObject();
JSONArray htags = new JSONArray();
for(int i=0;i<beank.size();i++)
{
q1=new JSONObject();
q1.put("name", beank.get(i).getDISTNAME());
System.out.println(beank.get(i).getDISTNAME());
array.add(q1);
}
for(int i=0;i<beanl.size();i++)
{
q1=new JSONObject();
q1.put("name", beanl.get(i).getSTOCKNAME());
System.out.println(beanl.get(i).getSTOCKNAME());
array2.add(q1);
}
w1.put("name", "对外投资");
w1.put("children", array);
w2.put("name", "股东");
w2.put("children", array2);
array3.add(w1);
array3.add(w2);
s.put("children", array3);
s.put("name", corpbean.getCORPNAME());
在规定写入的文件并写入
Tool tool=new Tool();
File file=new File("F:\\web\\Company\\WebContent\\data\\Text.json");
if(!file.exists())//判断文件是否存在,若不存在则新建
{
file.createNewFile();
}
FileOutputStream fileOutputStream=new FileOutputStream(file);//实例化FileOutputStream
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");//将字符流转换为字节流
BufferedWriter bufferedWriter= new BufferedWriter(outputStreamWriter);//创建字符缓冲输出流对象
String jsonString=s.toString();//将jsonarray数组转化为字符串
bufferedWriter.write(jsonString);//将格式化的jsonarray字符串写入文件
bufferedWriter.flush();//清空缓冲区,强制输出数据
bufferedWriter.close();//关闭输出流
之后就可以使用了
运行结果: