JFreeChart在制作折线图的时候可以使用两种不同的方式

package Line;

import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset; public class Line {
public static void main(String[] args) {
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
CategoryDataset mDataset = GetDataset();
JFreeChart mChart = ChartFactory.createLineChart(
"折线图",//图名字
"年份",//横坐标
"数量",//纵坐标
mDataset,//数据集
PlotOrientation.VERTICAL,
true, // 显示图例
true, // 采用标准生成器 
false);// 是否生成超链接 CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部横虚线
mPlot.setOutlinePaint(Color.RED);//边界线 ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
mChartFrame.pack();
mChartFrame.setVisible(true); }
public static CategoryDataset GetDataset()
{
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
mDataset.addValue(1, "First", "2013");
mDataset.addValue(3, "First", "2014");
mDataset.addValue(2, "First", "2015");
mDataset.addValue(6, "First", "2016");
mDataset.addValue(5, "First", "2017");
mDataset.addValue(12, "First", "2018");
mDataset.addValue(14, "Second", "2013");
mDataset.addValue(13, "Second", "2014");
mDataset.addValue(12, "Second", "2015");
mDataset.addValue(9, "Second", "2016");
mDataset.addValue(5, "Second", "2017");
mDataset.addValue(7, "Second", "2018");
return mDataset;
} }

第二种方式:

package Line;

import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection; public class XYLine {
public static void main(String[] args) {
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
XYSeriesCollection mCollection = GetCollection();
JFreeChart mChart = ChartFactory.createXYLineChart(
"折线图",
"X",
"Y",
mCollection,
PlotOrientation.VERTICAL,
true,
true,
false);
ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
mChartFrame.pack();
mChartFrame.setVisible(true); }
public static XYSeriesCollection GetCollection()
{
XYSeriesCollection mCollection = new XYSeriesCollection();
XYSeries mSeriesFirst = new XYSeries("First");
mSeriesFirst.add(1.0D, 1.0D);
mSeriesFirst.add(2D, 4D);
mSeriesFirst.add(3D, 3D);
mSeriesFirst.add(4D, 5D);
mSeriesFirst.add(5D, 5D);
mSeriesFirst.add(6D, 7D);
mSeriesFirst.add(7D, 7D);
mSeriesFirst.add(8D, 8D);
XYSeries mSeriesSecond = new XYSeries("Second");
mSeriesSecond.add(1.0D, 5D);
mSeriesSecond.add(2D, 7D);
mSeriesSecond.add(3D, 6D);
mSeriesSecond.add(4D, 8D);
mSeriesSecond.add(5D, 4D);
mSeriesSecond.add(6D, 4D);
mSeriesSecond.add(7D, 2D);
mSeriesSecond.add(8D, 1.0D);
mCollection.addSeries(mSeriesFirst);
mCollection.addSeries(mSeriesSecond);
return mCollection;
} }

  1. String sql = "select count(id) num, DATE_FORMAT(calltime, '%Y年%m月') ym,modulename mn from  tongji t group by DATE_FORMAT(calltime, '%Y年%m月'),mn";
  2. List list = getList(sql);
  3. // 绘图数据集
  4. DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
  5. for (Object obj : list) {
  6. Map<String, Object> map = (Map) obj;
  7. dataSet.setValue((Long) map.get("num"), (String) map.get("mn"), map.get("ym").toString());
  8. }
  9. //如果把createLineChart改为createLineChart3D就变为了3D效果的折线图
  10. JFreeChart  chart = ChartFactory.createLineChart("图表标题", "X轴标题", "Y轴标题", dataSet,
  11. PlotOrientation.VERTICAL, // 绘制方向
  12. true, // 显示图例
  13. true, // 采用标准生成器
  14. false // 是否生成超链接
  15. );
  16. chart.getTitle().setFont(titleFont); // 设置标题字体
  17. chart.getLegend().setItemFont(font);// 设置图例类别字体
  18. chart.setBackgroundPaint(bgColor);// 设置背景色
  19. //获取绘图区对象
  20. CategoryPlot plot = chart.getCategoryPlot();
  21. plot.setBackgroundPaint(Color.LIGHT_GRAY); // 设置绘图区背景色
  22. plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色
  23. plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
  24. plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色
  25. plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false
  26. CategoryAxis domainAxis = plot.getDomainAxis();
  27. domainAxis.setLabelFont(font); // 设置横轴字体
  28. domainAxis.setTickLabelFont(font);// 设置坐标轴标尺值字体
  29. domainAxis.setLowerMargin(0.01);// 左边距 边框距离
  30. domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。
  31. domainAxis.setMaximumCategoryLabelLines(2);
  32. ValueAxis rangeAxis = plot.getRangeAxis();
  33. rangeAxis.setLabelFont(font);
  34. rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数
  35. rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度
  36. rangeAxis.setUpperMargin(0.18);//上边距,防止最大的一个数据靠近了坐标轴。
  37. rangeAxis.setLowerBound(0);   //最小值显示0
  38. rangeAxis.setAutoRange(false);   //不自动分配Y轴数据
  39. rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));     // 设置坐标标记大小
  40. rangeAxis.setTickMarkPaint(Color.BLACK);     // 设置坐标标记颜色
  41. // 获取折线对象
  42. LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
  43. BasicStroke realLine = new BasicStroke(1.8f); // 设置实线
  44. // 设置虚线
  45. float dashes[] = { 5.0f };
  46. BasicStroke brokenLine = new BasicStroke(2.2f, // 线条粗细
  47. BasicStroke.CAP_ROUND, // 端点风格
  48. BasicStroke.JOIN_ROUND, // 折点风格
  49. 8f, dashes, 0.6f);
  50. for (int i = 0; i < dataSet.getRowCount(); i++) {
  51. if (i % 2 == 0)
  52. renderer.setSeriesStroke(i, realLine); // 利用实线绘制
  53. else
  54. renderer.setSeriesStroke(i, brokenLine); // 利用虚线绘制
  55. }
  56. plot.setNoDataMessage("无对应的数据,请重新查询。");
  57. plot.setNoDataMessageFont(titleFont);//字体的大小
  58. plot.setNoDataMessagePaint(Color.RED);//字体颜色

