📄 jf1.jsp
字号:
<%@ page contentType="text/html;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.category.DefaultCategoryDataset,
org.jfree.chart.ChartUtilities,
org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.CategoryLabelPositions,
org.jfree.chart.plot.CategoryPlot,
java.awt.*"%>
<%
//生成数据集
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
dataset2.addValue(100, "北京", "苹果");
dataset2.addValue(100, "上海", "苹果");
dataset2.addValue(100, "广州", "苹果");
dataset2.addValue(200, "北京", "梨子");
dataset2.addValue(200, "上海", "梨子");
dataset2.addValue(200, "广州", "梨子");
dataset2.addValue(300, "北京", "葡萄");
dataset2.addValue(300, "上海", "葡萄");
dataset2.addValue(300, "广州", "葡萄");
dataset2.addValue(400, "北京", "香蕉");
dataset2.addValue(400, "上海", "香蕉");
dataset2.addValue(400, "广州", "香蕉");
dataset2.addValue(500, "北京", "荔枝");
dataset2.addValue(500, "上海", "荔枝");
dataset2.addValue(500, "广州", "荔枝");
JFreeChart chart = ChartFactory.createBarChart3D(
"水果销量图", // 图表标题
"水果", // X轴的显示标题
"销量", // Y轴的显示标题
dataset2, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否包含图例
false, // 是否包含提示
false // 是否包含URL
);
// 设置图表的背景颜色
chart.setBackgroundPaint(new Color(207, 225, 235));
// 获得CategoryPlot对象的引用,用于设置更多的自定义绘制属性
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(246, 208, 146));
plot.setDomainGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.RED);
// 设置横轴标题文字的旋转方向
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// 设置图片生成路径
String fileName = ServletUtilities.saveChartAsJPEG(chart,400,300,null,null);
String url =request.getContextPath()+"/servlet/DisplayChart?filename=" + fileName;
%>
<html>
<head>
<title>JFreeChart Example </title>
</head>
<body>
<p align="center">
<img src="<%=url%>" border=1>
</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -