package org.lizhen;
import java.io.*;
import jofc2.model.*;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.YAxis;
import jofc2.model.elements.*;
public class LineDemo {
/**
* 使用 JOFC2 创建数据模型的一般流程为:
* 创建特定图表类型实例 -> 添加图表描述数据 -> 创建 OFC 图表对象 -> 序列化为 JSON 文件
* @param args
*/
public static void main (String[] args) {
System.out.println("*******************");
LineChart lineChart = new LineChart(LineChart.Style .NORMAL);//创建特定图表类型实例
lineChart.setColour("#669900");
lineChart.setText("line1");
lineChart.setFontSize(15);
lineChart.setTooltip("#val#%");
//添加图表描述数据 点的描述
/* lineChart.addValues(10);
lineChart.addValues(20);
lineChart.addValues(40);
lineChart.addValues(30);*/
LineChart.Dot dot1 = new LineChart.Dot(70,"0x6699CC");
LineChart.Dot dot2 = new LineChart.Dot(75,"0x6699CC");
LineChart.Dot dot3 = new LineChart.Dot(80,"0x6699CC");
LineChart.Dot dot4 = new LineChart.Dot(85,"0x6699CC");
LineChart.Dot dot5 = new LineChart.Dot(80,"0x6699CC");
LineChart.Dot dot6 = new LineChart.Dot(75,"0x6699CC");
LineChart.Dot dot7 = new LineChart.Dot(70,"0x6699CC");
LineChart.Dot dot8 = new LineChart.Dot(60,"0x6699CC");
lineChart.addDots(dot1);
lineChart.addDots(dot2);
lineChart.addDots(dot3);
lineChart.addDots(dot4);
lineChart.addDots(dot5);
lineChart.addDots(dot6);
lineChart.addDots(dot7);
lineChart.addDots(dot8);
//添加图表描述数据 X、Y坐标
XAxis xAxis = new XAxis();
xAxis.addLabels("1月份");
xAxis.addLabels("2月份");
xAxis.addLabels("3月份");
xAxis.addLabels("4月份");
xAxis.addLabels("5月份");
xAxis.addLabels("6月份");
xAxis.addLabels("7月份");
xAxis.addLabels("8月份");
xAxis.setColour("#9933CC");
long max =100;
YAxis yAxis = new YAxis();
yAxis.setMax(max+0.0);
yAxis.setSteps(10);
//创建 OFC 图表对象
Chart flashChart = new Chart("lineChart_demo","font-size:18px;color:#ff0000;");
flashChart.setXAxis(xAxis);
flashChart.setYAxis(yAxis);
Text yText = new Text("***lineChart***",Text.createStyle(20, "#736AFF", Text.TEXT_ALIGN_CENTER));
flashChart.setYLegend(yText);
flashChart.addElements(lineChart);
//序列化为 JSON 文件
String jsonString = flashChart.toString();
String fileName = System.currentTimeMillis()+".json";
System.out.println(fileName);
try {
FileOutputStream fos = new FileOutputStream(new File("D:/data/"+fileName));
try {
fos.write(jsonString.getBytes("UTF-8"));
fos.close();
System.out.println("OK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
用ofc2写折线的JSO出现了一个问题,就是无法在HTML上显示出来,后来看到文档上说LineChart.Style 有三种枚举类型,选NORMAL那个就可以显示出来。。。