利用JfreeChart制作仪表盘效果图表

在这里插入图片描述


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
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.RectangleEdge;

import btt.controls.scalechart.chartbean.StandartDiaRangSelf;

/**
 * 仪表盘制作
 * 
 * @author Administrator
 *
 */
public class DialDemo1 extends JFrame implements ChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** The first dataset. */
	DefaultValueDataset dataset1;
	DialTextAnnotation  annotation1; 

	@Override
	public void stateChanged(ChangeEvent e) {
		annotation1.setLabel(dataset1.getValue()+"%");
	}

	public DialDemo1(String title) {
		super(title);
		this.dataset1 = new DefaultValueDataset(99.0);
		DialPlot plot = new DialPlot();
		
		plot.setView(0.0, 0.0, 1.0, 1.0);
		plot.setDataset(0, this.dataset1);
		
		StandardDialFrame dialFrame = new StandardDialFrame();
		dialFrame.setBackgroundPaint(new Color(54, 54, 54));
		dialFrame.setForegroundPaint(new Color(54, 54, 54));
		plot.setDialFrame(dialFrame);
		
//		GradientPaint gp = new GradientPaint(new Point(), new Color(255, 0, 0), new Point(),
//				new Color(170, 170, 220));
//		DialBackground db = new DialBackground(gp);
//		db.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
//		plot.setBackground(db);
//		DialTextAnnotation annotation1 = new DialTextAnnotation("速度");
//		annotation1.setFont(new Font("Dialog", Font.BOLD, 14));
//		annotation1.setRadius(0.7);
//		plot.addLayer(annotation1);
		
    
//		DialValueIndicator dvi2 = new DialValueIndicator(1);
//		dvi2.setFont(new Font("Dialog", Font.PLAIN, 10));
//		dvi2.setOutlinePaint(Color.red);
//		dvi2.setRadius(0.60);
//		dvi2.setAngle(-77.0);
//		plot.addLayer(dvi2);
		
		
		
		StandartDiaRangSelf standarddialrange =new StandartDiaRangSelf(0D,30D, Color.red);
	    standarddialrange.setInnerRadius(0.83000000000000002D);
		standarddialrange.setOuterRadius(0.89000000000000004D);
		plot.addLayer(standarddialrange);
		
		StandartDiaRangSelf standarddialrange1 =new StandartDiaRangSelf(30D,70D, Color.orange);
		standarddialrange1.setInnerRadius(0.83000000000000002D);

		standarddialrange1.setOuterRadius(0.89000000000000004D);

		plot.addLayer(standarddialrange1);
		StandardDialRange standarddialrange2 =new StandartDiaRangSelf(70D,100D, Color.blue);
		
		standarddialrange2.setInnerRadius(0.83000000000000002D);
		standarddialrange2.setOuterRadius(0.89000000000000004D);
		
		plot.addLayer(standarddialrange2);
		
		StandardDialScale scale = new StandardDialScale();
		scale.setLowerBound(0); // 最底表盘刻度
		scale.setUpperBound(100); // 最高表盘刻度
		scale.setStartAngle(-120); // 弧度为120,刚好与人的正常视觉对齐
		scale.setExtent(-300); // 弧度为300,刚好与人的正常视觉对齐
		scale.setTickRadius(0.88);
		scale.setTickLabelOffset(0.15);
		scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
		scale.setTickLabelPaint(Color.WHITE);
		scale.setMajorTickPaint(Color.WHITE);
		scale.setMinorTickPaint(Color.WHITE);
		plot.addScale(0, scale);
		
		DialPointer.Pointer  needle = new DialPointer.Pointer();
		needle.setRadius(0.8);
		needle.setFillPaint(Color.blue);
		needle.setOutlinePaint(Color.blue);
		plot.addLayer(needle);


//		DialCap cap = new DialCap();
//		cap.setRadius(0.10);
//		plot.setCap(cap);
//		DialPointer needle2 = new DialPointer.Pin(1);
//		needle2.setRadius(0.55);
//		plot.addLayer(needle2);

		 annotation1 = new DialTextAnnotation("99%");
		plot.addLayer(annotation1);

		JFreeChart chart1 = new JFreeChart(plot);
		chart1.setBackgroundPaint(new Color(54, 54, 54));
		chart1.setTitle("仪表盘");
		chart1.getTitle().setPosition(RectangleEdge.BOTTOM);
		ChartPanel cp1 = new ChartPanel(chart1);
		cp1.setPreferredSize(new Dimension(400, 400));
		JPanel content = new JPanel(new BorderLayout());
		content.add(cp1);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(content);
	}

	public static void main(String[] args) {
		DialDemo1 app = new DialDemo1("仪表盘");
		app.pack();
		app.setVisible(true);
//	    new	Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				try {
//					Thread.sleep(5000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				app.annotation1.setLabel("12");
//				app.dataset1.setValue(34);
//				System.out.println("**********");
//			}
//		}).start();
	}

}


import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialScale;
import org.jfree.chart.plot.dial.StandardDialRange;

public class StandartDiaRangSelf extends StandardDialRange {
	
	public StandartDiaRangSelf(double lower, double upper, Paint paint) {
		super(lower, upper, paint);
	}
	private static final long serialVersionUID = 1L;

	public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {
		Rectangle2D arcRectInner = DialPlot.rectangleByRadius(frame, (getOuterRadius() -getInnerRadius())/2 + getInnerRadius(), (getOuterRadius() -getInnerRadius())/2 + getInnerRadius());


		DialScale scale = plot.getScale(getScaleIndex());
		if (scale == null) {
			throw new RuntimeException("No scale for scaleIndex = " + getScaleIndex());
		}

		double angleMin = scale.valueToAngle(getLowerBound());
		double angleMax = scale.valueToAngle(getUpperBound());


		Arc2D arcOuter = new Arc2D.Double(arcRectInner, angleMax, angleMin - angleMax, 0);
        double s = (getOuterRadius() -getInnerRadius()) * 180;
		g2.setPaint(getPaint());
		g2.setStroke(new BasicStroke((float)s));
		g2.draw(arcOuter);
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值