Jfreechart时钟表DialPlot图形例子
在做仪表盘的时候,想着DialPlot本身就是个钟表图,可以当仪表盘用,也能生成钟表,所以就试了下,结果还可以.它和仪表盘一样分三步:1.导包 2.创建一个web项目 3.在web.xml中配置
web.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
</web-app>
下面是在JSP页面实现代码,如下例子所示:
<%@ page contentType="text/html;charset=UTF-8"%
<%@ page import="org.jfree.chart.JFreeChart,
org.jfree.chart.servlet.ServletUtilities,
org.jfree.data.general.DefaultValueDataset,
org.jfree.experimental.chart.plot.dial.*,
java.awt.*,java.io.*,org.jfree.chart.ChartUtilities,
org.jfree.ui.StandardGradientPaintTransformer,
org.jfree.experimental.chart.plot.dial.DialPointer.*,
org.jfree.ui.GradientPaintTransformType"
%>
<%
//创建时钟和分钟的数据集
DefaultValueDataset hoursDataset = new DefaultValueDataset(8D);
DefaultValueDataset minitesDs = new DefaultValueDataset(35D);
//设置钟表图形属性
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0D, 0.0D, 1.0D, 1.0D);
dialplot.setDataset(0, hoursDataset);//设置数据源
dialplot.setDataset(1, minitesDs);//设置分钟数据源
//设置简单帧(钟表框颜色)
SimpleDialFrame simpledialframe = new SimpleDialFrame();
//设置帧背景色
simpledialframe.setBackgroundPaint(Color.lightGray);
//设置帧前景色
simpledialframe.setForegroundPaint(Color.darkGray);
dialplot.setDialFrame(simpledialframe);
//设置钟表图形背景
DialBackground dialbackground = new DialBackground(Color.orange);
dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(
GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
//设置钟表值范围,从0到12
StandardDialScale standarddialscale = new StandardDialScale(0.0D,12D, 90D, -360D);
standarddialscale.setFirstTickLabelVisible(false);
standarddialscale.setMajorTickIncrement(1);
//设置表刻度和数字离中心的半径
standarddialscale.setTickRadius(0.90D);
//设置数字离开表盘的距离
standarddialscale.setTickLabelOffset(0.1D);
standarddialscale.setTickLabelFont(new Font("宋体", 0, 14));
dialplot.addScale(0, standarddialscale);
StandardDialScale standarddialscale1 = new StandardDialScale(0.0D,60D, 90D, -360D);
standarddialscale1.setVisible(false);
standarddialscale1.setMajorTickIncrement(5D);
standarddialscale1.setTickRadius(0.68D);
standarddialscale1.setTickLabelOffset(0.15D);
standarddialscale1.setTickLabelFont(new Font("宋体", 0, 14));
dialplot.addScale(1, standarddialscale1);
//设置时针的指针
Pointerpointer = newPointer(0);
pointer.setRadius(0.5D); //设置时针长度
dialplot.addLayer(pointer);
dialplot.mapDatasetToScale(1, 1)
//设置分针的指针
Pointerpointer1 = newPointer(1);
pointer1.setRadius(0.7D);//设置分钟针长度
dialplot.addLayer(pointer1);
//设置刻度中间的圆圈的大小
DialCap dialcap = new DialCap();
dialcap.setRadius(0.05D);//设置圆圈长度
dialplot.setCap(dialcap);
//根据刻度图形建立JFreeChart对象
JFreeChart jfreechart = new JFreeChart(dialplot);
//设置图形的标题
jfreechart.setTitle("钟表图示例");
//保存图形图片
String filename =ServletUtilities.saveChartAsPNG(jfreechart, 300,300, null, session);
//得到图片地址
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
/**
//显示方法二 这种方法用输出流输出,不用调用后面的img两种方法都可实现图片输出
OutputStream outs =response.getOutputStream();
response.setContentType("image/png");
response.reset();
ChartUtilities.writeChartAsPNG(outs,jfreechart, 400, 300);
outs.flush();
outs.close();
*/
%>
<!-- 方法一调用<img>方法二不用它,使用方法二的时候去掉-->
<img src="<%=graphURL%>" width="300"height="300" border="0" usemap="#<%= filename %>">
运行后效果如下: