createBarChart3D 未定义标识符

下次自动登录
现在的位置:
& 综合 & 正文
BarChart3D(2)
import org.jfree.chart.ChartFimport org.jfree.chart.ChartPimport org.jfree.chart.JFreeCimport org.jfree.chart.axis.CategoryAimport org.jfree.chart.axis.CategoryLabelPimport org.jfree.chart.axis.CategoryLabelPimport org.jfree.chart.axis.CategoryLabelWidthTimport org.jfree.chart.plot.CategoryPimport org.jfree.chart.plot.PlotOimport org.jfree.data.category.CategoryDimport org.jfree.data.category.DefaultCategoryDimport org.jfree.text.TextBlockAimport org.jfree.ui.ApplicationFimport org.jfree.ui.RectangleAimport org.jfree.ui.RefineryUimport org.jfree.ui.TextAimport org.jfree.util.Limport org.jfree.util.PrintStreamLogT/** * A simple demonstration application showing how to create a horizontal 3D bar chart using data * from a {@link CategoryDataset}. * */public class BarChart3DDemo2 extends ApplicationFrame {
// ****************************************************************************
// * JFREECHART DEVELOPER GUIDE
// * The JFreeChart Developer Guide, written by David Gilbert, is available
// * to purchase from Object Refinery Limited:
// * http://www.object-refinery.com/jfreechart/guide.html
// * Sales are used to provide funding for the JFreeChart project - please
// * support us so that we can continue developing free software.
// ****************************************************************************
* Creates a new demo.
* @param title
the frame title.
public BarChart3DDemo2(final String title) {
super(title);
// create the chart...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(<font color="#.0, "Series 1", "London");
dataset.addValue(<font color="#.0, "Series 1", "New York");
dataset.addValue(<font color="#.0, "Series 1", "Istanbul");
dataset.addValue(<font color="#.0, "Series 1", "Cairo");
dataset.addValue(<font color="#.0, "Series 2", "London");
dataset.addValue(<font color="#.0, "Series 2", "New York");
dataset.addValue(<font color="#.0, "Series 2", "Istanbul");
dataset.addValue(<font color="#.0, "Series 2", "Cairo");
dataset.addValue(<font color="#.0, "Series 3", "London");
dataset.addValue(<font color="#.0, "Series 3", "New York");
dataset.addValue(<font color="#.0, "Series 3", "Istanbul");
dataset.addValue(<font color="#.0, "Series 3", "Cairo");
chart = createChart(dataset);
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(<font color="#0, <font color="#0));
setContentPane(chartPanel);
* Creates a chart.
* @param dataset
the dataset.
* @return The chart.
createChart(final CategoryDataset dataset) {
chart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo 2",
// chart title
"Category",
// domain axis label
// range axis label
PlotOrientation.HORIZONTAL,
// orientation
// include legend
// tooltips
final CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(<font color="#.0f);
// left align the category labels...
final CategoryAxis axis = plot.getDomainAxis();
final CategoryLabelPositions p = axis.getCategoryLabelPositions();
final CategoryLabelPosition left = new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
TextAnchor.CENTER_LEFT, <font color="#.0,
CategoryLabelWidthType.RANGE, <font color="#.30f
axis.setCategoryLabelPositions(CategoryLabelPositions.replaceLeftPosition(p, left));
* Starting point for the demonstration application.
* @param args
public static void main(final String[] args) {
Log.getInstance().addTarget(new PrintStreamLogTarget());
final BarChart3DDemo2 demo = new BarChart3DDemo2("3D Bar Chart Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
【上篇】【下篇】使用jfreechart生成柱状图、折线图、和饼状图
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications,
applets, servlets 以及JSP等使用所设计。下面我就详细介绍如何使用jfreechart生成柱状图、折线图、和饼状图。
①、导入其相应的jcommon-1.0.16.jar和jfreechart-1.0.13.jar文件(可点击下载)
②、下面就可以写实现各种图形的代码了
A、生成柱状图:
package com.whp.
import java.awt.F
import java.io.F
import java.io.IOE
import org.jfree.chart.ChartF
import org.jfree.chart.ChartF
import org.jfree.chart.ChartU
import org.jfree.chart.JFreeC
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickU
import org.jfree.chart.labels.StandardCategoryItemLabelG
import org.jfree.chart.plot.CategoryP
import org.jfree.chart.plot.PlotO
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryD
public class jfreeChart {
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
//添加数据
dataset.addValue(98, "数学", "张三");
dataset.addValue(87, "语文", "张三");
dataset.addValue(68, "数学", "李四");
dataset.addValue(89, "语文", "李四");
dataset.addValue(56, "数学", "王五");
dataset.addValue(96, "语文", "王五");
JFreeChart chart=ChartFactory.createBarChart3D(
"成绩统计表",
"学生姓名",//X轴的标签
"分数",//Y轴的标签
dataset, //图标显示的数据集合
PlotOrientation.VERTICAL, //图像的显示形式(水平或者垂直)
true,//是否显示子标题
true,//是否生成提示的标签
true); //是否生成URL链接
//处理图形上的乱码
//处理主标题的乱码
chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));
//处理子标题乱码
chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,15));
//获取图表区域对象
CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();
//获取X轴的对象
CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();
//获取Y轴的对象
NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();
//处理X轴上的乱码
categoryAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15));
//处理X轴外的乱码
categoryAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴上的乱码
numberAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴外的乱码
numberAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15));
//处理Y轴上显示的刻度,以10作为1格
numberAxis3D.setAutoTickUnitSelection(false);
NumberTickUnit unit = new NumberTickUnit(10);
numberAxis3D.setTickUnit(unit);
//获取绘图区域对象
BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();
//设置柱形图的宽度
barRenderer3D.setMaximumBarWidth(0.07);
//在图形上显示数字
barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
barRenderer3D.setBaseItemLabelsVisible(true);
barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15));
//在D盘目录下生成图片
File file = new File("chart.jpeg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
} catch (IOException e) {
e.printStackTrace();
//使用ChartFrame对象显示图像
ChartFrame frame = new ChartFrame("xyz",chart);
frame.setVisible(true);
frame.pack();
vcD4KPHA+QqGiyfqzydXbz9/NvDwvcD4KPHA+PHByZSBjbGFzcz0="brush:">package com.whp.
import java.awt.F
import java.awt.R
import java.io.F
import java.io.IOE
import org.jfree.chart.ChartF
import org.jfree.chart.ChartF
import org.jfree.chart.ChartU
import org.jfree.chart.JFreeC
import org.jfree.chart.axis.CategoryA
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberA
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickU
import org.jfree.chart.labels.StandardCategoryItemLabelG
import org.jfree.chart.plot.CategoryP
import org.jfree.chart.plot.PlotO
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeR
import org.jfree.data.category.DefaultCategoryD
public class jfreeChart1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// 添加数据
dataset.addValue(98, "数学", "张三");
dataset.addValue(68, "数学", "李四");
dataset.addValue(56, "数学", "王五");
JFreeChart chart = ChartFactory.createLineChart("用户统计报表(所属单位)", // 主标题的名称
"所属单位名称",// X轴的标签
"数量",// Y轴的标签
dataset, // 图标显示的数据集合
PlotOrientation.VERTICAL, // 图像的显示形式(水平或者垂直)
true,// 是否显示子标题
true,// 是否生成提示的标签
true); // 是否生成URL链接
// 处理图形上的乱码
// 处理主标题的乱码
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18));
// 处理子标题乱码
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
// 获取图表区域对象
CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
// 获取X轴的对象
CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
// 获取Y轴的对象
NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
// 处理X轴上的乱码
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15));
// 处理X轴外的乱码
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
// 处理Y轴上的乱码
numberAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15));
// 处理Y轴外的乱码
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
// 处理Y轴上显示的刻度,以10作为1格
numberAxis.setAutoTickUnitSelection(false);
NumberTickUnit unit = new NumberTickUnit(10);
numberAxis.setTickUnit(unit);
// 获取绘图区域对象
LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot
.getRenderer();
// 在图形上显示数字
lineAndShapeRenderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineAndShapeRenderer.setBaseItemLabelsVisible(true);
lineAndShapeRenderer
.setBaseItemLabelFont(new Font("宋体", Font.BOLD, 15));
// 在图形上添加转折点(使用小矩形显示)
Rectangle shape = new Rectangle(10, 10);
lineAndShapeRenderer.setSeriesShape(0, shape);
lineAndShapeRenderer.setSeriesShapesVisible(0, true);
//在D盘目录下生成图片
File file = new File("chart1.jpg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
} catch (IOException e) {
e.printStackTrace();
// 使用ChartFrame对象显示图像
ChartFrame frame = new ChartFrame("xyz", chart);
frame.setVisible(true);
frame.pack();
C、生成饼图:
package com.whp.
import java.awt.F
import java.awt.R
import java.io.F
import java.io.IOE
import org.jfree.chart.ChartF
import org.jfree.chart.ChartF
import org.jfree.chart.ChartU
import org.jfree.chart.JFreeC
import org.jfree.chart.axis.CategoryA
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberA
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickU
import org.jfree.chart.labels.StandardCategoryItemLabelG
import org.jfree.chart.labels.StandardPieSectionLabelG
import org.jfree.chart.plot.CategoryP
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotO
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeR
import org.jfree.data.category.DefaultCategoryD
import org.jfree.data.general.DefaultPieD
public class jfreeChart3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultPieDataset dataset = new DefaultPieDataset();
//添加数据
dataset.setValue("张三",40);
dataset.setValue("李四",32);
dataset.setValue("王五",28);
JFreeChart chart = ChartFactory.createPieChart3D("比重统计报表(所属单位)", //主标题的名称
dataset, //图标显示的数据集合
true,//是否显示子标题
true,//是否生成提示的标签
true); //是否生成URL链接
//处理图形上的乱码
//处理主标题的乱码
chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));
//处理子标题乱码
chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,15));
//获取图表区域对象
PiePlot3D categoryPlot = (PiePlot3D)chart.getPlot();
//处理图像上的乱码
categoryPlot.setLabelFont(new Font("宋体",Font.BOLD,15));
//设置图形的生成格式为(上海 2 (10%))
String format = "{0} {1} ({2})";
categoryPlot.setLabelGenerator(new StandardPieSectionLabelGenerator(format));
//在D盘目录下生成图片
File file = new File("chart2.jpg");
ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
} catch (IOException e) {
e.printStackTrace();
//使用ChartFrame对象显示图像
ChartFrame frame = new ChartFrame("xyz",chart);
frame.setVisible(true);
frame.pack();用JFreeChart画统计分析柱状图
  我们介绍使用 JFreeChart 生成柱状图,首先从一个最简单的例子开始。
  一 最简单的例子
  为了降低门槛,让大家心理有个底,先介绍一个简单的不能再简单的例子,图片中的各类属性都采用默认值。
&%@ page contentType="text/charset=GBK"%&&%@ page import="org.jfree.chart.ChartFactory,&&&&&&&&&&&&&&&&&org.jfree.chart.JFreeChart,&&&&&&&&&&&&&&&&&org.jfree.chart.plot.PlotOrientation,&&&&&&&&&&&&&&&&&org.jfree.chart.servlet.ServletUtilities,&&&&&&&&&&&&&&&&&org.jfree.data.DefaultCategoryDataset"%&&%DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(300, "广州", "苹果");dataset.addValue(200, "广州", "梨子");dataset.addValue(500, "广州", "葡萄");dataset.addValue(340, "广州", "芒果");dataset.addValue(280, "广州", "荔枝");JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", &&&&&&&&&&&&&&&&&&"水果",&&&&&&&&&&&&&&&&&&"销量",&&&&&&&&&&&&&&&&&&dataset,&&&&&&&&&&&&&&&&&&PlotOrientation.VERTICAL,&&&&&&&&&&&&&&&&&&false,&&&&&&&&&&&&&&&&&&false,&&&&&&&&&&&&&&&&&&false);String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" +%&&img src="&%= graphURL %&" width=500 height=300 border=0 usemap="#&%= filename %&"&
这个 JSP 程序运行的结果如下图
  二 柱状图高级特性
  上面的程序简单,但生成的柱状图也很简单。更多的时候,我们可能需要不同的效果。 org.jfree.chart.ChartFactory 这个工厂类有 createBarChart, createStackedBarChart, createBarChart3D, createStackedBarChart3D 这几个工厂方法创建不同类型的柱状图。关于这四个方法的 JFreeChart 的 Java Doc API 文档有详细说明,比较重要的是 PlotOrientation.VERTICAL 让平行柱垂直显示,而 PlotOrientation.HORIZONTAL 则让平行柱水平显示。
几个对柱状图影响较大的几个类,它们分别是:org.jfree.chart.axis.CategoryAxisorg.jfree.chart.axis.ValueAxisorg.jfree.chart.renderer.BarRendererorg.jfree.chart.renderer.BarRenderer3D
我们还是以实例来说明这几个类,先来假设一个需要统计的数据表:
根据上表数据,首先构造 CategoryDataset, 这里不再使用上面简单例子里面的 DefaultCategoryDataset 类,而是 DatasetUtilities 更有效的构造 CategoryDataset,如下列代码:
double[][] data = new double[][] {{672, 766, 223, 540, 126}, {325, 521, 210, 340, 106}, {332, 256, 523, 240, 526}};String[] rowKeys = {"苹果","梨子","葡萄"};String[] columnKeys = {"北京","上海","广州","成都","深圳"};CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
用上面的 dataset 生成的 3D 柱状图
org.jfree.chart.axis.CategoryAxis
CategoryAxis domainAxis = plot.getDomainAxis();//设置 columnKey 是否垂直显示domainAxis.setVerticalCategoryLabels(true);//设置距离图片左端距离domainAxis.setLowerMargin(0.1);//设置距离图片右端距离domainAxis.setUpperMargin(0.1);//设置 columnKey 是否间隔显示domainAxis.setSkipCategoryLabelsToFit(true);plot.setDomainAxis(domainAxis);
上面代码产生的效果如下图,注意与图二的区别。
org.jfree.chart.axis.ValueAxis
ValueAxis rangeAxis = plot.getRangeAxis();//设置最高的一个柱与图片顶端的距离rangeAxis.setUpperMargin(0.15);//设置最低的一个柱与图片底端的距离//rangeAxis.setLowerMargin(0.15);plot.setRangeAxis(rangeAxis);
上面代码产生的效果如下图,注意与图二的区别。
org.jfree.chart.renderer.BarRenderer3D
BarRenderer3D renderer = new BarRenderer3D();renderer.setBaseOutlinePaint(Color.BLACK);//设置 Wall 的颜色renderer.setWallPaint(Color.gray);//设置每种水果代表的柱的颜色renderer.setSeriesPaint(0, new Color(0, 0, 255));renderer.setSeriesPaint(1, new Color(0, 100, 255));renderer.setSeriesPaint(2, Color.GREEN);//设置每种水果代表的柱的 Outline 颜色renderer.setSeriesOutlinePaint(0, Color.BLACK);renderer.setSeriesOutlinePaint(1, Color.BLACK);renderer.setSeriesOutlinePaint(2, Color.BLACK);//设置每个地区所包含的平行柱的之间距离renderer.setItemMargin(0.1);//显示每个柱的数值,并修改该数值的字体属性renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,12));renderer.setItemLabelsVisible(true);
上面代码产生的效果如下图,注意与图二的区别。
  补充两个有用的方法
  补充 org.jfree.chart.plot.CategoryPlot 的两个方法,这两个方法对所有类型的图表都有作用,因为在前面没有介绍,这里补充一下。
  //设置地区、销量的显示位置plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
上面代码产生的效果如下图,注意与图二的区别。
  三 完整范例
  前面都是一些代码片段,现在把这些片段组合成一个完整范例。
&%@ page contentType="text/charset=GBK"%&&%@ page import="java.awt.Color,&&&&&&&&&&&&&&&&java.awt.Font,&&&&&&&&&&&&&&&&org.jfree.chart.ChartFactory,&&&&&&&&&&&&&&&&org.jfree.chart.JFreeChart,&&&&&&&&&&&&&&&&org.jfree.chart.plot.PlotOrientation,&&&&&&&&&&&&&&&&org.jfree.chart.servlet.ServletUtilities,&&&&&&&&&&&&&&&&org.jfree.data.CategoryDataset,&&&&&&&&&&&&&&&&org.jfree.data.DatasetUtilities,&&&&&&&&&&&&&&&&org.jfree.chart.plot.CategoryPlot,&&&&&&&&&&&&&&&&org.jfree.chart.axis.CategoryAxis,&&&&&&&&&&&&&&&&org.jfree.chart.axis.ValueAxis,&&&&&&&&&&&&&&&&org.jfree.chart.renderer.BarRenderer3D,&&&&&&&&&&&&&&&&org.jfree.chart.labels.StandardCategoryItemLabelGenerator,&&&&&&&&&&&&&&&&org.jfree.chart.axis.AxisLocation"%&&%double[][] data = new double[][] {{672, 766, 223, 540, 126},{325, 521, 210, 340, 106},{332, 256, 523, 240, 526}};String[] rowKeys = {"苹果","梨子","葡萄"};String[] columnKeys = {"北京","上海","广州","成都","深圳"};CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",&&&&&&&&&&&&&&&&&&null,&&&&&&&&&&&&&&&&&&null,&&&&&&&&&&&&&&&&&&dataset,&&&&&&&&&&&&&&&&&&PlotOrientation.VERTICAL,&&&&&&&&&&&&&&&&&&true,false,false);chart.setBackgroundPaint(Color.WHITE);CategoryPlot plot = chart.getCategoryPlot();CategoryAxis domainAxis = plot.getDomainAxis();domainAxis.setVerticalCategoryLabels(false);plot.setDomainAxis(domainAxis);ValueAxis rangeAxis = plot.getRangeAxis();//设置最高的一个 Item 与图片顶端的距离rangeAxis.setUpperMargin(0.15);//设置最低的一个 Item 与图片底端的距离rangeAxis.setLowerMargin(0.15);plot.setRangeAxis(rangeAxis);BarRenderer3D renderer = new BarRenderer3D();renderer.setBaseOutlinePaint(Color.BLACK);//设置 Wall 的颜色renderer.setWallPaint(Color.gray);//设置每种水果代表的柱的颜色renderer.setSeriesPaint(0, new Color(0, 0, 255));renderer.setSeriesPaint(1, new Color(0, 100, 255));renderer.setSeriesPaint(2, Color.GREEN);//设置每个地区所包含的平行柱的之间距离renderer.setItemMargin(0.1);//显示每个柱的数值,并修改该数值的字体属性renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setItemLabelsVisible(true);plot.setRenderer(renderer);//设置柱的透明度plot.setForegroundAlpha(0.6f);//设置地区、销量的显示位置plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" +%s&&img src="&%= graphURL %&" width=500 height=300 border=0 usemap="#&%= filename %&"&
看看程序运行的结果吧:
  三 总结
  我只介绍了少量的方法,更多的请参考 JFreeChart 的 Java Doc API 文档和 Sample Code。
最新五条评论
?用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
?本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
?请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为博客分类:
前面刚整理了关于JFreeChart的基本使用:
补充:生成立体柱状图(返回立体柱状图的JFreeChart对象)
代码如下,很多配置依然和前面相同,很多属性的配置可以参考前面的例子。
public JFreeChart createBarChart3D(String title, String botTitle, String leftTitle, CategoryDataset data, boolean url) {
// 创建栈堆型柱状图形
JFreeChart chart = ChartFactory.createBarChart3D(title, botTitle, leftTitle, data, PlotOrientation.VERTICAL, true, true, url);
CategoryPlot plot = chart.getCategoryPlot();
StandardCategoryItemLabelGenerator scg = new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, new DecimalFormat("0"));
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
// 将每个柱的数值显示在柱顶
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 设置标签是否显示
renderer.setBaseItemLabelsVisible(true);
// 设置标签数字的显示位置
renderer.setBaseItemLabelsVisible(true);
// 设置超连接
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
// 超连接可以扩展StandardCategoryURLGenerator对象的方法generateURL();
renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("aa.html"));
// 设置柱型的间距
renderer.setMaximumBarWidth(0.2);
renderer.setBaseItemLabelGenerator(scg);
plot.setRenderer(renderer);
// 设置柱的透明度
plot.setForegroundAlpha(0.9f);
ValueAxis va = chart.getCategoryPlot().getRangeAxis();
// 设置Y轴的最小值
va.setLowerBound(0);
不仅可以让轴数值进行一定角度的旋转,当柱图显示的数值太长时,为避免出现覆盖的情况,也可以对柱图上面的数值进行旋转,代码如下:
public JFreeChart createBarChart(String title, CategoryDataset dataSet, String Xtitle, String Ytitle, boolean url) {
// 创建拄图类型的图表对象
JFreeChart chart = ChartFactory.createBarChart(title, Xtitle, Ytitle, dataSet, PlotOrientation.VERTICAL, true, false, url);
StandardCategoryItemLabelGenerator scg = new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, new DecimalFormat("0"));
// 显示的图例放到右边
// chart.getLegend().setPosition(RectangleEdge.RIGHT);
Font ticketLableFont = new Font("Arial", Font.PLAIN, 11);
Font labelFont = new Font("Arial", Font.PLAIN, 12);
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
// 设置详细图表的显示细节部分的背景颜色
plot.setBackgroundPaint(new Color(255, 255, 204));
// 设置是否显示水平网格线
plot.setRangeGridlinesVisible(true);
// 设置水平网格线颜色
plot.setRangeGridlinePaint(Color.black);
// 将所有数据转换为整数形式
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// 设置X轴标题的倾斜程度
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(ticketLableFont);// 轴数值
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
// 设置柱状体与图片边框的左右间距
// domainAxis.setLowerMargin(0.01);
// domainAxis.setUpperMargin(0.01);
// 设置柱状体与图片边框的上下间距
ValueAxis rAxis = plot.getRangeAxis();
rAxis.setLabelFont(labelFont);
rAxis.setTickLabelFont(ticketLableFont);
rAxis.setUpperMargin(0.15);
rAxis.setLowerMargin(0.15);
BarRenderer barRenderer = new BarRenderer();
// 设置每个柱的最大宽度
barRenderer.setMaximumBarWidth(0.20);
// 设置每一个柱状图都显示数值
barRenderer.setBaseItemLabelGenerator(scg);
barRenderer.setBaseItemLabelsVisible(true);
ItemLabelPosition itemLabelPosition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT, -0.57D);
barRenderer.setBasePositiveItemLabelPosition(itemLabelPosition);
barRenderer.setBaseNegativeItemLabelPosition(itemLabelPosition);
ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT, -0.57D);
barRenderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
barRenderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
plot.setRenderer(0, barRenderer);
// 设置是否在柱图的状态条上显示边框
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
BarRenderer render = (BarRenderer) plot.getRenderer();
render.setItemMargin(0.0);
render.setBaseItemLabelFont(new Font("Arial Narrow", Font.TRUETYPE_FONT, 10));
// 设置没个柱状的颜色
renderer.setSeriesPaint(0, new Color(128, 128, 192));
renderer.setSeriesPaint(1, new Color(128, 0, 64));
renderer.setSeriesPaint(2, new Color(128, 128, 0));
zhangzhenting
浏览: 120872 次
来自: 火星
不错,明天好试验试验
谢谢,找好多例子终于成功一个了
传说中单点登录,哈哈
sat on the fix ?? all day ???
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 未定义 的文章

 

随机推荐