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项目折线图(支持多根折线)
* @param data
* 基础数据。List<String[]> list节点中保存的是长度为3的数据【数字,单个线的名称(可以为多个,如果为多个就是多个线的统计,为一个时设为""即可),X轴单个节点名称】<br/>
* */
public Map<String, String> makeWebLine(List<String[]> 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<String, String> m1 = new HashMap<String, String>();
m1.put("fileName", filename);
m1.put("fileUrl", graphURL);
return m1;
}
/** 普通折线图 */
public boolean makeLine(List<String[]> 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<String[]> 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<String[]> list = new ArrayList<String[]>();
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包导一下,就可以跑起来了
你自己试试
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项目折线图(支持多根折线)
* @param data
* 基础数据。List<String[]> list节点中保存的是长度为3的数据【数字,单个线的名称(可以为多个,如果为多个就是多个线的统计,为一个时设为""即可),X轴单个节点名称】<br/>
* */
public Map<String, String> makeWebLine(List<String[]> 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<String, String> m1 = new HashMap<String, String>();
m1.put("fileName", filename);
m1.put("fileUrl", graphURL);
return m1;
}
/** 普通折线图 */
public boolean makeLine(List<String[]> 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<String[]> 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<String[]> list = new ArrayList<String[]>();
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包导一下,就可以跑起来了
你自己试试
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询