1 40 41 package org.jfree.chart.demo; 42 43 import java.awt.Color ; 44 import java.awt.Font ; 45 import java.awt.GradientPaint ; 46 import java.awt.Insets ; 47 import java.awt.event.WindowAdapter ; 48 import java.awt.event.WindowEvent ; 49 50 import javax.swing.JFrame ; 51 52 import org.jfree.chart.ChartFrame; 53 import org.jfree.chart.JFreeChart; 54 import org.jfree.chart.MeterLegend; 55 import org.jfree.chart.plot.DialShape; 56 import org.jfree.chart.plot.MeterPlot; 57 import org.jfree.data.DefaultValueDataset; 58 import org.jfree.data.Range; 59 import org.jfree.ui.RefineryUtilities; 60 61 66 public class MeterChartDemo { 67 68 74 void displayMeterChart(double value, DialShape shape) { 75 76 DefaultValueDataset data = new DefaultValueDataset(75.0); 77 MeterPlot plot = new MeterPlot(data); 78 plot.setUnits("Degrees"); 79 plot.setRange(new Range(20.0, 140.0)); 80 plot.setNormalRange(new Range(70.0, 100.0)); 81 plot.setWarningRange(new Range(100.0, 120.0)); 82 plot.setCriticalRange(new Range(120.0, 140.0)); 83 84 plot.setDialShape(shape); 85 plot.setNeedlePaint(Color.white); 86 plot.setTickLabelFont(new Font ("SansSerif", Font.BOLD, 9)); 87 88 plot.setInsets(new Insets (5, 5, 5, 5)); 89 JFreeChart chart = new JFreeChart( 90 "Meter Chart", 91 JFreeChart.DEFAULT_TITLE_FONT, 92 plot, 93 false 94 ); 95 96 MeterLegend legend = new MeterLegend(chart, "Sample Meter"); 97 chart.setLegend(legend); 98 99 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 100 101 JFrame chartFrame = new ChartFrame("Meter Chart", chart); 102 chartFrame.addWindowListener(new WindowAdapter () { 103 107 public void windowClosing(WindowEvent e) 108 { 109 System.exit(0); 110 } 111 }); 112 chartFrame.pack(); 113 RefineryUtilities.positionFrameRandomly(chartFrame); 114 chartFrame.setSize(250, 250); 115 chartFrame.show(); 116 117 } 118 119 130 135 public static void main(String [] args) { 136 137 if (args.length == 0) { 138 System.err.println("Usage: java TestMeter <type> <value>"); 139 System.err.println("Type: 0 = PIE"); 140 System.err.println("Type: 1 = CIRCLE"); 141 System.err.println("Type: 2 = CHORD"); 142 } 143 MeterChartDemo h = new MeterChartDemo(); 144 double val = 85; 145 DialShape dialShape = DialShape.CIRCLE; 146 if (args.length > 0) { 147 int type = Integer.parseInt(args[0]); 148 if (type == 0) { 149 dialShape = DialShape.PIE; 150 } 151 else if (type == 1) { 152 dialShape = DialShape.CIRCLE; 153 } 154 else if (type == 0) { 155 dialShape = DialShape.CHORD; 156 } 157 } 158 if (args.length > 1) { 159 val = new Double (args[1]).doubleValue(); 160 } 161 h.displayMeterChart(val, dialShape); 162 163 } 164 165 } 166 | Popular Tags |