eagletony 发表于 2013-1-23 01:46:18

Jfreechart+ajax 实现动态仪表图 java代码

package demo;import java.awt.Color;import java.awt.Font;import java.awt.GradientPaint;import java.awt.Point;import java.io.FileOutputStream;import java.io.IOException;import java.net.URISyntaxException;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.plot.dial.DialBackground;import org.jfree.chart.plot.dial.DialCap;import org.jfree.chart.plot.dial.DialPlot;import org.jfree.chart.plot.dial.DialTextAnnotation;import org.jfree.chart.plot.dial.DialValueIndicator;import org.jfree.chart.plot.dial.StandardDialFrame;import org.jfree.chart.plot.dial.StandardDialRange;import org.jfree.chart.plot.dial.StandardDialScale;import org.jfree.data.general.DefaultValueDataset;import org.jfree.ui.GradientPaintTransformType;import org.jfree.ui.StandardGradientPaintTransformer;public class Dial {public static String getDial(String warnName,String warnValueStr) {double warnValue;try{double tempDouble = Double.valueOf(warnValueStr);warnValue =tempDouble;}catch(Exception ex){ex.printStackTrace();warnValue=0;}DefaultValueDataset dataset = new DefaultValueDataset();dataset = new DefaultValueDataset(warnValue);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);simpledialframe.setForegroundPaint(Color.darkGray);dialplot.setDialFrame(simpledialframe);GradientPaint gradientpaint = new GradientPaint(new Point(),Color.lightGray, new Point(),Color.GRAY);DialBackground dialbackground = new DialBackground(gradientpaint);dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));dialplot.setBackground(dialbackground);dialplot.setBackgroundPaint(Color.CYAN);DialTextAnnotation dialtextannotation = new DialTextAnnotation(warnName);dialtextannotation.setFont(new Font("宋体", 1, 16));dialtextannotation.setPaint(Color.black);dialtextannotation.setRadius(0.69999999999999996D);dialplot.addLayer(dialtextannotation);if(warnValue==0){StandardDialRange standarddialrange2 = new StandardDialRange(0D, 100D,Color.cyan);standarddialrange2.setInnerRadius(0.52000000000000002D);standarddialrange2.setOuterRadius(1);dialplot.addLayer(standarddialrange2);}else if(warnValue>0&&warnValue<=100){StandardDialRange standarddialrange1 = new StandardDialRange(0D, 100D,Color.yellow);standarddialrange1.setInnerRadius(0.52000000000000002D);standarddialrange1.setOuterRadius(1);dialplot.addLayer(standarddialrange1);}else if(warnValue>100&&warnValue<=1000){StandardDialRange standarddialrange = new StandardDialRange(0D, 1000D,new Color(251,57,36));standarddialrange.setInnerRadius(0.52000000000000002D);standarddialrange.setOuterRadius(1);dialplot.addLayer(standarddialrange);}else if(warnValue>1000){StandardDialRange standarddialrange = new StandardDialRange(0D, 10000D,Color.red);standarddialrange.setInnerRadius(0.52000000000000002D);standarddialrange.setOuterRadius(1);dialplot.addLayer(standarddialrange);}DialValueIndicator dialvalueindicator = new DialValueIndicator(0);dialvalueindicator.setFont(new Font("宋体", 1, 14));dialvalueindicator.setPaint(Color.cyan);dialvalueindicator.setOutlinePaint(Color.lightGray);dialvalueindicator.setBackgroundPaint(Color.GRAY);dialvalueindicator.setRadius(0.39999999999999998D);//dialvalueindicator.setPaint(Color.red);dialplot.addLayer(dialvalueindicator);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();pointer.setFillPaint(Color.red);pointer.setOutlinePaint(Color.red);pointer.setRadius(0.75D);dialplot.addLayer(pointer);// 实例化DialCapDialCap 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";//动态文件名 相对String webPath="WarnImages/meter.jpeg";try {filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();filePath =filePath.replace("WEB-INF/classes/", "");filePath += webPath;System.out.println(filePath);} catch (URISyntaxException e1) {// TODO Auto-generated catch blocke1.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("测试","366"));System.out.println(String.valueOf(randomNum()));System.out.println(new Dial().getDial("测试",String.valueOf(randomNum())));}private static intrandomNum()   {            System.out.println((int)(Math.random()*60+300));               return (int)(Math.random()*60+80);   } }package demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sinoufc.nms.mon.rmi.RMIClient;public class DialServlet extends HttpServlet {/** * Constructor of the object. */public DialServlet() {super();} public void service(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {    int memoryUsedRate = 0;      try      {      //memoryUsedRate = RMIClient.getRMIInterface().getInFlow("10.10..9.188",1);      //memoryUsedRate = RMIClient.getRMIInterface().getInFlow("111", 1);      System.out.println("memoryUsedRate="+memoryUsedRate);      }      catch(Exception ex)      {      ex.printStackTrace();      }       // System.out.println(new Dial().getDial("测试",String.valueOf(memoryUsedRate)));      System.out.println(Dial.getDial("测试",String.valueOf(randomNum()))); }/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}private static intrandomNum()   {            System.out.println((int)(Math.random()*60+300));               return (int)(Math.random()*60+80);   }/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code here}}
页: [1]
查看完整版本: Jfreechart+ajax 实现动态仪表图 java代码