gpt4 book ai didi

java - 使用 jfreechart 绘制折线图

转载 作者:行者123 更新时间:2023-12-01 06:20:11 25 4
gpt4 key购买 nike

我在 jsp 和 servlet 中使用 jfreeChart API 创建了折线图。

以下是servlet中的代码

// Get the output stream from the response object:
OutputStream out = response.getOutputStream();

// Set the HTTP Response Type:
response.setContentType("image/png");

XYSeries series = new XYSeries("Average Weight1233");
series.add(1.48, 20.0);
series.add(1.52, 25.0);
series.add(2.02, 50.0);
series.add(2.05, 65.0);
series.add(3.30, 2);
series.add(3.52, 50);
XYDataset xyDataset = new XYSeriesCollection(series);

// Create chart:
JFreeChart chart
= ChartFactory.createXYLineChart("XYLine Chart using JFreeChart",
"Age", "Weight",xyDataset,
PlotOrientation.VERTICAL,
true, true, false);

// Write the data to the output stream:
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);

它会根据我在 XYSeries 上添加的值自动创建水平数字,但我想手动设置该值。

这可能吗?怎么办?

最佳答案

我不确定,但我有一个例子。有一天,我需要根据时间创建水平数字。然后我创建了这个类:

public class TimeAxis {

public static TickUnitSource createTimeTickUnits() {
TickUnits units = new TickUnits();
TimeFormat format = new TimeFormat();
units.add(new NumberTickUnit(1, format, 2));
units.add(new NumberTickUnit(2, format, 2));
units.add(new NumberTickUnit(5, format, 5));
units.add(new NumberTickUnit(10, format, 2));
units.add(new NumberTickUnit(20, format, 2));
units.add(new NumberTickUnit(60, format, 5));
units.add(new NumberTickUnit(120, format, 2));
units.add(new NumberTickUnit(240, format, 2));
units.add(new NumberTickUnit(540, format, 5));
units.add(new NumberTickUnit(1080, format, 2));
units.add(new NumberTickUnit(2160, format, 2));
units.add(new NumberTickUnit(5400, format, 5));
units.add(new NumberTickUnit(10800, format, 2));
units.add(new NumberTickUnit(21600, format, 2));
units.add(new NumberTickUnit(54000, format, 5));
units.add(new NumberTickUnit(108000, format, 2));
units.add(new NumberTickUnit(216000, format, 2));
units.add(new NumberTickUnit(540000, format, 5));
units.add(new NumberTickUnit(1080000, format, 2));
units.add(new NumberTickUnit(2160000, format, 2));
units.add(new NumberTickUnit(5400000, format, 5));
units.add(new NumberTickUnit(10800000, format, 2));
units.add(new NumberTickUnit(21600000, format, 2));
units.add(new NumberTickUnit(54000000, format, 5));
units.add(new NumberTickUnit(108000000, format, 2));
units.add(new NumberTickUnit(216000000, format, 2));
units.add(new NumberTickUnit(540000000, format, 5));
units.add(new NumberTickUnit(1080000000, format, 2));
units.add(new NumberTickUnit(2160000000.0, format, 2));
units.add(new NumberTickUnit(5400000000.0, format, 5));
units.add(new NumberTickUnit(10800000000.0, format, 2));
return units;
}
}

然后,我创建了一个类来操作“TimeFormat”:

public class TimeFormat extends NumberFormat {

@Override
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
StringBuffer myReturn = new StringBuffer();
//here I have the source to transform time in seconds in that format: 12h25m35s
return myReturn;
}

@Override
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
return new StringBuffer();
}

@Override
public Number parse(String source, ParsePosition parsePosition) {
return 0;
}
}

最后,我设置了这个属性:

ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setStandardTickUnits(TimeAxis.createTimeTickUnits()); //Here sets the horizontal numbers

设置垂直数字:

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(TimeAxis.createTimeTickUnits());

希望对您有用

关于java - 使用 jfreechart 绘制折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10719703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com