JFreeChart是一个报表框架,可以实现各种各样的图表,例如:饼形图,柱形图,折线图,区域图,时序图,组合图,仪表盘等等
在这里我只实现几种不同的仪表盘
1,时钟
import java.awt.*;
import javax.swing.*;
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.*;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;
/**
* 用JfreeChart实现时钟
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class DialDemo4 extends JFrame {
static class DemoPanel extends JPanel implements ChangeListener{
public void stateChanged(ChangeEvent changeevent){
hoursDataset.setValue(new Integer(slider1.getValue()));
dataset2.setValue(new Integer(slider2.getValue()));
}
DefaultValueDataset hoursDataset;
DefaultValueDataset dataset2;
JSlider slider1;
JSlider slider2;
public DemoPanel(){
super(new BorderLayout());
hoursDataset = new DefaultValueDataset(6D);
dataset2 = new DefaultValueDataset(15D);
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0D, 0.0D, 1.0D, 1.0D);
dialplot.setDataset(0, hoursDataset);
dialplot.setDataset(1, dataset2);
StandardDialFrame standarddialframe = new StandardDialFrame();
standarddialframe.setBackgroundPaint(Color.lightGray);
standarddialframe.setForegroundPaint(Color.darkGray);
dialplot.setDialFrame(standarddialframe);
DialBackground dialbackground = new DialBackground(Color.white);
dialbackground.setGradientPaintTransformer(
new StandardGradientPaintTransformer(
GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
StandardDialScale standarddialscale =
new StandardDialScale(0.0D, 12D, 90D, -360D, 10D, 4);
standarddialscale.setFirstTickLabelVisible(false); //刻度盘刻度为隐藏
standarddialscale.setMajorTickIncrement(1.0D);
standarddialscale.setTickRadius(0.88D);
standarddialscale.setTickLabelOffset(0.14999999999999999D);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
dialplot.addScale(0, standarddialscale);
StandardDialScale standarddialscale1 =
new StandardDialScale(0.0D, 60D, 90D, -360D, 10D, 4);
standarddialscale1.setVisible(false);
standarddialscale1.setMajorTickIncrement(5D);
standarddialscale1.setTickRadius(0.68000000000000005D);
standarddialscale1.setTickLabelOffset(0.14999999999999999D);
standarddialscale1.setTickLabelFont(new Font("Dialog", 0, 14));
dialplot.addScale(1, standarddialscale1);
org.jfree.chart.plot.dial.DialPointer.Pointer pointer =
new org.jfree.chart.plot.dial.DialPointer.Pointer(0);
pointer.setRadius(0.55000000000000004D);
dialplot.addLayer(pointer);
dialplot.mapDatasetToScale(1,