八、时间序列图
时间序列图和折线图很相似,不同的是它在 domain轴的数据是时间而不是数字。 时间序列图的dataset 是
XYDataset 接口,具体实现类是TimeSeriesCollection ,和上面类似,有TimeSeries
对象,它被添加入
TimeSeriesCollection 。
1、创建一个数据源(dataset):
private static XYDataset createDataset()
{
TimeSeries timeseries = new TimeSeries(”L&G
European Index Trust”,Month.class);
timeseries.add(new Month(2, 2001),
181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等
timeseries.add(new Month(3, 2001), 167.3D);
timeseries.add(new Month(4, 2001), 153.8D);
timeseries.add(new Month(5, 2001), 167.6D);
timeseries.add(new Month(6, 2001), 158.8D);
timeseries.add(new Month(7, 2001), 148.3D);
timeseries.add(new Month(8, 2001), 153.9D);
timeseries.add(new Month(9, 2001), 142.7D);
timeseries.add(new Month(10, 2001), 123.2D);
timeseries.add(new Month(11, 2001), 131.8D);
timeseries.add(new Month(12, 2001), 139.6D);
timeseries.add(new Month(1, 2002), 142.9D);
timeseries.add(new Month(2, 2002), 138.7D);
timeseries.add(new Month(3, 2002), 137.3D);
timeseries.add(new Month(4, 2002), 143.9D);
timeseries.add(new Month(5, 2002), 139.8D);
timeseries.add(new Month(6, 2002), 137D);
timeseries.add(new Month(7, 2002), 132.8D);
TimeSeries timeseries1 = new TimeSeries(”L&G UK
Index Trust”,Month.class);
timeseries1.add(new Month(2, 2001), 129.6D);
timeseries1.add(new Month(3, 2001), 123.2D);
timeseries1.add(new Month(4, 2001), 117.2D);
timeseries1.add(new Month(5, 2001), 124.1D);
timeseries1.add(new Month(6, 2001), 122.6D);
timeseries1.add(new Month(7, 2001), 119.2D);
timeseries1.add(new Month(8, 2001), 116.5D);
timeseries1.add(new Month(9, 2001), 112.7D);
timeseries1.add(new Month(10, 2001), 101.5D);
timeseries1.add(new Month(11, 2001), 106.1D);
timeseries1.add(new Month(12, 2001), 110.3D);
timeseries1.add(new Month(1, 2002), 111.7D);
timeseries1.add(new Month(2, 2002), 111D);
timeseries1.add(new Month(3, 2002), 109.6D);
timeseries1.add(new Month(4, 2002), 113.2D);
timeseries1.add(new Month(5, 2002), 111.6D);
timeseries1.add(new Month(6, 2002), 108.8D);
timeseries1.add(new Month(7, 2002), 101.6D);
TimeSeriesCollection timeseriescollection = new
TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
timeseriescollection.setDomainIsPointsInTime(true);
//domain轴上的刻度点代表的是时间点而不是时间段
return timeseriescollection;
}
2、由ChartFactory 产生 JFreeChart 对象
private static JFreeChart createChart(XYDataset xydataset)
{
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(”Legal
& General Unit Trust Prices”,
“Date”,
“Price Per Unit”,
xydataset,
true,
true,
false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot :
XYPlot!!
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer =
xyplot.getRenderer();
if(xyitemrenderer instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer xylineandshaperenderer =
(XYLineAndShapeRenderer)xyitemrenderer;
xylineandshaperenderer.setDefaultShapesVisible(true); //数据点可见
xylineandshaperenderer.setDefaultShapesFilled(true);
//数据点是实心点
}
DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis(); //对domain
轴上日期显示格式定义
dateaxis.setDateFormatOverride(new
SimpleDateFormat(”MMM-yyyy”));
return jfreechart;
}
一些重要的方法:
A、增加标记线:
xyplot.addRangeMarker(new ValueMarker(550D)); //数值轴
Quarter quarter = new Quarter(2, 2002);
xyplot.addDomainMarker(new
ValueMarker(quarter.getMiddleMillisecond())); //时间轴
B、数据点的调整
XYLineAndShapeRenderer xylineandshaperenderer =
(XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setDefaultShapesVisible(true); //数据点可见
xylineandshaperenderer.setSeriesFillPaint(0, Color.red);
//数据点填充为红色
xylineandshaperenderer.setSeriesFillPaint(1, Color.white);
//数据点填充为白色
xylineandshaperenderer.setUseFillPaint(true); //应用
C、平均值曲线
这个曲线有什么用呢?很简单的例子,这里有一个以半年每天为单位的数据绘制的曲线,我们想看看以月为单位数据
的变化,这时就可以用到它了。
TimeSeries timeseries = createEURTimeSeries();
//就是以半年每天为单位的数据
TimeSeries timeseries1 =
MovingAverage.createMovingAverage(timeseries,
“30 day moving average”,
30, //30天为一个周期
30); //最开始的30天跳过
TimeSeriesCollection timeseriescollection = new
TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
return timeseriescollection;
九、总结一下
dataset plot renderer
饼图 PieDataset(DefaultPieDataset) PiePlot ——
柱状图 CatagoryDataset(DefaultCategoryDataset) CategoryPlot
BarRenderer
折线图 CatagoryDataset(DefaultCategoryDataset) CategoryPlot
LineAndShapeRenderer
XYDataset(XYSeriesCollection) XYPlot XYLineAndShapeRenderer
时间序列图 XYDataset (TimeSeriesCollection) XYPlot
XYLineAndShapeRenderer
这里只是一些常用的方法,具体还是看API
十、Item Lable
这里以柱状图为例说明,具体来说就是在每个柱状上显示它的数据,具体有下面内容:
A、使 Item Lable 可见
B、调整 Item Lable 的颜色、字体等
C、调整 Item Lable 的位置
D、定制 Item Lable 的内容
1、分配一个 Lable Generator 给 renderer
BarRenderer barrenderer =
(BarRenderer)categoryplot.getRenderer();
GategoryLableGenerator generator =new
StandardGategoryLableGenerator(
“{2}”, new DecimalFormat(”0.00″) //调整显示的数字和字符格式
);
barrenderer.setLableGenerator(generator);
2、使 Item Lable 可见
barrenderer.setItemLableVisible(true);
3、调整 Item Lable 的颜色、字体等
barrenderer.setItemLablePaint(Color.red);
barrenderer.setItemLableFont(new
Font(”SansSerif”,Font.PLAIN,10));
4、调整 Item Lable 的位置
这里涉及到一个新的对象 ItemLablePosition ,
ItemLablePosition的构造函数有两个或四个参数
public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor,
org.jfree.ui.TextAnchor textAnchor,
org.jfree.ui.TextAnchor rotationAnchor,
double angle)
itemLabelAnchor - Item Lable 的位置 (最重要的!!)
textAnchor - Item Lable里包含的正文相对于Item Lable 的位置
rotationAnchor - Item Lable里包含的正文旋转的位置
angle - 旋转的角度
ItemLabelPosition itemlabelposition = new
ItemLabelPosition(ItemLabelAnchor.INSIDE12,
TextAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT,
-1.57D);
barrenderer.setPositiveItemLabelPosition(itemlabelposition);
这样就可以每个柱状上显示它的数据了,当然可以定制 Item Lable 的内容,比如 Item Lable text
超过100的才显示,这样就需要定制自己的类,它要实现GategoryLableGenerator
接口,实现generateItemLable()方法
其他说明:
//设置Legend的位置
//((JFreeChart)
chart).getLegend().setPosition(RectangleEdge.RIGHT);
//设置最高的一个 Item 与图片顶端的距离
plot.getRangeAxis().setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
plot.getRangeAxis().setLowerMargin(0.15);
//坐标轴字体
plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN,
12));
//横轴每个分类的字体
plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.BOLD,
12));
if(labelPositionsUP_45)
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//柱图列宽度((BarRenderer)
plot.getRenderer()).setMaximumBarWidth(barWidth);
相关推荐
JFreeChart作为一款功能全面的图表绘制工具,为Java开发者提供了强大而灵活的选择。通过本文的介绍,相信读者已经掌握了如何安装和使用JFreeChart的基础知识,并能够根据实际需求选择合适的图表类型进行绘制。在未来...
在"jfreechart-1.0.13-developer-guide.pdf"中,你将找到关于JFreeChart库的全面介绍,包括如何创建、定制和展示图表的详细步骤。这份文档通常会涵盖以下关键知识点: 1. **安装与集成**:讲解如何将JFreeChart库...
通过细致地解析标题、描述以及部分已给出的内容,本文将为读者提供全面而深入的理解,帮助开发者掌握利用JFreeChart进行图表设计的核心技术。 ### JFreeChart简介 JFreeChart是一个开源的Java图表库,它可以用来...
以上知识点总结了《JFreeChart 开发指南》的主要内容,为开发者提供了从入门到进阶所需的全面指导。无论是初学者还是有经验的开发人员,都可以通过这些知识点快速上手,并深入了解JFreeChart的各种特性和高级用法。
这个源码包专注于讲解如何利用 JFreeChart 在 Web 环境下进行图表制作,涵盖了多种常见的图表类型,为开发者提供了详细的实例和代码,帮助理解和应用 JFreeChart。 1. **下载与配置 JFreeChart** 首先,你需要从...
这个1.0.6版本的资源包提供了全面的文档和支持,使得开发者可以有效地利用 JFreeChart 来实现各种复杂的图表需求。 **主要功能** 1. **图表类型丰富**:JFreeChart 支持多种图表类型,包括饼图(Pie Chart)、柱状...
总的来说,JFreeChart 1.0.13 开发指南和示例为开发者提供了全面的参考资料,无论你是初次接触 JFreeChart 还是有经验的使用者,都能从中获得宝贵的知识和实践指导。通过深入学习和实践,你可以轻松地在你的 Java ...
以下是对JFreeChart及其相关知识点的详细讲解: 1. **JFreeChart简介**:JFreeChart是一个开源项目,它为Java开发者提供了一套完整的API,用于在应用程序、Web应用或Java Applet中生成高质量的2D图表。它的设计目标...
总的来说,《JFreeChart 开发指南》是一本全面的参考书籍,无论是对初学者还是有经验的开发者,都能从中获取关于JFreeChart库的宝贵知识。通过阅读和实践,你可以掌握创建高质量、交互式图表的技能,从而提升你的...
总而言之,《JFreeChart 开发指南》全面覆盖了JFreeChart库的各个方面,无论你是初学者还是有经验的开发者,都能从中获益。通过学习和实践书中的内容,你将能够有效地利用JFreeChart为你的项目带来直观且专业的数据...
`jfreechart教程.doc`文件提供了关于JFreeChart的详细教程,涵盖了从基本使用到高级特性的全面讲解。通过学习这个教程,开发者能够掌握如何利用JFreeChart创建复杂的图表,并进行个性化定制。 总之,JFreeChart是...
本学习资料旨在帮助你全面掌握JFreeChart的使用。 首先,JFreeChart的API是其核心组成部分,它提供了大量的类和接口,如`ChartFactory`用于创建常见类型的图表,`CategoryDataset`和`TimeSeriesCollection`用于存储...
#### 二、jfreechart是什么? jfreechart是一款开源的Java图表库,它支持创建各种类型的图表,包括饼图、条形图、线图等,并且提供了一系列高度可定制的特性。这些图表可以用于报表、仪表盘或其他数据可视化项目。该...
2. **第二章:JFreeChart简介** 这一章会深入讲解JFreeChart库,介绍它的历史、特性以及如何在项目中引入和配置。JFreeChart支持多种图表类型,如柱状图、饼图、线图、散点图等,适合用于数据可视化。 3. **第三章...
#### 二、文档结构与内容概览 文档分为多个章节,覆盖了JFreeChart的基本使用方法到高级定制技巧: - **第1章:简介** - 提供了关于JFreeChart的基本介绍,以及该文档的目的。 - **第2章:样本图表** - 展示了各种...
- 该文档旨在为开发者提供全面的指南,帮助他们了解如何使用JFreeChart来创建和自定义图表。 - **受众**: 主要面向希望使用JFreeChart创建图表的开发人员。 - **内容**: 包括了从下载安装、基础图表绘制到高级...
在讲解方式上,使用基础知识与具体实例相结合的方式对Hibernate进行了全面、深入、细致的讲解,使读者在学习的过程中可以通过具体的练习来加深对讲解内容的理解和把握。 本书在讲解Hibernate的同时,还介绍了经常与...
这份教程可能包含了从基础知识到实践案例的全面讲解。 数据可视化的核心是将数据转化为图形或图像,如柱状图、饼图、折线图等,以便人们能快速理解和分析。在Java中,我们可以借助一些库来实现这一目标,如...
- **文档目的**:本官方教程旨在为开发者提供全面的指导,帮助他们更好地理解和使用JFreeChart的各种特性。 - **文档内容概述**:文档包括对JFreeChart各个方面的详细介绍,如各类图表的创建与定制、动态图表的实现...
讲解到位:对每种技术都剖析最有价值的核心部分,绝不拖泥带水 代码经典:提供了大量高质量代码供读者理解,并对代码进行了详细注释 示例丰富:提供了600余个从实际项目总结的示例程序,有较高的应用价值 工具...