jfreechart 折线图怎样显示数据值

 我来答
lichengfangyes
推荐于2016-07-04
知道答主
回答量:4
采纳率:0%
帮助的人:6.1万
展开全部
//给你个源代码,上面有注释说明哪段是显示数据的
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.*"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.servlet.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.chart.renderer.*"%>
<%@ page import="org.jfree.chart.entity.*"%>
<%@ page import="org.jfree.chart.title.*"%>
<%@ page import="org.jfree.chart.axis.*"%>
<%@ page import="org.jfree.chart.renderer.category.LineAndShapeRenderer"%>
<%@ page import="org.jfree.data.*"%>
<%@ page import="org.jfree.data.general.*"%>
<%@ page import="org.jfree.data.category.*"%>
<%@ page import="org.jfree.ui.*"%>
<%@ page import="org.jfree.util.*"%>
<%@ page import="org.jfree.chart.labels.*"%>

<%!

String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"};
String category[] = {"第1周", "第2周", "第3周", "第4周" };
double bookSales;
String chartTitle = "JFreeChart实例11: 自定义线段图";

// 创建数据集
public CategoryDataset createDataset()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i=0; i < bookTitle.length; i++)
{
for (int j=0; j < category.length; j++ )
{
bookSales = 1 + (Math.random() * 100);
dataset.addValue(bookSales, bookTitle[i], category[j]);
}
}
return dataset;
}

public JFreeChart createChart(CategoryDataset dataset)
{
// 创建图表对象
JFreeChart chart = ChartFactory.createLineChart
(
chartTitle, // 图表标题
"销售时间:2005年2月", // 坐标标题
"销售量", // 坐标标题
dataset, // 定义绘制数据
PlotOrientation.VERTICAL, // 直方图的方向
true, // 定义图表是否包含图例
true, // 定义图表是否包含提示
false // 定义图表是否包含URL
);
return chart;
}
%>
<%
// 创建一个 500X375 的图像
int width=500, height=375;

CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);

// 开始自定义图表绘制的相关属性

// 设置图表的背景颜色
chart.setBackgroundPaint(new Color(205, 241, 197));

// 自定义图表的标题的字体和颜色
TextTitle title = chart.getTitle();
title.setFont(new Font("黑体", Font.BOLD, 25));

// 获得图表对象的引用,用于设置更多的自定义绘制属性
CategoryPlot plot = (CategoryPlot) chart.getPlot();
GradientPaint bg = new GradientPaint(0, 50, new Color(248, 253, 255),
0, 250, new Color(205, 237, 252));
plot.setBackgroundPaint(bg);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.RED);

// 设置横轴标题的字体
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));

// 设置纵轴标题文字的字体及其旋转方向
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
rangeAxis.setLabelAngle(Math.PI/2);

// 自定义图例的显示风格
StandardLegend legend = (StandardLegend) chart.getLegend();
legend.setDisplaySeriesShapes(true);
legend.setShapeScaleX(1.5);
legend.setShapeScaleY(1.5);
legend.setDisplaySeriesLines(true);

// 获取渲染对象
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseItemLabelsVisible(true);
//renderer.setDrawShapes(true);
//renderer.setShapesFilled(true);

//设置数据显示位置
//ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER_LEFT,TextAnchor.CENTER_LEFT, -Math.PI / 2.0 );
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
//显示折点相应数据
renderer.setBaseLabelGenerator(new StandardCategoryLabelGenerator());
// 自定义线段的绘制颜色
Color color[] = new Color[bookTitle.length];
color[0] = new Color(99,99,0);
color[1] = new Color(255,169,66);
color[2] = new Color(33,255, 66);
color[3] = new Color(33,0,255);
color[4] = new Color(255,0,66);
for (int i = 0; i < color.length; i++)
{
renderer.setSeriesPaint(i, color[i]);
}

// 自定义线段的绘制风格
BasicStroke bs ;
for (int i = 0; i < bookTitle.length; i++)
{
float dashes[] = {10.0f};
bs = new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 10.f, dashes, 0.0f);
if (i % 2 != 0)
renderer.setSeriesStroke(i, bs);
else
renderer.setSeriesStroke(i, new BasicStroke(2.0f));
}

// 结束自定义图表绘制的相关属性

ChartRenderingInfo info =
new ChartRenderingInfo(new StandardEntityCollection());

// 设置图片生成格式
String fileName =
ServletUtilities.saveChartAsPNG(chart, width, height, info, session);

// 设置图片生成路径
String graphURL =
request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;

%>
<HTML>
<HEAD>
<TITLE><%=chartTitle%></TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
<img src="<%=graphURL %>" border="1" >
</P>
</BODY>
</HTML>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
可以叫我表哥
推荐于2016-02-27 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464973
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
jfreechart 折线图显示数据节点:
import java.awt.*;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.RectangleInsets;

/**
* A line chart demo showing the use of a custom drawing supplier.
*
*/
public class LineChartDemo2 extends ApplicationFrame
{

/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo2(final String title)
{
super(title);
TimeSeries timeSeries = new TimeSeries("阿蜜果blog访问量统计", Month.class);
//时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
//构造数据集合
timeSeries.add(new Month(1, 2007), 11200);
timeSeries.add(new Month(2, 2007), 9000);
timeSeries.add(new Month(3, 2007), 6200);
timeSeries.add(new Month(4, 2007), 8200);
timeSeries.add(new Month(5, 2007), 8200);
timeSeries.add(new Month(6, 2007), 12200);
timeSeries.add(new Month(7, 2007), 13200);
timeSeries.add(new Month(8, 2007), 8300);
timeSeries.add(new Month(9, 2007), 12400);
timeSeries.add(new Month(10, 2007), 12500);
timeSeries.add(new Month(11, 2007), 13600);
timeSeries.add(new Month(12, 2007), 12500);

lineDataset.addSeries(timeSeries);
JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true);
//设置子标题
TextTitle subtitle = new TextTitle("2007年度", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
//设置主标题
chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15)));
chart.setAntiAlias(true);

XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//设置曲线图与xy轴的距离
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
//设置曲线是否显示数据点
xylineandshaperenderer.setBaseShapesVisible(true);
//设置曲线显示各数据点的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));

//下面三句是对设置折线图数据标示的关键代码
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setRenderer(xyitem);

final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);

}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(final String[] args){

final LineChartDemo2 demo = new LineChartDemo2("Line Chart Demo 5");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式