一般情况下,JFreeChart只能是基于applet的app,不过,这对客户端要求过高,那么有人就会想到,能转换成jpg或者png等图片格式吗?当然,这是最关键的就是ChartUtilities这个类,它就像一个JFreeChart想摆脱局限的桥梁,有了它,那就一切皆有可能(当然也不是万能的,就像钱一样)。
为此,我就对于我的上篇文章,做进一步的扩展。先看效果 
再看源代码
/**//*
*author:hujinpu
*/
packagecom.hujinpu.test;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.HashMap;
importjava.util.Map;
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.ChartRenderingInfo;
importorg.jfree.chart.ChartUtilities;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.labels.StandardPieToolTipGenerator;
importorg.jfree.chart.plot.PiePlot;
importorg.jfree.chart.urls.CustomPieURLGenerator;
importorg.jfree.data.general.DefaultPieDataset;

publicclassPieWithMap...{
publicstaticvoidmain(String[]args)throwsIOException...{
//第一步创建数据集
DefaultPieDatasetdataset=newDefaultPieDataset();
dataset.setValue("类别1",125.3);
dataset.setValue("类别2",52.7);
dataset.setValue("类别3",10.6);
dataset.setValue("类别4",85.96);
dataset.setValue("类别5",43.2);
//第二步创建一个JFreeChart对象
JFreeChartchart=ChartFactory.createPieChart("第一个饼图",dataset,true,//是否有图注
true,//是否有提示
false//是否有URLS
);
ChartUtilities.saveChartAsJPEG(newFile("firstPieChart.jpg"),chart,300,300);
//根据不同类型的图表使用不同类,以下是针对饼图的操作
PrintWriterw=null;
FileOutputStreamfos_jpg=null;
FileOutputStreamfos_cri=null;
//这个是重点
ChartRenderingInfoinfo=newChartRenderingInfo();
PiePlotplot=(PiePlot)chart.getPlot();
//构造一个Map用于指定具体类别的url
Mapurls=newHashMap();
urls.put("类别1","http://www.livahu.com");
urls.put("类别2","http://www.google.com");
urls.put("类别3","http://www.baidu.com");
urls.put("类别4","http://www.163.com");
urls.put("类别5","http://www.youkuaiyun.com");
CustomPieURLGeneratorcpug=newCustomPieURLGenerator();
cpug.addURLs(urls);
plot.setURLGenerator(cpug);
//设置工具提示
plot.setToolTipGenerator(newStandardPieToolTipGenerator());
fos_jpg=newFileOutputStream("firstPieChart.jpg");
//把pieChart图生成出来,写入到输出流生成图片,同时把图片的具体信息放入ChartRenderingInfo的一个实例为以后生成Map提供信息
ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,400,300,info);
fos_cri=newFileOutputStream("firstPieChart.map");
w=newPrintWriter(fos_cri);
//使用info中的图像具体信息生成关于这个图像的类别热点,并把它写到输出流中生成map文件。
ChartUtilities.writeImageMap(w,"mymap",info,true);
w.flush();
}
}
大家会注意到,ChartUtilities类在其中起了一个最为重要的作用。
我创建了一个ChartRenderingInfo对象并在调用ChartUtilities的writeChartAsJPEG时作为最后一个参数传递进去。
调用该方法结束后将产生一个图像文件以及一个填充好MAP数据的ChartRenderingInfo对象,再通过ChartUtilities的writeImageMap方法来将ChartRenderingInfo对象读取出来,写到map文件中去。
具体map文件如下
<
map
id
="mymap"
name
="mymap"
>
<
area
shape
="poly"
coords
="137,92,150,80,165,71,182,66,200,64,200,147,200,147"
onMouseOver
="returnoverlib(''类别5=43.2'');"
onMouseOut
="returnnd();"
href
="http://www.youkuaiyun.com"
/>
<
area
shape
="poly"
coords
="153,216,140,205,129,191,121,175,117,158,116,140,120,123,126,107,137,92,200,147,200,147"
onMouseOver
="returnoverlib(''类别4=85.96'');"
onMouseOut
="returnnd();"
href
="http://www.163.com"
/>
<
area
shape
="poly"
coords
="169,225,153,216,200,147,200,147"
onMouseOver
="returnoverlib(''类别3=10.6'');"
onMouseOut
="returnnd();"
href
="http://www.baidu.com"
/>
<
area
shape
="poly"
coords
="251,213,232,224,211,230,190,230,169,225,200,147,200,147"
onMouseOver
="returnoverlib(''类别2=52.7'');"
onMouseOut
="returnnd();"
href
="http://www.google.com"
/>
<
area
shape
="poly"
coords
="200,64,213,65,225,68,237,73,248,79,258,87,266,97,273,108,278,120,282,133,283,146,282,158,279,171,275,183,269,194,261,204,251,213,200,147,200,147"
onMouseOver
="returnoverlib(''类别1=125.3'',STICKY,MOUSEOFF);"
onMouseOut
="returnnd();"
href
="http://blog.youkuaiyun.com/hujinpu"
/>
</
map
>
这里还有必要说明是我用到了overlib中的overlib.js,overlib是一个开源js类库, 这是官方网站,里面说得很详细,其实一般情况下,我们只要把overlib.js这个js复制到你的目录就行了
(也可以在本站看我写的overlib简明教程)
注意类别1,我玩子一个小小的trick哦!(这里由于没有上传overlib.js这个文件,可能看不到效果)
3064

被折叠的 条评论
为什么被折叠?



