欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jfreechart之折线图

程序员文章站 2022-05-27 13:33:12
...
package test;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class LineCharts extends ApplicationFrame {

public LineCharts(String s) {
super(s);
setContentPane(createDemoLine());
}

public static void main(String[] args) {
LineCharts fjc = new LineCharts("折线图");
fjc.pack();
RefineryUtilities.centerFrameOnScreen(fjc);
fjc.setVisible(true);

}

// 生成显示图表的面板
public static JPanel createDemoLine() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}

// 生成图表主对象JFreeChart
public static JFreeChart createChart(DefaultCategoryDataset linedataset) {
//定义图表对象
JFreeChart chart = ChartFactory.createLineChart("折线图", // chart title
    "时间", // domain axis label
    "销售额(百万)", // range axis label
    linedataset, // data
    PlotOrientation.VERTICAL, // orientation
    true, // include legend
    true, // tooltips
    false // urls
    );
CategoryPlot plot = chart.getCategoryPlot();
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);

return chart;
}

//生成数据
public static DefaultCategoryDataset createDataset() {
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
// 各曲线名称
String series1 = "冰箱";
String series2 = "彩电";
String series3 = "洗衣机";

//    横轴名称(列名称)
String type1 = "1月";
String type2 = "2月";
String type3 = "3月";

linedataset.addValue(0.0, series1, type1);
linedataset.addValue(4.2, series1, type2);
linedataset.addValue(3.9, series1, type3);

linedataset.addValue(1.0, series2, type1);
linedataset.addValue(5.2, series2, type2);
linedataset.addValue(7.9, series2, type3);

linedataset.addValue(2.0, series3, type1);
linedataset.addValue(9.2, series3, type2);
linedataset.addValue(8.9, series3, type3);

return linedataset;
}

}

package com.iman.nrms.nrmwns.wrm.analyse.domain.util;   
  
import java.awt.Color;   
import java.awt.Font;   
import java.text.DecimalFormat;   
  
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.renderer.category.BarRenderer;   
  
/**  
 * 格式化 JFreeChart 输出图片使用  
 *   
 *     
 * Author :   
 * Date   : Nov 26, 2009  
 * Time   : 11:50:41 AM   
 * Version: 1.0  
 */  
public class FormatPic {   
    /**  
     * 格式化折线图使用  
     *   
     * @param chart  
     * @returnType: void  
     * @author:   
     * @data: Nov 26, 2009  
     * @time: 11:51:26 AM  
     */  
    public static void setView(JFreeChart chart){   
                 chart.setTextAntiAlias(false);    
                   chart.setBackgroundPaint(Color.WHITE);    
                   CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();    
                   // x轴 // 分类轴网格是否可见    
                   categoryplot.setDomainGridlinesVisible(true);    
                   // y轴 //数据轴网格是否可见    
                   categoryplot.setRangeGridlinesVisible(true);    
                      
                   this.configFont(chart);    
                   categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩    
                   categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩    
                   categoryplot.setBackgroundPaint(Color.lightGray);    
                   // 设置轴和面板之间的距离    
                   // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));    
                   CategoryAxis domainAxis = categoryplot.getDomainAxis();    
                   domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的    
                   // Lable    
                   // 45度倾斜    
                   // 设置距离图片左端距离    
                   domainAxis.setLowerMargin(0.0);    
                   // 设置距离图片右端距离    
                   domainAxis.setUpperMargin(0.0);    
                   NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();    
                   numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());    
                   numberaxis.setAutoRangeIncludesZero(true);    
                   // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!    
                   LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot    
                     .getRenderer();    
                   lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见    
                   lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见    
                   // 显示折点数据    
                   // lineandshaperenderer.setBaseItemLabelGenerator(new    
                   // StandardCategoryItemLabelGenerator());    
                   // lineandshaperenderer.setBaseItemLabelsVisible(true);    
    }   
}