1

我在 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但我想手动设置该值。

可能吗?如何?

4

1 回答 1

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

我希望有用

于 2012-05-23T12:51:29.400 回答