如何用jfreechart作曲线图。没有JSP文件的情况下。

不用其他容器。GUIchartpanel之类的。就是只是在页面中显示一个img。网上的例子基本都是用JSP来取值的。而我现在只能写在JAVA类里面。求高手帮忙解决。... 不用其他容器。GUI chartpanel 之类的。就是只是在页面中显示一个img。网上的例子基本都是用JSP来取值的。而我现在只能写在JAVA类里面。求高手帮忙解决。 展开
 我来答
rogue1988
2011-05-31 · 超过55用户采纳过TA的回答
知道小有建树答主
回答量:332
采纳率:0%
帮助的人:121万
展开全部
下面是 我自己的一个项目中 jfreechart结合Struts2 的例子 你自己看看吧
就是只是在页面中显示一个img 三种图都有
public class AssChartAction extends ActionSupport{
private static final long serialVersionUID = 3944557819068533081L;
private String[] word;
private String type;
private String style;
private String title;
private String xname;
private String yname;
private String[] assessids;

public String[] getWord() {
return word;
}

public void setWord(String[] word) {
this.word = word;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getStyle() {
return style;
}

public void setStyle(String style) {
this.style = style;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getXname() {
return xname;
}

public void setXname(String xname) {
this.xname = xname;
}

public String getYname() {
return yname;
}

public void setYname(String yname) {
this.yname = yname;
}

public String[] getAssessids() {
return assessids;
}

public void setAssessids(String[] assessids) {
this.assessids = assessids;
}

public String saveid() {
System.out.println(assessids[0]);
System.out.println(assessids.length);
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("assessids", assessids);
return SUCCESS;
}

private JFreeChart chart;

public JFreeChart getChart() {
return chart;
}

public void setChart(JFreeChart chart) {
this.chart = chart;
}

public String chartDB() {
if (style.equals("饼图")) {
drawPieChart();
} else if (style.equals("垂直柱状统计图")) {
drawBarChart();
} else if (style.equals("折线统计图")) {
drawLineChart();
}

return SUCCESS;
}

private void drawLineChart() {
Map<String, Object> session = ActionContext.getContext().getSession();
assessids = (String[]) session.get("assessids");
String[] field = new String[word.length]; // 获得要查询的字段
String[] rowKeys = new String[word.length]; // 各字段的中文名称

for (int i = 0; i < word.length; i++) {
field[i] = word[i].split("-")[0];
rowKeys[i] = word[i].split("-")[1];
}
StringBuffer sb = new StringBuffer("SELECT ");
for (int j = 0; j < field.length; j++) {
sb.append( field[j] + ", ");
}
sb
.append("year from assessrecord where id in "
+ Util.typesToString(assessids)
+ " group by year");
List<Object[]> list = new SearchService().findBySql(sb.toString());
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
String[] category = new String[list.size()];
for (int k = 0; k < field.length; k++) {
for (int j = 0; j < list.size(); j++) {
if(list.get(j)[field.length]==null){
category[j] = "";
}else{
category[j] = list.get(j)[field.length].toString();
}
if(list.get(j)[k] ==null){
dataSet.addValue(new Double(0), rowKeys[k], category[j]);
}else{
dataSet.addValue(new Double(list.get(j)[k].toString()),
rowKeys[k], category[j]);
}
}
}

chart = ChartFactory.createLineChart(title, xname, yname, dataSet,
PlotOrientation.VERTICAL, true, true, false);
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

}

private void drawBarChart() {
Map<String, Object> session = ActionContext.getContext().getSession();
assessids = (String[]) session.get("assessids");
String[] field = new String[word.length]; // 获得要查询的字段
String[] rowKeys = new String[word.length]; // 各字段的中文名称
for (int i = 0; i < word.length; i++) {
field[i] = word[i].split("-")[0];
rowKeys[i] = word[i].split("-")[1];
}
StringBuffer sb = new StringBuffer("SELECT ");

for (int j = 0; j < field.length; j++) {
sb.append( field[j] + ", ");

}
sb
.append("year from assessrecord where id in "
+ Util.typesToString(assessids)
+ " group by year");

List<Object[]> list = new SearchService().findBySql(sb.toString());
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
String[] category = new String[list.size()];
for (int k = 0; k < field.length; k++) {
for (int j = 0; j < list.size(); j++) {
if(list.get(j)[field.length]==null){
category[j] = "";
}else{
category[j] = list.get(j)[field.length].toString();
}
if(list.get(j)[k] ==null){
dataSet.addValue(new Double(0), rowKeys[k], category[j]);
}else{
dataSet.addValue(new Double(list.get(j)[k].toString()),
rowKeys[k], category[j]);
}
}
}
chart = ChartFactory.createBarChart3D(title, xname, yname, dataSet,
PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象
CategoryAxis domainAxis = plot.getDomainAxis();
// 水平底部列表
domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 14));
// 水平底部标题
domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));
// 垂直标题
ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.BOLD, 15));
BarRenderer3D renderer = new BarRenderer3D();

renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));

renderer.setItemLabelAnchorOffset(10D);

renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));

renderer.setItemLabelsVisible(true);

//设置每个地区所包含的平行柱的之间距离

//renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));

}

public void drawPieChart() {
Map<String, Object> session = ActionContext.getContext().getSession();
assessids = (String[]) session.get("assessids");
String sql = "";
String field[] = word[0].split("-");

sql = "SELECT "
+ field[0]
+ ", year from assessrecord where id in "
+ Util.typesToString(assessids)
+ " group by year";

List<Object[]> list = new SearchService().findBySql(sql);

DefaultPieDataset data = new DefaultPieDataset();
for (Object[] o : list) {
if(o[0] == null){
data.setValue(o[1].toString(), new Double(0));
}
data.setValue(o[1].toString(), new Double(o[0].toString()));
}
chart = ChartFactory.createPieChart(title, data, true, true, false);
Font font = new Font("SimSun", 10, 20);
TextTitle txtTitle = null;
txtTitle = chart.getTitle();
txtTitle.setFont(font);
PiePlot pieplot = (PiePlot)chart.getPlot();
pieplot.setLabelFont(font);
chart.getLegend().setItemFont(font);
}

}
百度网友b0a6438
2011-05-31 · TA获得超过599个赞
知道小有建树答主
回答量:303
采纳率:0%
帮助的人:119万
展开全部
在网上找下java写的画图的小demo,剩下的就是jfreechart的使用技巧了,碰到问题自己多查资料,尽量少依靠别人,参加工作可没老师教你了。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lws0888
2011-06-03
知道答主
回答量:7
采纳率:0%
帮助的人:0
展开全部
通过servlet实现,其实可以生成一张图片,想怎么用都可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式