jfreechart做折线图有没有简单的方法控制
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 06:02:47
jfreechart做折线图有没有简单的方法控制
jfreechart做折线图有没有简单的方法控制
jfreechart做折线图有没有简单的方法控制
package daimon.jfreechart;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
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.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
/** 折线图
* @author 顾源 */
public class LineDraw {
/** web项目折线图(支持多根折线)
* @paramdata
* 基础数据.Listlist节点中保存的是长度为3的数据【数字,单个线的名称(可以为多个,如果为多个就是多个线的统计,为一个时设为""即可),X轴单个节点名称】
* */
public Map makeWebLine(List data, String lineName, String xName, String yName ,int width ,int height ,HttpServletRequest request) {
CategoryDataset linedataset = this.getDataSet(data);
JFreeChart chart = ChartFactory.createLineChart(lineName, // chart title
xName, // domain axis label
yName, // range axis label
linedataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
CategoryPlot line = chart.getCategoryPlot();
// 坐标轴的设置
NumberAxis rangeAxis = (NumberAxis) line.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
line.setRangeAxis(rangeAxis);
String filename = "";
try {
filename = ServletUtilities.saveChartAsPNG(chart, width, height, null, request.getSession());
} catch (IOException e) {
e.printStackTrace();
}
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
Map m1 = new HashMap();
m1.put("fileName", filename);
m1.put("fileUrl", graphURL);
return m1;
}
/** 普通折线图 */
public boolean makeLine(List data, String lineName, String xName, String yName ,int width ,int height) {
CategoryDataset linedataset = this.getDataSet(data);
JFreeChart chart = ChartFactory.createLineChart(lineName, // chart title
xName, // domain axis label
yName, // range axis label
linedataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
CategoryPlot line = chart.getCategoryPlot();
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) line.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
line.setRangeAxis(rangeAxis);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
//第二个参数是设置图片清晰度,从0.1f到1.0f
ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, width, height, null);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
fos_jpg.close();
} catch (Exception e) {
}
}
System.out.println("结束");
return true;
}
//构建数据
private CategoryDataset getDataSet(List data){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(String[] ss : data){
dataset.addValue(Double.valueOf(ss[0]), ss[1], ss[2]);
}
return dataset;
}
//主方法测试
public static void main(String[] args){
LineDraw ld = new LineDraw();
List list = new ArrayList();
String[] s1 = {"10" ,"北京" ,"2010"};
String[] s2 = {"15" ,"北京" ,"2011"};
String[] s3 = {"20" ,"北京" ,"2012"};
String[] s4 = {"13" ,"北京" ,"2013"};
String[] s5 = {"17" ,"北京" ,"2014"};
String[] s6 = {"50" ,"北京" ,"2015"};
String[] z1 = {"25" ,"上海" ,"2010"};
String[] z2 = {"30" ,"上海" ,"2011"};
String[] z3 = {"5" ,"上海" ,"2012"};
//String[] z4 = {"60" ,"上海" ,"2013"};
String[] z5 = {"30" ,"上海" ,"2014"};
String[] z6 = {"0" ,"上海" ,"2015"};
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
list.add(z1);
list.add(z2);
list.add(z3);
//list.add(z4);
list.add(z5);
list.add(z6);
ld.makeLine(list, "测试图", "年份", "吨", 600, 500);
}
}
自己把jfreechart包导一下,就可以跑起来了
你自己试试