jfreechart 折线图


版本:jfreechart.1.0.13


jfreechart 折线图



package demo;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.text.SimpleDateFormat;

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.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
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.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

/**
 * A line chart demo showing the use of a custom drawing supplier.
 *
 */
public class LineChartDemo22 extends ApplicationFrame
{

    private static final long serialVersionUID = -7736483874493003214L;

    /**
     * Creates a new demo.
     *
     * @param title the frame title.
     */
    public LineChartDemo22(final String title)
    {
        super(title);
        
        TimeSeries timeSeries = new TimeSeries("", Month.class);
        
        //时间曲线数据集合
        TimeSeriesCollection lineDataset = new TimeSeriesCollection();
        
        //构造数据集合       
        timeSeries.add(new Month(1, 2007), 11200);
        timeSeries.add(new Month(2, 2007), 9000);
        timeSeries.add(new Month(3, 2007), 6200);
        timeSeries.add(new Month(4, 2007), 8200);
        timeSeries.add(new Month(5, 2007), 8200);
        timeSeries.add(new Month(6, 2007), 12200);
        timeSeries.add(new Month(7, 2007), 13200);
        timeSeries.add(new Month(8, 2007), 8300);
        timeSeries.add(new Month(9, 2007), 12400);
        timeSeries.add(new Month(10, 2007), 12500);
        timeSeries.add(new Month(11, 2007), 13600);
        timeSeries.add(new Month(12, 2007), 12500);
        lineDataset.addSeries(timeSeries);
        
        
        
        
        Font font = new Font("微软雅黑", Font.PLAIN, 12);  
        
        Font fontTitle = new Font("微软雅黑", Font.BOLD, 14);
        
        
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                                            "注册会员统计", 
                                            "( 月份 )", 
                                            "数量",  
                                            lineDataset, 
                                            false, 
                                            false, 
                                            false);
        
        
        chart.setTitle(new TextTitle(chart.getTitle().getText(), fontTitle));//设置标题字体样式
        
        /*//设置主标题
        chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15)));
        chart.setAntiAlias(true);
        
         //设置子标题
        TextTitle subtitle = new TextTitle("2007年度", new Font("黑体", Font.BOLD, 12));
        chart.addSubtitle(subtitle);
        */

        
        
        
        
        //=====================================================================================
        XYPlot plot = (XYPlot) chart.getPlot();        
       // plot.setBackgroundPaint(Color.white);//设置网格背景颜色        
       // plot.setDomainGridlinePaint(Color.pink);//设置网格竖线颜色       
       // plot.setRangeGridlinePaint(Color.pink); //设置网格横线颜色       
       // plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D)); //设置曲线图与xy轴的距离       
        
        
        
        //=====================================================================================
        ValueAxis  valueAxisY = plot.getRangeAxis();
        valueAxisY.setLabelFont(font);  //设置Y轴上标志符字体
        
        
        //=====================================================================================
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
        xylineandshaperenderer.setBaseShapesVisible(true); //设置曲线是否显示转接点 
        
        
        XYItemRenderer xyitem = plot.getRenderer();         //设置曲线显示各数据点的值
        xyitem.setBaseItemLabelsVisible(true);    
        xyitem.setBasePositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
        xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
        xyitem.setBaseItemLabelFont(new Font("Arial", Font.PLAIN, 10)); 
        
        xyitem.setSeriesPaint(0, new Color(0,128,128));     //设置折线颜色
        
        
        
        
        //=====================================================================================
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("M月")); //设置X轴上日期样式         
        
        ValueAxis  valueAxisX = plot.getDomainAxis();        
        valueAxisX.setLabelFont(font);    //设置X轴的标识符字体
        valueAxisX.setTickLabelFont(font);//设置X轴上的字体
        valueAxisX.setUpperMargin(0.05);  //设置右边距
        
        
        
        //=====================================================================================        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(600, 300));
        setContentPane(chartPanel);
        
        

    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args ignored.
     */
    public static void main(final String[] args){

        final LineChartDemo22 demo = new LineChartDemo22("Line Chart Demo 5");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}











你可能感兴趣的:(jfreechart,折线图)