JFreeChart 柱状图参数设置方法
2011-06-10 13:50
323 查看
JFreeChart 柱状图参数设置方法
效果图:
Java代码
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.TextAnchor;
主代码:
Java代码
/**
* 格式化 JFreeChart 输出图片使用
*
*
* Author : 李斌
* Date : Nov 26, 2009
* Time : 11:50:41 AM
* Version: 1.0
*/
public class FormatPic {
/**
* 格式化纵向柱状图使用
*
* @param chart
* @returnType: void
* @author:
* @data: Nov 26, 2009
* @time: 11:51:26 AM
*/
public static void setView(JFreeChart chart){
// 初始化字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 14);
Font noFont = new Font("SansSerif", Font.TRUETYPE_FONT, 48);
// 格式化 图片
CategoryPlot plot = chart.getCategoryPlot();
// 没有数据是显示的消息
plot.setNoDataMessage("没有数据!");
//// 没有数据时显示的消息字体
plot.setNoDataMessageFont(noFont);
//// 没有数据时显示的消息颜色
plot.setNoDataMessagePaint(Color.RED);
// // 数据轴精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// // 数据轴数据标签的显示格式
CategoryAxis domainAxis = plot.getDomainAxis();
//
// // 设置刻度必须从0开始
vn.setAutoRangeIncludesZero(true);
//// 设置纵坐标数据精度
//// DecimalFormat df = new DecimalFormat("#0.00");
//// vn.setNumberFormatOverride(df);
//
//
// x轴设置
domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(labelFont);// 轴数值
// y轴设置
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// 三维设置
BarRenderer3D renderer = new BarRenderer3D();
// // 设置柱子宽度
renderer.setMaximumBarWidth(0.05);
// // 设置柱子高度
// renderer.setMinimumBarLength(0.2);
// // 设置柱子边框颜色
renderer.setBaseOutlinePaint(Color.BLACK);
// // 设置距离图片左端距离
domainAxis.setLowerMargin(0.01);
// // 设置距离图片右端距离
// domainAxis.setUpperMargin(0.2);
//
// // 设置显示位置
//// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//
// plot.setDomainAxis(domainAxis);
// // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
// plot.setBackgroundPaint(new Color(255, 255, 204));
// //设置柱子上数值的字体
// renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,14));
// renderer.setItemLabelsVisible(true);
// //设置柱子上数据的颜色
// renderer.setItemLabelPaint(Color.RED);
// 指定分类的数据标签的字体
renderer.setSeriesItemLabelFont(3,labelFont);
// 指定分类的数据标签的字体颜色
renderer.setSeriesItemLabelPaint(3,Color.RED);
//
//设置柱子上比例数值的显示,如果按照默认方式显示,数值为方向正常显示
//设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14
ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT,-1.57D);
//下面的设置是为了解决,当柱子的比例过小,而导致表示该柱子比例的数值无法显示的问题
//设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面
ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
TextAnchor.HALF_ASCENT_LEFT,-1.57D);
//设置正常显示的柱子label的position
renderer.setPositiveItemLabelPosition(itemLabelPosition);
renderer.setNegativeItemLabelPosition(itemLabelPosition);
//设置不能正常显示的柱子label的position
renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//以下设置,将按照指定格式,制定内容显示每个柱的数值。可以显示柱名称,所占百分比
// renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.0%")));
// // 横轴上的label旋转90 度
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
// // 设置每个平行柱之间距离
renderer.setItemMargin(0.05);
//
plot.setRenderer(renderer);
}
Java代码
/**
* 配置字体
* @param chart JFreeChart 对象
*/
public static void configFont(JFreeChart chart){
// 配置字体
Font xfont = new Font("宋体",Font.PLAIN,14) ;// X轴
Font yfont = new Font("宋体",Font.PLAIN,14) ;// Y轴
Font kfont = new Font("宋体",Font.PLAIN,14) ;// 底部
Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题
CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
// 图片标题
chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
// 底部
chart.getLegend().setItemFont(kfont);
// X 轴
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(xfont);// 轴标题
domainAxis.setTickLabelFont(xfont);// 轴数值
domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示
// Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色
rangeAxis.setTickLabelFont(yfont);
}
效果图:
Java代码
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.TextAnchor;
import java.awt.Color; import java.awt.Font; import java.text.DecimalFormat; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryToolTipGenerator; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.title.TextTitle; import org.jfree.ui.TextAnchor;
主代码:
Java代码
/**
* 格式化 JFreeChart 输出图片使用
*
*
* Author : 李斌
* Date : Nov 26, 2009
* Time : 11:50:41 AM
* Version: 1.0
*/
public class FormatPic {
/**
* 格式化纵向柱状图使用
*
* @param chart
* @returnType: void
* @author:
* @data: Nov 26, 2009
* @time: 11:51:26 AM
*/
public static void setView(JFreeChart chart){
// 初始化字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 14);
Font noFont = new Font("SansSerif", Font.TRUETYPE_FONT, 48);
// 格式化 图片
CategoryPlot plot = chart.getCategoryPlot();
// 没有数据是显示的消息
plot.setNoDataMessage("没有数据!");
//// 没有数据时显示的消息字体
plot.setNoDataMessageFont(noFont);
//// 没有数据时显示的消息颜色
plot.setNoDataMessagePaint(Color.RED);
// // 数据轴精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// // 数据轴数据标签的显示格式
CategoryAxis domainAxis = plot.getDomainAxis();
//
// // 设置刻度必须从0开始
vn.setAutoRangeIncludesZero(true);
//// 设置纵坐标数据精度
//// DecimalFormat df = new DecimalFormat("#0.00");
//// vn.setNumberFormatOverride(df);
//
//
// x轴设置
domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(labelFont);// 轴数值
// y轴设置
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// 三维设置
BarRenderer3D renderer = new BarRenderer3D();
// // 设置柱子宽度
renderer.setMaximumBarWidth(0.05);
// // 设置柱子高度
// renderer.setMinimumBarLength(0.2);
// // 设置柱子边框颜色
renderer.setBaseOutlinePaint(Color.BLACK);
// // 设置距离图片左端距离
domainAxis.setLowerMargin(0.01);
// // 设置距离图片右端距离
// domainAxis.setUpperMargin(0.2);
//
// // 设置显示位置
//// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//
// plot.setDomainAxis(domainAxis);
// // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
// plot.setBackgroundPaint(new Color(255, 255, 204));
// //设置柱子上数值的字体
// renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,14));
// renderer.setItemLabelsVisible(true);
// //设置柱子上数据的颜色
// renderer.setItemLabelPaint(Color.RED);
// 指定分类的数据标签的字体
renderer.setSeriesItemLabelFont(3,labelFont);
// 指定分类的数据标签的字体颜色
renderer.setSeriesItemLabelPaint(3,Color.RED);
//
//设置柱子上比例数值的显示,如果按照默认方式显示,数值为方向正常显示
//设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14
ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT,-1.57D);
//下面的设置是为了解决,当柱子的比例过小,而导致表示该柱子比例的数值无法显示的问题
//设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面
ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
TextAnchor.HALF_ASCENT_LEFT,-1.57D);
//设置正常显示的柱子label的position
renderer.setPositiveItemLabelPosition(itemLabelPosition);
renderer.setNegativeItemLabelPosition(itemLabelPosition);
//设置不能正常显示的柱子label的position
renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//以下设置,将按照指定格式,制定内容显示每个柱的数值。可以显示柱名称,所占百分比
// renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.0%")));
// // 横轴上的label旋转90 度
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
// // 设置每个平行柱之间距离
renderer.setItemMargin(0.05);
//
plot.setRenderer(renderer);
}
/** * 格式化 JFreeChart 输出图片使用 * * * Author : 李斌 * Date : Nov 26, 2009 * Time : 11:50:41 AM * Version: 1.0 */ public class FormatPic { /** * 格式化纵向柱状图使用 * * @param chart * @returnType: void * @author: * @data: Nov 26, 2009 * @time: 11:51:26 AM */ public static void setView(JFreeChart chart){ // 初始化字体 Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 14); Font noFont = new Font("SansSerif", Font.TRUETYPE_FONT, 48); // 格式化 图片 CategoryPlot plot = chart.getCategoryPlot(); // 没有数据是显示的消息 plot.setNoDataMessage("没有数据!"); //// 没有数据时显示的消息字体 plot.setNoDataMessageFont(noFont); //// 没有数据时显示的消息颜色 plot.setNoDataMessagePaint(Color.RED); // // 数据轴精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); // // 数据轴数据标签的显示格式 CategoryAxis domainAxis = plot.getDomainAxis(); // // // 设置刻度必须从0开始 vn.setAutoRangeIncludesZero(true); //// 设置纵坐标数据精度 //// DecimalFormat df = new DecimalFormat("#0.00"); //// vn.setNumberFormatOverride(df); // // // x轴设置 domainAxis.setLabelFont(labelFont);// 轴标题 domainAxis.setTickLabelFont(labelFont);// 轴数值 // y轴设置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(labelFont); rangeAxis.setTickLabelFont(labelFont); // 三维设置 BarRenderer3D renderer = new BarRenderer3D(); // // 设置柱子宽度 renderer.setMaximumBarWidth(0.05); // // 设置柱子高度 // renderer.setMinimumBarLength(0.2); // // 设置柱子边框颜色 renderer.setBaseOutlinePaint(Color.BLACK); // // 设置距离图片左端距离 domainAxis.setLowerMargin(0.01); // // 设置距离图片右端距离 // domainAxis.setUpperMargin(0.2); // // // 设置显示位置 //// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); //// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); // // plot.setDomainAxis(domainAxis); // // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) // plot.setBackgroundPaint(new Color(255, 255, 204)); // //设置柱子上数值的字体 // renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,14)); // renderer.setItemLabelsVisible(true); // //设置柱子上数据的颜色 // renderer.setItemLabelPaint(Color.RED); // 指定分类的数据标签的字体 renderer.setSeriesItemLabelFont(3,labelFont); // 指定分类的数据标签的字体颜色 renderer.setSeriesItemLabelPaint(3,Color.RED); // //设置柱子上比例数值的显示,如果按照默认方式显示,数值为方向正常显示 //设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14 ItemLabelPosition itemLabelPosition= new ItemLabelPosition( ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT,-1.57D); //下面的设置是为了解决,当柱子的比例过小,而导致表示该柱子比例的数值无法显示的问题 //设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面 ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT,-1.57D); //设置正常显示的柱子label的position renderer.setPositiveItemLabelPosition(itemLabelPosition); renderer.setNegativeItemLabelPosition(itemLabelPosition); //设置不能正常显示的柱子label的position renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback); renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback); // 显示每个柱的数值,并修改该数值的字体属性 renderer.setIncludeBaseInRange(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); //以下设置,将按照指定格式,制定内容显示每个柱的数值。可以显示柱名称,所占百分比 // renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.0%"))); // // 横轴上的label旋转90 度 // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90); // // 设置每个平行柱之间距离 renderer.setItemMargin(0.05); // plot.setRenderer(renderer); }
Java代码
/**
* 配置字体
* @param chart JFreeChart 对象
*/
public static void configFont(JFreeChart chart){
// 配置字体
Font xfont = new Font("宋体",Font.PLAIN,14) ;// X轴
Font yfont = new Font("宋体",Font.PLAIN,14) ;// Y轴
Font kfont = new Font("宋体",Font.PLAIN,14) ;// 底部
Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题
CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
// 图片标题
chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
// 底部
chart.getLegend().setItemFont(kfont);
// X 轴
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(xfont);// 轴标题
domainAxis.setTickLabelFont(xfont);// 轴数值
domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示
// Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色
rangeAxis.setTickLabelFont(yfont);
}
相关文章推荐
- JFreeChart 柱状图参数设置方法
- jfreeChart柱状图参数设置
- jfreechart柱状图参数设置
- JavaFX项目发布时虚拟机参数设置方法
- Spring XML设置bean的构造参数和属性方法
- Hive设置参数的三种方法
- JFreeChart中柱状图的详细设置
- js中对函数设置默认参数值的3种方法
- IDEA设置eclipse一样的方法自动显示参数提示
- 原生camera应用 保存设置界面参数方法
- RAC CSS diagwait参数的作用和设置方法
- JSP中初始化参数的设置和获取方法
- PowerShell函数参数设置为即可选又必选的方法
- vlc 详细使用方法:libvlc_media_add_option 函数中的参数设置
- JFreeChart中柱状图的详细设置
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
- java heap space解决方法和JVM参数设置
- js函数参数设置默认值的一种变通实现方法
- [转] -jar参数运行应用时classpath的设置方法
- axios全局请求参数设置,请求及返回拦截器的方法