用JFreeChart在java中画图,解决中文乱码
最近有学生问JFreeChart画图的中文乱码问题,只好研究研究解决了
先来点介绍:JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。
我把图和代码一起放上吧,一共调了3个图,这个中文显示问题是在让人头疼,JFreeChart的作者收费提供API Docs,而且还是没有搜索功能的.只能查源代码捣鼓.
1柱状图 BarChart3DDemo1 版权版权,木哈哈哈哈
效果图:
代码:
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; import org.jfree.ui.TextAnchor; public class BarChart3DDemo1 extends ApplicationFrame { public BarChart3DDemo1(String title) { super(title); CategoryDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setFillZoomRectangle(true); chartPanel.setMouseWheelEnabled(true); chartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartPanel); } private static CategoryDataset createDataset() { double[][] data = new double[][] { { 1310, 1220, 1110, 1000 }, { 720, 700, 680, 640 }, { 1130, 1020, 980, 800 }, { 440, 400, 360, 300 } }; String[] rowKeys = { "猪肉", "牛肉", "鸡肉", "鱼肉" }; String[] columnKeys = { "广州", "深圳", "东莞", "佛山" }; CategoryDataset dataset = DatasetUtilities.createCategoryDataset( rowKeys, columnKeys, data); return dataset; } private static JFreeChart createChart(CategoryDataset dataset) { JFreeChart chart = ChartFactory.createBarChart3D("肉类销量统计图", "肉类", "销量", dataset, PlotOrientation.VERTICAL, true, true, false); chart.getTitle().setFont(new Font("宋体", 0, 12)); CategoryPlot plot = chart.getCategoryPlot(); // 设置网格背景颜色 plot.setBackgroundPaint(Color.white); // 设置网格竖线颜色 plot.setDomainGridlinePaint(Color.pink); // 设置网格横线颜色 plot.setRangeGridlinePaint(Color.pink); // 显示每个柱的数值,并修改该数值的字体属性 plot.getDomainAxis().setLabelFont(new Font("宋体", 0, 12)); plot.getDomainAxis().setTickLabelFont(new Font("宋体", 0, 12)); plot.getRangeAxis().setLabelFont(new Font("宋体", 0, 12)); LegendTitle title = (LegendTitle) chart.getSubtitle(0); title.setItemFont(new Font("宋体", 0, 12)); BarRenderer3D renderer = new BarRenderer3D(); renderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); // 默认的数字显示在柱子中,通过如下两句可调整数字的显示 // 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer.setItemLabelAnchorOffset(10D); // 设置每个地区所包含的平行柱的之间距离 // renderer.setItemMargin(0.3); plot.setRenderer(renderer); // 设置地区、销量的显示位置 // 将下方的“肉类”放到上方 plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); // 将默认放在左边的“销量”放到右方 plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); return chart; } public static void main(String[] args) { BarChart3DDemo1 demo = new BarChart3DDemo1("3D柱状图--30c.org"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }
2饼状图 PieChart3DDemo1 版权版权,木哈哈哈哈
效果图:
代码:
import java.awt.Dimension; import java.awt.Font; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PiePlot3D; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class PieChart3DDemo1 extends ApplicationFrame{ public PieChart3DDemo1(String title) { super(title); DefaultPieDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setFillZoomRectangle(true); chartPanel.setMouseWheelEnabled(true); chartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(chartPanel); } private static DefaultPieDataset createDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("初中高级程序员", 0.55); dataset.setValue("项目经理", 0.1); dataset.setValue("系统分析师", 0.1); dataset.setValue("软件架构师", 0.1); dataset.setValue("其他", 0.2); return dataset; } private static JFreeChart createChart(DefaultPieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart3D("", dataset, true, false, false); chart.setTitle(new TextTitle("30c.org-IT行业职业分布图")); PiePlot3D pieplot = (PiePlot3D) chart.getPlot(); pieplot.setLabelFont(new Font("宋体", 0, 12)); //没有数据的时候显示的内容 pieplot.setNoDataMessage("无数据显示"); pieplot.setCircular(false); pieplot.setLabelGap(0.02D); LegendTitle title = (LegendTitle) chart.getSubtitle(0); title.setItemFont(new Font("宋体", 0, 12)); return chart; } public static void main(String[] args) { PieChart3DDemo1 demo = new PieChart3DDemo1("3D饼图--30c.org"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }
3折线图 LineChartDemo1 版权版权,木哈哈哈哈
效果图:
代码:
import java.awt.Color; import java.awt.Font; import java.text.SimpleDateFormat; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.time.Month; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RectangleInsets; import org.jfree.ui.RefineryUtilities; public class LineChartDemo1 extends ApplicationFrame { public LineChartDemo1(String title) { super(title); setContentPane(createDemoPanel()); } private static XYDataset createDataset() { TimeSeries s1 = new TimeSeries("测试线a"); s1.add(new Month(2, 2001), 181.8); s1.add(new Month(3, 2001), 167.3); s1.add(new Month(4, 2001), 153.8); s1.add(new Month(5, 2001), 167.6); s1.add(new Month(6, 2001), 158.8); s1.add(new Month(7, 2001), 148.3); s1.add(new Month(8, 2001), 153.9); s1.add(new Month(9, 2001), 142.7); s1.add(new Month(10, 2001), 123.2); s1.add(new Month(11, 2001), 131.8); s1.add(new Month(12, 2001), 139.6); s1.add(new Month(1, 2002), 142.9); s1.add(new Month(2, 2002), 138.7); s1.add(new Month(3, 2002), 137.3); s1.add(new Month(4, 2002), 143.9); s1.add(new Month(5, 2002), 139.8); s1.add(new Month(6, 2002), 137.0); s1.add(new Month(7, 2002), 132.8); TimeSeries s2 = new TimeSeries("测试线b"); s2.add(new Month(2, 2001), 129.6); s2.add(new Month(3, 2001), 123.2); s2.add(new Month(4, 2001), 117.2); s2.add(new Month(5, 2001), 124.1); s2.add(new Month(6, 2001), 122.6); s2.add(new Month(7, 2001), 119.2); s2.add(new Month(8, 2001), 116.5); s2.add(new Month(9, 2001), 112.7); s2.add(new Month(10, 2001), 101.5); s2.add(new Month(11, 2001), 106.1); s2.add(new Month(12, 2001), 110.3); s2.add(new Month(1, 2002), 111.7); s2.add(new Month(2, 2002), 111.0); s2.add(new Month(3, 2002), 109.6); s2.add(new Month(4, 2002), 113.2); s2.add(new Month(5, 2002), 111.6); s2.add(new Month(6, 2002), 108.8); s2.add(new Month(7, 2002), 101.6); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(s1); dataset.addSeries(s2); return dataset; } private static JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createTimeSeriesChart("", // title "时间", // x-axis label "测试数据", // y-axis label dataset, // data true, // create legend? true, // generate tooltips? false // generate URLs? ); chart.setTitle(new TextTitle("30c.org-行业职业分布图", new Font("宋体", 0, 12)));// 主标题中文 chart.setBackgroundPaint(Color.white); LegendTitle title = (LegendTitle) chart.getSubtitles().get(0); title.setItemFont(new Font("宋体", 0, 12));// subtitle中文 XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); XYItemRenderer r = plot.getRenderer(); if (r instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; renderer.setBaseShapesVisible(true); renderer.setBaseShapesFilled(true); renderer.setDrawSeriesLineAsPath(true); } DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setLabelFont(new Font("宋体", 0, 12));// 横轴中文 axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM"));// 时间格式 ValueAxis ca = plot.getRangeAxis(); ca.setLabelFont(new Font("宋体", 0, 12));// 纵轴中文 return chart; } public static JPanel createDemoPanel() { JFreeChart chart = createChart(createDataset()); return new ChartPanel(chart); } public static void main(String[] args) { LineChartDemo1 demo = new LineChartDemo1("折线图Demo-30c.org"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }
分享到: | |
没有看到哪里表示解决乱码问题的啊
这个代码已经是带转码的了
请教楼主,如何将这样的图通过action传给jsp呢?
请注意,jfreechart只是ui绘图,可以显示效果
跟Action有啥关系呢
给出的例子也是在jsp中显示
如果你说的是action传递给jsp,就跟普通传递方式一样就行了
网速越来越慢,每次编辑都提心吊胆的怕丢数据
还有米胖的外链不是很稳定了