今天刷微博时刷出巨多黑店,顿时心想应该有一款小工具能够记录查找一下,免得日后还得翻微博找好久,但是,当我做了一个小字典后,深深陷入了对java GUI的不忍直视中。。。
怎么才能让java GUI好看点呢?我尝试一下做了一下对Jfreecharts的封装,把我喜欢的样式封装进去,确实好看不少,原来是这样的:
现在是这样的:
日后不用在去写很多的样式代码,省力不少,以饼状图和柱状图为例,下面放马
public static JFreeChart CreatePieChart(DefaultPieDataset dataset, String title,boolean is3D)
{
JFreeChart chart = null;
if (is3D) {
chart = ChartFactory.createPieChart3D(title, dataset, true,true,true);
}
else {
chart = ChartFactory.createPieChart(title,dataset,true,true,true);
}
chart.setTitle(new TextTitle(title, new Font("黑体",Font.PLAIN, 16)));
// 设置图例的字体==为了防止中文乱码:必须设置字体
chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 13));
// 获取饼图的Plot对象(实际图表)
PiePlot plot = (PiePlot) chart.getPlot();
plot.setOutlinePaint(Color.WHITE);
plot.setShadowPaint(Color.WHITE);
// 图形边框颜色
plot.setBaseSectionOutlinePaint(Color.WHITE);
// 图形边框粗细
plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));
// 设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制
plot.setDirection(Rotation.ANTICLOCKWISE);
// 设置绘制角度(图形旋转角度)
plot.setStartAngle(70);
// 设置背景色透明度
plot.setBackgroundAlpha(0.0F);
plot.setForegroundAlpha(0.8F);
plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
plot.setLabelBackgroundPaint(null);//标签背景颜色
plot.setLabelOutlinePaint(null);//标签边框颜色
plot.setLabelShadowPaint(null);//标签阴影颜色
if(is3D)
plot.setExplodePercent(dataset.getKey(3), 0.1D);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{1}\r\n({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
plot.setCircular(true);
plot.setNoDataMessage("找不到可用数据...");
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
return chart;
}
柱状图封装类:
public static JFreeChart createbarChart( DefaultCategoryDataset dataset,String title,String x,String y,boolean is3d)
{
JFreeChart chart = null;
if(is3d){
chart = ChartFactory.createBarChart3D(title,x,y,dataset,PlotOrientation.VERTICAL,true,false,false);
}else{
chart = ChartFactory.createBarChart(title,x,y, dataset,PlotOrientation.VERTICAL,true,false,false);
}
chart.setTitle(new TextTitle(title, new Font("黑体",Font.PLAIN, 16)));
// 设置图例的字体==为了防止中文乱码:必须设置字体
chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 13));
// 获取饼图的Plot对象(实际图表)
CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setForegroundAlpha(0.65F); // 设置前景色透明度
plot.setOutlinePaint(Color.WHITE);
// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);
CategoryAxis h = plot.getDomainAxis(); // 获取x轴
h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
h.setLabelFont(new Font("宋体", Font.PLAIN, 12));// 设置字体,防止中文乱码
h.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));// 轴数值
// h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12)); // Y轴设置字体,防止中文乱码
// 柱图的呈现器
BarRenderer renderer = new BarRenderer();
renderer.setBarPainter( new StandardBarPainter() );
// 设置柱子边框可见
renderer.setDrawBarOutline(false);
// 设置每个柱的颜色
renderer.setSeriesPaint(0, Color.BLUE);
renderer.setSeriesPaint(1, Color.GREEN);
renderer.setSeriesPaint(2, Color.RED);
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0);
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
// 设置柱的透明度
plot.setForegroundAlpha(0.75f);
renderer.setShadowVisible(false);
// 给柱图添加呈现器
plot.setRenderer(renderer);
// 没有数据的时候显示的内容
plot.setNoDataMessage("找不到可用数据...");
return chart;
}