<%@ page language="java" 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.data.category.*,org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.ValueAxis,
org.jfree.chart.plot.CategoryPlot,
org.jfree.chart.title.TextTitle,java.awt.*,org.jfree.chart.title.LegendTitle,org.jfree.chart.axis.NumberAxis" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'jfreechartJsptest.jsp' starting page</title>
</head>
<body>
<%
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(610, "廣州", "豬肉");
dataset.addValue(220, "廣州", "牛肉");
dataset.addValue(530, "廣州", "雞肉");
dataset.addValue(340, "廣州", "魚肉");
JFreeChart chart = ChartFactory.createBarChart3D("肉類銷量統計圖",
"肉類",
"銷量",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();//x軸
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
//ValueAxis numberaxis = plot.getRangeAxis(); //y軸
TextTitle t = chart.getTitle();
t.setFont(new Font("宋體",Font.BOLD,40));//標題文字
domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//x軸標題文字
domainAxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//x軸座標上文字
numberaxis.setLabelFont(new Font("宋體",Font.BOLD,20));//y軸標題文字
numberaxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//y軸座標上文字
// LegendTitle title=chart.getLegend();
// title.setItemFont(new Font("黑體",Font.BOLD,5));//圖例文字
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,null,session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>"width=500 height=300 border=0 usemap="#<%= filename %>">
</body>
</html>
org.jfree.data.category.DefaultCategoryDataset,org.jfree.data.category.*,org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.ValueAxis,
org.jfree.chart.plot.CategoryPlot,
org.jfree.chart.title.TextTitle,java.awt.*,org.jfree.chart.title.LegendTitle,org.jfree.chart.axis.NumberAxis" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'jfreechartJsptest.jsp' starting page</title>
</head>
<body>
<%
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(610, "廣州", "豬肉");
dataset.addValue(220, "廣州", "牛肉");
dataset.addValue(530, "廣州", "雞肉");
dataset.addValue(340, "廣州", "魚肉");
JFreeChart chart = ChartFactory.createBarChart3D("肉類銷量統計圖",
"肉類",
"銷量",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();//x軸
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
//ValueAxis numberaxis = plot.getRangeAxis(); //y軸
TextTitle t = chart.getTitle();
t.setFont(new Font("宋體",Font.BOLD,40));//標題文字
domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//x軸標題文字
domainAxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//x軸座標上文字
numberaxis.setLabelFont(new Font("宋體",Font.BOLD,20));//y軸標題文字
numberaxis.setTickLabelFont(new Font("黑體",Font.BOLD,10));//y軸座標上文字
// LegendTitle title=chart.getLegend();
// title.setItemFont(new Font("黑體",Font.BOLD,5));//圖例文字
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,null,session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>"width=500 height=300 border=0 usemap="#<%= filename %>">
</body>
</html>
附:標題亂碼
- chart.getTitle().setFont(new Font("宋體", Font.BOLD,12));
其他
- 1. CategoryAxis domainAxis = plot.getDomainAxis();
- 2.
- 3. // NumberAxis valueAxis=(NumberAxis) plot.getRangeAxis();
- 4. //有人說這個是水平方向設置的 方法。
- 5. ValueAxis numberaxis = plot.getRangeAxis();
- 6.
- 7. /*------設置X軸座標上的文字-----------*/
- 8. domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
- 9.
- 10. /*------設置X軸的標題文字------------*/
- 11. domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
- 12.
- 13. /*------設置Y軸座標上的文字-----------*/
- 14. numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
- 15.
- 16. /*------設置Y軸的標題文字------------*/
- 17. numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
- 18.
- 19. /*------這句代碼解決了底部漢字亂碼的問題-----------*/
- 20. jfreechart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));