001: /* --------------
002: * DialDemo1.java
003: * --------------
004: * (C) Copyright 2006, by Object Refinery Limited.
005: */
006:
007: package org.jfree.experimental.chart.demo;
008:
009: import java.awt.BorderLayout;
010: import java.awt.Color;
011: import java.awt.Dimension;
012: import java.awt.Font;
013: import java.awt.GradientPaint;
014: import java.awt.Point;
015: import javax.swing.JFrame;
016: import javax.swing.JPanel;
017: import javax.swing.JSlider;
018: import javax.swing.event.ChangeEvent;
019: import javax.swing.event.ChangeListener;
020: import org.jfree.chart.ChartPanel;
021: import org.jfree.chart.JFreeChart;
022: import org.jfree.experimental.chart.plot.dial.DialPlot;
023: import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
024: import org.jfree.experimental.chart.plot.dial.DialTextAnnotation;
025: import org.jfree.experimental.chart.plot.dial.StandardDialRange;
026: import org.jfree.experimental.chart.plot.dial.StandardDialScale;
027: import org.jfree.experimental.chart.plot.dial.DialBackground;
028: import org.jfree.experimental.chart.plot.dial.DialCap;
029: import org.jfree.experimental.chart.plot.dial.DialPointer;
030: import org.jfree.experimental.chart.plot.dial.DialValueIndicator;
031: import org.jfree.data.general.DefaultValueDataset;
032: import org.jfree.ui.GradientPaintTransformType;
033: import org.jfree.ui.StandardGradientPaintTransformer;
034:
035: /**
036: * A sample application showing the use of a {@link DialPlot}.
037: */
038: public class DialDemo1 extends JFrame implements ChangeListener {
039:
040: /** A slider to update the dataset value. */
041: JSlider slider;
042:
043: /** The dataset. */
044: DefaultValueDataset dataset;
045:
046: /**
047: * Creates a new instance.
048: *
049: * @param title the frame title.
050: */
051: public DialDemo1(String title) {
052: super (title);
053:
054: this .dataset = new DefaultValueDataset(10.0);
055:
056: // get data for diagrams
057: DialPlot plot = new DialPlot();
058: plot.setView(0.0, 0.0, 1.0, 1.0);
059: plot.setDataset(this .dataset);
060: SimpleDialFrame dialFrame = new SimpleDialFrame();
061: dialFrame.setBackgroundPaint(Color.lightGray);
062: dialFrame.setForegroundPaint(Color.darkGray);
063: plot.setDialFrame(dialFrame);
064:
065: GradientPaint gp = new GradientPaint(new Point(), new Color(
066: 255, 255, 255), new Point(), new Color(170, 170, 220));
067: DialBackground db = new DialBackground(gp);
068: db
069: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
070: GradientPaintTransformType.VERTICAL));
071: plot.setBackground(db);
072:
073: DialTextAnnotation annotation1 = new DialTextAnnotation(
074: "Temperature");
075: annotation1.setFont(new Font("Dialog", Font.BOLD, 14));
076: annotation1.setRadius(0.7);
077:
078: plot.addLayer(annotation1);
079:
080: DialValueIndicator dvi = new DialValueIndicator(0, "c");
081: plot.addLayer(dvi);
082:
083: StandardDialScale scale = new StandardDialScale(-40, 60, -120,
084: -300);
085: scale.setTickRadius(0.88);
086: scale.setTickLabelOffset(0.15);
087: scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
088: plot.addScale(0, scale);
089:
090: StandardDialRange range = new StandardDialRange(40.0, 60.0,
091: Color.red);
092: range.setInnerRadius(0.52);
093: range.setOuterRadius(0.55);
094: plot.addLayer(range);
095:
096: StandardDialRange range2 = new StandardDialRange(10.0, 40.0,
097: Color.orange);
098: range2.setInnerRadius(0.52);
099: range2.setOuterRadius(0.55);
100: plot.addLayer(range2);
101:
102: StandardDialRange range3 = new StandardDialRange(-40.0, 10.0,
103: Color.green);
104: range3.setInnerRadius(0.52);
105: range3.setOuterRadius(0.55);
106: plot.addLayer(range3);
107:
108: DialPointer needle = new DialPointer.Pointer();
109: plot.addLayer(needle);
110:
111: DialCap cap = new DialCap();
112: cap.setRadius(0.10);
113: plot.setCap(cap);
114:
115: JFreeChart chart1 = new JFreeChart(plot);
116: chart1.setTitle("Demo Dial 1");
117: ChartPanel cp1 = new ChartPanel(chart1);
118: cp1.setPreferredSize(new Dimension(400, 400));
119: this .slider = new JSlider(-40, 60);
120: this .slider.setMajorTickSpacing(10);
121: this .slider.setPaintLabels(true);
122: this .slider.addChangeListener(this );
123: JPanel content = new JPanel(new BorderLayout());
124: content.add(cp1);
125: content.add(this .slider, BorderLayout.SOUTH);
126: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
127: setContentPane(content);
128: }
129:
130: /**
131: * Handle a change in the slider by updating the dataset value. This
132: * automatically triggers a chart repaint.
133: *
134: * @param e the event.
135: */
136: public void stateChanged(ChangeEvent e) {
137: this .dataset.setValue(new Integer(this .slider.getValue()));
138: }
139:
140: /**
141: * Starting point for the demo application.
142: *
143: * @param args ignored.
144: */
145: public static void main(String[] args) {
146: DialDemo1 app = new DialDemo1("JFreeChart - Demo Dial 1");
147: app.pack();
148: app.setVisible(true);
149: }
150:
151: }
|