平面折线图效果

3D折线图效果:

设置线条、数据点颜色和显示属性

CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.WHITE);// 设置绘图区背景色
mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部横虚线
mPlot.setOutlinePaint(Color.RED);//边界线
mPlot.setDomainGridlinePaint(Color.BLUE); // 设置垂直方向背景线颜色
mPlot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false

LineAndShapeRenderer lasp = (LineAndShapeRenderer) mPlot.getRenderer();// 获取显示线条的对象
lasp.setBaseShapesVisible(true);// 设置拐点是否可见/是否显示拐点
lasp.setDrawOutlines(true);// 设置拐点不同用不同的形状
lasp.setUseFillPaint(true);// 设置线条是否被显示填充颜色
lasp.setBaseFillPaint(Color.BLACK);//// 设置拐点颜色
//lasp.setSeriesPaint(series, paint);

JFreeChart在制作折线图的更多相关文章

  1. JFreeChart应用实例-折线图

    http://www.tuicool.com/articles/Nr2Yna JFreeChart在制作折线图的时候可以使用两种不同的方式 package Line; import java.awt. ...

  2. Microsoft Excel Sheet/表格 制作折线图

    Microsoft Excel Sheet/表格 制作折线图 虽然比较简单,但是仍然需要稍微花一点功夫. 1.制作好表格数据 2.先将数据选定(不包括 横座标的 年月日或其他的刻度 的那一列) 3.插 ...

  3. struts2整合JFreechart 饼图、折线图、柱形图

    struts2整合JFreechart 饼图.折线图.柱形图 上效果图: 当然可以将数据导出图片格式存储.具体下的链接里的文件有保存成图片的操作. 因为是strust2整合JFreechart,所以s ...

  4. JFreeChart绘制XY折线图(工具类设计)

    准备用Java写通信的仿真平台作为毕业设计,相比matlab绘图,Java绘图需要自己去写很多工具类,博主在这采用了JFreeChart的开源解决方案,摸索着自己写了一个XY折线图工具类,话不多说贴源 ...

  5. Python制作折线图

    利用python的第三方包Pygal制作简单的折线图. 申明:本文仅供学习交流使用.源码大部分来自<python编程从入门到实践>:如有侵权,请联系我删除. 1 #!usr/bin/env ...

  6. excel制作折线图太麻烦?试试这些折线图在线生成工具

    折线图是以折线的上升或下降来表示统计数量的增减变化的统计图,叫作折线统计图.用折线的起伏表示数据的增减变化情况,不仅可以表示数量的多少,而且可以反映数据的增减变化情况.并且折线图也是目前最方便的一种统 ...

  7. Vue整合d3.v5.js制作--折线图(line)

    先上效果图(x轴固定为时间轴): 图中出现的悬浮框是鼠标悬停效果 1.环境说明: vue版本:"vue": "^2.5.2" d3版本:"d3&quo ...

  8. chart.js制作折线图

    <!DOCTYPE html> <html> <head> <title></title> </head> <script ...

  9. JFreeChart 之折线图

    JFreeChart 之折线图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets ...

随机推荐

  1. Python 汉字简体和繁体的相互转换

    其实利用python实现汉字的简体和繁体相互转早有人做过,并发布到github上了,地址:https://github.com/skydark/nstools/tree/master/zhtools ...

  2. Linux GDB调试全面解析

    GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具,GDB主要可帮助工程师完成下面4个方面的功能: 启动程序,可以按照工程师自定义的要求随心所欲的运行程序. 让被调试的程序在工程师指定的断 ...

  3. Java 实现导出excel表 POI

    1.首先下载poi-3.6-20091214.jar 2.Student.java import java.util.Date; public class Student { private int ...

  4. dom classList

    才发现dom对象就有classList属性,通过它可以判断该dom是否有指定的class名存在. var tar = e.target; var classList = tar.classList; ...

  5. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  6. Asset Store

    Asset Store 是用来供Unity开发者将自己的插件拿出来供大家有偿使用的一个平台. 报错信息: failed to import package with error cannot impo ...

  7. qt 打开串口 UI卡死

    imx6在qt中打开调试串口时,ui总是会卡死.调试串口已经被文件系统占用,而在qt的app中使用open函数却能够调用open函数,打开成功,造成ui卡死,并且调试串口也卡死.本文记录这个问题的解决 ...

  8. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  9. 复合事件ready,hover,toggle

    1.ready 2.hover 3.toggle(fn1,fn2, …)(被废弃) 2.hover(fn(){……},fn(){……}) 特别强调一点,hover的是mouseenter和mousel ...

  10. nohup和&的区别

    nohup和&的区别http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=21288388 ...