jfreechart實現儀表盤dashbord

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) {   
                       
                }   
            }   
        }   
    }  
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章