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;  
    }  
  
}  




本文为xyw_Eliot原创,转载请注明出处:http://blog.youkuaiyun.com/xyw_eliot/article/details/8692643



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值