public String getDial(String warnName,String warnValueStr) {
double warnValue=1.0;
try{
double tempDouble = Double.valueOf(warnValueStr);
warnValue =tempDouble;
}catch(Exception ex){
ex.printStackTrace();
warnValue=0;
}
// 数据集合对象 此处为DefaultValueDataset
DefaultValueDataset dataset = new DefaultValueDataset();
// 当前指针指向的位置,即:我们需要显示的数据
dataset = new DefaultValueDataset(warnValue);
// 实例化DialPlot
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0D, 0.0D, 1.0D, 1.0D);
// 设置数据集合
dialplot.setDataset(dataset);
// 开始设置显示框架结构
StandardDialFrame simpledialframe = new StandardDialFrame();
simpledialframe.setBackgroundPaint(Color.lightGray);//Color.lightGray //仪表盘边框内部颜色
simpledialframe.setForegroundPaint(Color.darkGray);//Color.darkGray //仪表盘边框外部颜色
dialplot.setDialFrame(simpledialframe);
// 结束设置显示框架结构,表盘颜色 从最上部 白色 过渡到最下部的蓝色
GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(229,229,229), new Point(), new Color(229,229,229));
DialBackground dialbackground = new DialBackground(gradientpaint);
dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
// 设置显示在表盘中央位置的信息
DialTextAnnotation dialtextannotation = new DialTextAnnotation(warnName);
dialtextannotation.setFont(new Font("宋体", 1, 16));
dialtextannotation.setRadius(0.69999999999999996D);
dialplot.addLayer(dialtextannotation);
// 设置刻度范围(绿色)
if(warnValue==0){
StandardDialRange standarddialrange2 = new StandardDialRange(0D, 100D,Color.green);
standarddialrange2.setInnerRadius(0.52000000000000002D);
standarddialrange2.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange2);
}else if(warnValue>0&&warnValue<=100){// 设置刻度范围(橘黄色)
StandardDialRange standarddialrange1 = new StandardDialRange(0D, 100D,Color.orange);
standarddialrange1.setInnerRadius(0.52000000000000002D);
standarddialrange1.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange1);
}else if(warnValue>100&&warnValue<=1000){// 设置刻度范围(红色)
StandardDialRange standarddialrange = new StandardDialRange(0D, 1000D,Color.red);
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
}else if(warnValue>1000){// 设置刻度范围(红色)
StandardDialRange standarddialrange = new StandardDialRange(0D, 10000D,Color.red);
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
}
//指针指示框 ,仪表盘中间的小方框
DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
dialvalueindicator.setFont(new Font("宋体", 1, 14));
dialvalueindicator.setOutlinePaint(new Color(229,229,229));
dialvalueindicator.setBackgroundPaint(new Color(229,229,229));
dialvalueindicator.setRadius(0.39999999999999998D);
//dialvalueindicator.setPaint(Color.red);
dialplot.addLayer(dialvalueindicator);
/*
* StandardDialScale 方法
* 参数1 开始数值 类似手表 开始 0点
* 参数2 结束数值 类似手表 结束 12点
* 参数5 间隔差值 类似手表 间隔差值5分钟
* 参数6 间隔数量 类似手表 1点到2点 有4个间隔点
*/
//如果 预警条数少于 100条,开度从 0 至 100 ,间隔 10
double startPosition=0D; //开度 0
double endPosition=100D; //开度 100
double skipValue=10D; //间隔 10
if(warnValue>100&&warnValue<1000){
endPosition=1000D;
skipValue=100D;
}else if(warnValue>=1000){
endPosition=10000D;
skipValue=1000D;
}
//刻度盘设置
StandardDialScale standarddialscale = new StandardDialScale(startPosition, endPosition,-120D, -300D, skipValue, 4);
standarddialscale.setTickRadius(0.88D);//设置半径
standarddialscale.setTickLabelOffset(0.14999999999999999D);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 10));//刻度盘数字大小
// 注意是 dialplot.addScale()不是dialplot.addLayer()
dialplot.addScale(0, standarddialscale);
// 设置指针
org.jfree.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.chart.plot.dial.DialPointer.Pointer();
dialplot.addLayer(pointer);
// 实例化DialCap
DialCap dialcap = new DialCap();
dialcap.setRadius(0.10000000000000001D);
dialplot.setCap(dialcap);
// 生成chart对象
JFreeChart jfreechart = new JFreeChart(dialplot);
// 3、设定参数输出结果,首先在 项目/WEB-INF/classes/,添加WarnImages文件夹
String filePath="";//绝对路径
String webPath="WarnImages/"+System.currentTimeMillis()+".jpeg";//动态文件名 相对
try {
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
filePath += webPath;
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileOutputStream file = null;
try {
file = new FileOutputStream(filePath);
ChartUtilities.writeChartAsJPEG(file, 1.0f, jfreechart, 200, 200,null);//200,200 图片大小
} catch (IOException e) {
e.printStackTrace();
} // 生成图片
finally {
try {
file.close();// 最后关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
}
return webPath;
}
public static void main(String[] args) {
//System.out.println(new Dial().getDial("测试预警","120"));
}
double warnValue=1.0;
try{
double tempDouble = Double.valueOf(warnValueStr);
warnValue =tempDouble;
}catch(Exception ex){
ex.printStackTrace();
warnValue=0;
}
// 数据集合对象 此处为DefaultValueDataset
DefaultValueDataset dataset = new DefaultValueDataset();
// 当前指针指向的位置,即:我们需要显示的数据
dataset = new DefaultValueDataset(warnValue);
// 实例化DialPlot
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0D, 0.0D, 1.0D, 1.0D);
// 设置数据集合
dialplot.setDataset(dataset);
// 开始设置显示框架结构
StandardDialFrame simpledialframe = new StandardDialFrame();
simpledialframe.setBackgroundPaint(Color.lightGray);//Color.lightGray //仪表盘边框内部颜色
simpledialframe.setForegroundPaint(Color.darkGray);//Color.darkGray //仪表盘边框外部颜色
dialplot.setDialFrame(simpledialframe);
// 结束设置显示框架结构,表盘颜色 从最上部 白色 过渡到最下部的蓝色
GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(229,229,229), new Point(), new Color(229,229,229));
DialBackground dialbackground = new DialBackground(gradientpaint);
dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
// 设置显示在表盘中央位置的信息
DialTextAnnotation dialtextannotation = new DialTextAnnotation(warnName);
dialtextannotation.setFont(new Font("宋体", 1, 16));
dialtextannotation.setRadius(0.69999999999999996D);
dialplot.addLayer(dialtextannotation);
// 设置刻度范围(绿色)
if(warnValue==0){
StandardDialRange standarddialrange2 = new StandardDialRange(0D, 100D,Color.green);
standarddialrange2.setInnerRadius(0.52000000000000002D);
standarddialrange2.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange2);
}else if(warnValue>0&&warnValue<=100){// 设置刻度范围(橘黄色)
StandardDialRange standarddialrange1 = new StandardDialRange(0D, 100D,Color.orange);
standarddialrange1.setInnerRadius(0.52000000000000002D);
standarddialrange1.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange1);
}else if(warnValue>100&&warnValue<=1000){// 设置刻度范围(红色)
StandardDialRange standarddialrange = new StandardDialRange(0D, 1000D,Color.red);
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
}else if(warnValue>1000){// 设置刻度范围(红色)
StandardDialRange standarddialrange = new StandardDialRange(0D, 10000D,Color.red);
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
}
//指针指示框 ,仪表盘中间的小方框
DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
dialvalueindicator.setFont(new Font("宋体", 1, 14));
dialvalueindicator.setOutlinePaint(new Color(229,229,229));
dialvalueindicator.setBackgroundPaint(new Color(229,229,229));
dialvalueindicator.setRadius(0.39999999999999998D);
//dialvalueindicator.setPaint(Color.red);
dialplot.addLayer(dialvalueindicator);
/*
* StandardDialScale 方法
* 参数1 开始数值 类似手表 开始 0点
* 参数2 结束数值 类似手表 结束 12点
* 参数5 间隔差值 类似手表 间隔差值5分钟
* 参数6 间隔数量 类似手表 1点到2点 有4个间隔点
*/
//如果 预警条数少于 100条,开度从 0 至 100 ,间隔 10
double startPosition=0D; //开度 0
double endPosition=100D; //开度 100
double skipValue=10D; //间隔 10
if(warnValue>100&&warnValue<1000){
endPosition=1000D;
skipValue=100D;
}else if(warnValue>=1000){
endPosition=10000D;
skipValue=1000D;
}
//刻度盘设置
StandardDialScale standarddialscale = new StandardDialScale(startPosition, endPosition,-120D, -300D, skipValue, 4);
standarddialscale.setTickRadius(0.88D);//设置半径
standarddialscale.setTickLabelOffset(0.14999999999999999D);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 10));//刻度盘数字大小
// 注意是 dialplot.addScale()不是dialplot.addLayer()
dialplot.addScale(0, standarddialscale);
// 设置指针
org.jfree.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.chart.plot.dial.DialPointer.Pointer();
dialplot.addLayer(pointer);
// 实例化DialCap
DialCap dialcap = new DialCap();
dialcap.setRadius(0.10000000000000001D);
dialplot.setCap(dialcap);
// 生成chart对象
JFreeChart jfreechart = new JFreeChart(dialplot);
// 3、设定参数输出结果,首先在 项目/WEB-INF/classes/,添加WarnImages文件夹
String filePath="";//绝对路径
String webPath="WarnImages/"+System.currentTimeMillis()+".jpeg";//动态文件名 相对
try {
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
filePath += webPath;
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileOutputStream file = null;
try {
file = new FileOutputStream(filePath);
ChartUtilities.writeChartAsJPEG(file, 1.0f, jfreechart, 200, 200,null);//200,200 图片大小
} catch (IOException e) {
e.printStackTrace();
} // 生成图片
finally {
try {
file.close();// 最后关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
}
return webPath;
}
public static void main(String[] args) {
//System.out.println(new Dial().getDial("测试预警","120"));
}