`
zjx2388
  • 浏览: 1304127 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

jfreechart实现仪表盘dashbord

    博客分类:
  • J2SE
 
阅读更多
package com.htcf.dashbord;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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;

/**
 * @description 构造数据,测试仪表盘图片生成
 */
public class DashBords {
	  
    public static void main(String[] args) {
    	DashBords test = new DashBords();
    	test.getDataSet();
    	test.temperatureData();
    }   
    
    
    public void getDataSet(){
    	//数据集合对象 
    	DefaultValueDataset dataset = new DefaultValueDataset();
    	//当前指针指向的位置,即:我们需要显示的数据
    	dataset = new DefaultValueDataset(20D);
    	//实例化DialPlot
    	DialPlot dialplot = new DialPlot();
    	//设置数据集合
    	dialplot.setDataset(dataset);
        //开始设置显示框架结构
        StandardDialFrame standarddialframe= new StandardDialFrame();
        standarddialframe.setBackgroundPaint(Color.black);//Color.lightGray
        standarddialframe.setForegroundPaint(Color.darkGray);//圆边的颜色
        dialplot.setDialFrame(standarddialframe);
         //结束设置显示框架结构           
        GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));
        DialBackground dialbackground = new DialBackground(gradientpaint);
        dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
        dialplot.setBackground(dialbackground);
        //设置显示在表盘中央位置的信息
        DialTextAnnotation dialtextannotation = new DialTextAnnotation("成本执行");
        dialtextannotation.setFont(new Font("Dialog", 17, 17));
        dialtextannotation.setRadius(0.6D);//字体距离圆心的距离
        dialplot.addLayer(dialtextannotation);
        DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
        dialplot.addLayer(dialvalueindicator);
        //根据表盘的直径大小(0.88),设置总刻度范围
        StandardDialScale standarddialscale = new StandardDialScale(0.0D,100.0D,-120.0D,-300.0D,10D,9);
        standarddialscale.setTickRadius(0.9D);
        standarddialscale.setTickLabelOffset(0.1D);//显示数字 距离圆边的距离
        standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
        //主意是 dialplot.addScale()不是dialplot.addLayer()
        dialplot.addScale(0, standarddialscale);
        //设置刻度范围(红色)
        StandardDialRange standarddialrange = new StandardDialRange(0D, 50D, Color.green);
        standarddialrange.setInnerRadius(0.6D);
        standarddialrange.setOuterRadius(0.62D);
        dialplot.addLayer(standarddialrange);
        //设置刻度范围(橘黄色)           
        StandardDialRange standarddialrange1 = new StandardDialRange(50D, 80D, Color.orange);
        standarddialrange1.setInnerRadius(0.6D);// 半径返回两条线  
        standarddialrange1.setOuterRadius(0.62D);
        dialplot.addLayer(standarddialrange1);
        //设置刻度范围(绿色)               
        StandardDialRange standarddialrange2 = new StandardDialRange(80D, 100D, Color.red);
        standarddialrange2.setInnerRadius(0.6D);
        standarddialrange2.setOuterRadius(0.62D);
        dialplot.addLayer(standarddialrange2);
        //设置指针
        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.1D);//指针中心圆的大小
        dialplot.setCap(dialcap);
        //生成chart对象
        JFreeChart jfreechart = new JFreeChart(dialplot);
        
        //设置标题
        jfreechart.setTitle("目标成本执行情况分析");
        
        DashBords testBords = new DashBords();
        testBords.saveAsFile(jfreechart, "d:\\jfreechart\\dashbord_"+Math.random()*20+".png", 400, 400);
    }
   
    
    public void temperatureData(){

    	//数据集合对象 
    	DefaultValueDataset dataset = new DefaultValueDataset();
    	//当前指针指向的位置,即:我们需要显示的数据
    	dataset = new DefaultValueDataset(16D);
    	//实例化DialPlot
    	DialPlot dialplot = new DialPlot();
    	//设置数据集合
    	dialplot.setDataset(dataset);
        //开始设置显示框架结构
        StandardDialFrame standarddialframe= new StandardDialFrame();
        standarddialframe.setBackgroundPaint(Color.lightGray);
        standarddialframe.setForegroundPaint(Color.darkGray);//圆边的颜色
        dialplot.setDialFrame(standarddialframe);
         //结束设置显示框架结构           
        GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));
        DialBackground dialbackground = new DialBackground(gradientpaint);
        dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
        dialplot.setBackground(dialbackground);
        //设置显示在表盘中央位置的信息
        DialTextAnnotation dialtextannotation = new DialTextAnnotation("温度");
        dialtextannotation.setFont(new Font("Dialog", 1, 14));
        dialtextannotation.setRadius(0.69999999999999996D);//字体距离圆心的距离
        dialplot.addLayer(dialtextannotation);
        DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
        dialplot.addLayer(dialvalueindicator);
        //根据表盘的直径大小(0.88),设置总刻度范围
        StandardDialScale standarddialscale = new StandardDialScale(-40D,60D,-120.0D,-300.0D,10D,9);//???
        standarddialscale.setTickRadius(0.88D);
        standarddialscale.setTickLabelOffset(0.14999999999999999D);//显示数字 距离圆边的距离
        standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
        //主意是 dialplot.addScale()不是dialplot.addLayer()
        dialplot.addScale(0, standarddialscale);
        //设置刻度范围(红色)
        StandardDialRange standarddialrange = new StandardDialRange(40D, 60D, Color.red);
        standarddialrange.setInnerRadius(0.52000000000000002D);
        standarddialrange.setOuterRadius(0.55000000000000004D);
        dialplot.addLayer(standarddialrange);
        //设置刻度范围(橘黄色)           
        StandardDialRange standarddialrange1 = new StandardDialRange(10D, 40D, Color.orange);
        standarddialrange1.setInnerRadius(0.52000000000000002D);// 半径返回两条线  
        standarddialrange1.setOuterRadius(0.55000000000000004D);
        dialplot.addLayer(standarddialrange1);
        //设置刻度范围(绿色)               
        StandardDialRange standarddialrange2 = new StandardDialRange(-40D, 10D, Color.green);
        standarddialrange2.setInnerRadius(0.52000000000000002D);
        standarddialrange2.setOuterRadius(0.55000000000000004D);
        dialplot.addLayer(standarddialrange2);
        //设置指针
        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.1D);//指针中心圆的大小
        dialplot.setCap(dialcap);
        //生成chart对象
        JFreeChart jfreechart = new JFreeChart(dialplot);
        
        //设置标题
        jfreechart.setTitle("设备取水温度采样");
        
        DashBords testBords = new DashBords();
        testBords.saveAsFile(jfreechart, "d:\\jfreechart\\dashbord_"+Math.random()*20+".png", 400, 400);
    
    }
    
    
    /**保存为文件*/   
    public void saveAsFile(JFreeChart chart, String outputPath, int width, int height) {   
        FileOutputStream out = null;   
        try {   
            File outFile = new File(outputPath);   
            if (!outFile.getParentFile().exists()) {   
                outFile.getParentFile().mkdirs();   
            }   
            out = new FileOutputStream(outputPath);   
            // 保存为PNG   
            ChartUtilities.writeChartAsPNG(out, chart, width, height);   
            // 保存为JPEG   
            // ChartUtilities.writeChartAsJPEG(out, chart, width, height);   
            out.flush();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            if (out != null) {   
                try {   
                    out.close();   
                } catch (IOException e) {   
                       
                }   
            }   
        }   
    }  
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics