package edu.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
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.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.GradientPaintTransformer;
import org.jfree.ui.StandardGradientPaintTransformer;
public class Dialplot extends JFrame implements Runnable{
private DefaultValueDataset dataset;
public void setdataset(double t)
{
this.dataset.setValue(t);
}
public void setDialplot ()
{
dataset = new DefaultValueDataset();
dataset = new DefaultValueDataset(20D);
DialPlot dialplot = new DialPlot();
dialplot.setDataset(dataset);
//开始设置显示框架结构
StandardDialFrame standarddialframe= new StandardDialFrame();
standarddialframe.setBackgroundPaint(Color.black);
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((GradientPaintTransformer) 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("数据包传输速率");
ChartPanel chartPanel = new ChartPanel(jfreechart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 500));
this.getContentPane().add(chartPanel);
}
public static void main(String args[])
{
Dialplot a=new Dialplot();
a.setTitle("jfreechart tset");
a.setSize(600,500);
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setDialplot();
a.setdataset(11.33);
new Thread(a).start();
a.setVisible(true);
}
@Override
public void run() {
// TODO Auto-generated method stub
double k;
while(true)
{
k=1;
for(double i=0.01;k<100;i+=0.01)
{
k=k+i;
this.setdataset(k);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}