最近由于项目需要,需要在网页中生成甘特图,在网上浏览资料之后,***通过JFreeChart实现了仪表盘。
代码如下:
- DefaultValueDataset data = new DefaultValueDataset(32.0);
- MeterPlot plot = new MeterPlot(data);
- plot.setDialShape(DialShape.CHORD);
- plot.setDialBackgroundPaint(Color.WHITE);
- plot.setRange(new Range(0, 120));
- plot.setDialOutlinePaint(Color.GRAY);
- plot.setNeedlePaint(Color.BLACK);
- plot.setTickLabelsVisible(true);
- plot.setTickLabelPaint(Color.BLACK);
- plot.setTickPaint(Color.GRAY);
- plot.setTickLabelFormat(NumberFormat.getNumberInstance());
- plot.setTickSize(10);
- plot.setValuePaint(Color.BLACK);
- plot.addInterval(new MeterInterval("Low", new Range(0, 70), null, null,new Color(128, 255, 128,90) ));
- plot.addInterval(new MeterInterval("Normal", new Range(70, 100), null, null, new Color(255, 255, 128,90)));
- plot.addInterval(new MeterInterval("High", new Range(100, 120), null, null, new Color(255, 128, 128,90)));
- //创建chart,***一个参数决定是否显示图例
- final JFreeChart chart = new JFreeChart("Meter Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
- //放到SWT的Composite里,以前介绍过这个方法
- Composite drawarea = new Composite(tabFolder, SWT.EMBEDDED);
- drawarea.setLayout(new FillLayout());
- Frame canvasFrame = SWT_AWT.new_Frame(drawarea);
- java.awt.Canvas canvas = new java.awt.Canvas() {
- public void paint(Graphics g) {
- super.paint(g);
- if (chart != null)
- chart.draw((Graphics2D) g, getBounds());
- }
- };
- TabItem tab = new TabItem(tabFolder, SWT.NONE);
- tab.setControl(drawarea);
- tab.setText("Meter");
- canvasFrame.add(canvas);
效果图:
原文链接:http://www.cnblogs.com/bjzhanghao/archive/2007/03/13/673303.html
【编辑推荐】