【JAVA图表】Jfreechart常用图表总结

来源:互联网 发布:双子星股票交易软件 编辑:程序博客网 时间:2024/05/01 12:12

       这个寒假在学习机器学习,很多案例中的数据集需要用图表呈现,因此,我总结了一些常用的图表代码,为日后使用提供方便。

       说明:以下的代码只涉及极少一部分jfreechart的API,如果想进一步了解,请访问它的主页http://www.jfree.org/jfreechart/ 。并且,以下的代码均不可以直接复制然后运行,需要进一步实现自己的数据集,数据集的接口,我已经预留,大家实现即可。对于下面的图表配图,来自我之前的博文,因此可能存在小部分不匹配的问题,但大致效果相同。

柱状图:

package chart;import java.awt.Color;import java.awt.Font;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.DefaultCategoryDataset;/** * @author shenchao *柱状图 */public class BarChart {public BarChart() {Map<String,Integer> map = initDataSet();showChart(map);}/** * 初始化数据集 * 柱状图的数据集接受三个参数,第一个参数为柱子的高度,第二个为几个类别进行比较,第三个为共有几组数据 *  * @return */private Map<String, Integer> initDataSet() {return null;}/** * @param map */private void showChart(Map<String, Integer> map) {// 创建饼图数据对象DefaultCategoryDataset dataset = new DefaultCategoryDataset();Set<Entry<String, Integer>> set = map.entrySet();for (Entry<String, Integer> entry : set) {dataset.setValue(entry.getValue(), "评论数量",entry.getKey());}JFreeChart chart = ChartFactory.createBarChart3D("评论次数TOP10", "好友昵称","评论数量", dataset, PlotOrientation.VERTICAL, false, true, true);ChartFrame frame = new ChartFrame("评论次数TOP10", chart, true);// 自定义设定背景色// chart.setBackgroundPaint(Color.getHSBColor(23,192,223));chart.setBackgroundPaint(Color.WHITE);// 获得 plot:3dBar为CategoryPlotCategoryPlot categoryPlot = chart.getCategoryPlot();// 设定图表数据显示部分背景色categoryPlot.setBackgroundPaint(Color.WHITE);// 横坐标网格线categoryPlot.setDomainGridlinePaint(Color.GRAY);// 设置网格线可见categoryPlot.setDomainGridlinesVisible(true);// 纵坐标网格线categoryPlot.setRangeGridlinePaint(Color.GRAY);// 重要的类,负责生成各种效果// BarRenderer3D renderer=(BarRenderer3D) categoryPlot.getRenderer();// 获取纵坐标NumberAxis numberaxis = (NumberAxis) categoryPlot.getRangeAxis();// 设置纵坐标的标题字体和大小numberaxis.setLabelFont(new Font("黑体", Font.CENTER_BASELINE, 16));// 设置丛坐标的坐标值的字体颜色numberaxis.setLabelPaint(Color.BLACK);// 设置丛坐标的坐标轴标尺颜色numberaxis.setTickLabelPaint(Color.BLACK);// 坐标轴标尺颜色numberaxis.setTickMarkPaint(Color.BLUE);// 丛坐标的默认间距值// numberaxis.setAutoTickUnitSelection(true);// 设置丛坐标间距值numberaxis.setAutoTickUnitSelection(true);// numberaxis.setTickUnit(new NumberTickUnit(150));//在柱体的上面显示数据         BarRenderer custombarrenderer3d = new BarRenderer();         custombarrenderer3d.setBaseItemLabelPaint(Color.BLACK);//数据字体的颜色         custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());         custombarrenderer3d.setBaseItemLabelsVisible(true);         categoryPlot.setRenderer(custombarrenderer3d); // 获取横坐标CategoryAxis domainAxis = categoryPlot.getDomainAxis();// 设置横坐标的标题字体和大小domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));// 设置横坐标的坐标值的字体颜色domainAxis.setTickLabelPaint(Color.GRAY);// 设置横坐标的坐标值的字体domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 16));// 设置横坐标的显示domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));// 这句代码解决了底部汉字乱码的问题//chart.getLegend().setItemFont(new Font("黑体", 0, 16));// 设置图例标题Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 20);TextTitle title = new TextTitle("谁是评论你最多的人?");title.getBackgroundPaint();title.setFont(font);// 设置标题的字体颜色chart.setTitle(title);frame.pack();frame.setVisible(true);}}

折线图:

package chart;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.LineAndShapeRenderer;import org.jfree.data.category.DefaultCategoryDataset;public class LineChart {public LineChart() {Map<Integer, Integer> map = initDataSet();showChart(map);}/** * 初始化数据集 * 柱状图的数据集接受三个参数,第一个参数为柱子的高度,第二个为几个类别进行比较,第三个为共有几组数据 *  * @return */private Map<Integer, Integer> initDataSet() {return null;}private void showChart(Map<Integer, Integer> map) {// 创建饼图数据对象DefaultCategoryDataset dataSet = new DefaultCategoryDataset();Set<Entry<Integer, Integer>> set = map.entrySet();for (Entry<Integer, Integer> entry : set) {dataSet.setValue(entry.getValue(), "数量", entry.getKey());}// 如果把createLineChart改为createLineChart3D就变为了3D效果的折线图JFreeChart chart = ChartFactory.createLineChart("每年说说的发布量", "年份", "数目",dataSet, PlotOrientation.VERTICAL, // 绘制方向false, // 显示图例true, // 采用标准生成器false // 是否生成超链接);ChartFrame frame = new ChartFrame("图表标题", chart, true);chart.setBackgroundPaint(Color.WHITE);// 设置背景色// 获取绘图区对象CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(Color.WHITE); // 设置绘图区背景色plot.setRangeGridlinePaint(Color.GRAY); // 设置水平方向背景线颜色plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为trueplot.setDomainGridlinePaint(Color.GRAY); // 设置垂直方向背景线颜色plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为falseCategoryAxis domainAxis = plot.getDomainAxis();domainAxis.setLowerMargin(0.01);// 左边距 边框距离domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。domainAxis.setMaximumCategoryLabelLines(2);domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));ValueAxis rangeAxis = plot.getRangeAxis();rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());// Y轴显示整数rangeAxis.setAutoRangeMinimumSize(1); // 最小跨度rangeAxis.setUpperMargin(0.18);// 上边距,防止最大的一个数据靠近了坐标轴。rangeAxis.setLowerBound(0); // 最小值显示0rangeAxis.setAutoRange(false); // 不自动分配Y轴数据rangeAxis.setTickMarkStroke(new BasicStroke(1.6f)); // 设置坐标标记大小rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色rangeAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));// 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot.getRenderer();lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见lineandshaperenderer.setBaseLinesVisible(true);// 显示折点数据lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());lineandshaperenderer.setBaseItemLabelsVisible(true);frame.pack();frame.setVisible(true);}}

饼图:

package chart;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.StandardPieToolTipGenerator;import org.jfree.chart.plot.PiePlot3D;import org.jfree.chart.title.TextTitle;import org.jfree.data.general.DefaultPieDataset;/** * @author shenchao *饼图 */public class PieChart {public PieChart() {Map<String,Integer> map = initDataSet();showChart(map);}/** * 饼图接受的数据集要求是键值对<br> *  * 可以在这里初始化数据集 * @return map */private Map<String,Integer> initDataSet() {return null;}/** * 显示图表 * @param map */private void showChart(Map<String, Integer> map) {// 创建饼图数据对象DefaultPieDataset dfp = new DefaultPieDataset();Set<Entry<String, Integer>> set = map.entrySet();for (Entry<String, Integer> entry : set) {dfp.setValue(entry.getKey(), entry.getValue());}// createpieChart3D创建3D饼图JFreeChart chart = ChartFactory.createPieChart3D("窗口标题",dfp, true, true, true);// 图片背景色chart.setBackgroundPaint(Color.white);// 设置标题文字ChartFrame frame = new ChartFrame("窗口标题", chart, true);// 取得3D饼图对象PiePlot3D plot = (PiePlot3D) chart.getPlot();// 图形边框颜色plot.setBaseSectionOutlinePaint(Color.WHITE);// 图形边框粗细plot.setBaseSectionOutlineStroke(new BasicStroke(1.0f));// 指定图片的透明度(0.0-1.0)plot.setForegroundAlpha(0.45f);// 指定显示的饼图上圆形(false)还椭圆形(true)plot.setCircular(true);// 设置第一个 饼块section 的开始位置,默认是12点钟方向plot.setStartAngle(360);// 设置鼠标悬停提示plot.setToolTipGenerator(new StandardPieToolTipGenerator());// 设置饼图各部分标签字体plot.setLabelFont(new Font("微软雅黑", Font.ITALIC, 16));// 设置分饼颜色plot.setSectionPaint(0, new Color(244, 194, 144));// 定义字体格式          Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE,20);          TextTitle title = new TextTitle("图表标题");          title.setFont(font);          // 设置字体,非常关键不然会出现乱码的,下方的字体          chart.setTitle(title);          frame.pack();         frame.setVisible(true);}}

散点图:

package chart;import java.awt.BasicStroke;import java.awt.Color;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;import org.jfree.data.xy.DefaultXYDataset;/** * @author shenchao 散点图 */public class ScatterPlotChart {public ScatterPlotChart() {Map<Integer,List<Data>> map = initDataSet();showChart(map);}/** *  这里的Data类是数据的封装,可以安装实际需求实现,map集合的键为类别 */private Map<Integer, List<Data>> initDataSet() {return null;}public void showChart(Map<Integer,List<Data>> map) {DefaultXYDataset xydataset = new DefaultXYDataset();//根据类别建立数据集for (Entry<Integer, List<Data>> entry : map.entrySet()) {List<Data> l = entry.getValue();int size = l.size();//散点图要求数据集为二维数组double[][] datas = new double[2][size];for (int i = 0; i < size; i++) {Data data = l.get(i);datas[0][i] = data.getDistance();datas[1][i] = data.getIcecream();}xydataset.addSeries(entry.getKey(), datas);}JFreeChart chart = ChartFactory.createScatterPlot("散点图", "每年获取的飞行常客里程数", "每周所消费的冰淇淋公升数", xydataset, PlotOrientation.VERTICAL, true, false, false);ChartFrame frame = new ChartFrame("散点图", chart, true);chart.setBackgroundPaint(Color.white);  chart.setBorderPaint(Color.GREEN);  chart.setBorderStroke(new BasicStroke(1.5f));          XYPlot xyplot = (XYPlot) chart.getPlot();            xyplot.setBackgroundPaint(new Color(255, 253, 246));          ValueAxis vaaxis = xyplot.getDomainAxis();          vaaxis.setAxisLineStroke(new BasicStroke(1.5f));            ValueAxis va = xyplot.getDomainAxis(0);          va.setAxisLineStroke(new BasicStroke(1.5f));            va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细          va.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色          xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细          va.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色          va.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色          ValueAxis axis = xyplot.getRangeAxis();          axis.setAxisLineStroke(new BasicStroke(1.5f));            XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot                  .getRenderer();          xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);          xylineandshaperenderer.setUseOutlinePaint(true);          NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();          numberaxis.setAutoRangeIncludesZero(false);          numberaxis.setTickMarkInsideLength(2.0F);          numberaxis.setTickMarkOutsideLength(0.0F);          numberaxis.setAxisLineStroke(new BasicStroke(1.5f));                  frame.pack();frame.setVisible(true);}}

0 0
原创粉丝点击