使用jfreechart的折线图

时间:2012-05-23 12:15:25

标签: java servlets jfreechart

我在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上添加的值自动创建水平数字但是我想手动设置该值。

有可能吗?怎么样?

1 个答案:

答案 0 :(得分:-1)

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

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());

我希望有用