在dateaxis中设置小时范围 - XYbarchart -jfreechart

时间:2015-10-24 11:46:18

标签: java jfreechart

我使用下面的代码创建XYbarchart。我需要绘制两个或两个多小时的栏,但我的代码一天只绘制一个小时。有没有办法做到这一点?

private void createXYBarChart(OutputStream out, Object data)
throws Exception {

ChartRenderingInfo info = new ChartRenderingInfo();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] ptsImageBytes = null;
final IntervalXYDataset intvlXYDataset = createIntervalXYDataset();

if (intvlXYDataset != null) {

final JFreeChart chart = ChartFactory.createXYBarChart("Time on Hold","Arrival Time (Hrs)", true,"Hold Time (Mins)", intvlXYDataset,PlotOrientation.VERTICAL, true, true, false);

DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();   
axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1));
axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));

if(intvlXYDataset.getSeriesCount()>8){
axis.setVerticalTickLabels(true);
}   

XYBarRenderer renderer = (XYBarRenderer) chart.getXYPlot().getRenderer();
renderer.setBarPainter(new StandardXYBarPainter());
renderer.setDrawBarOutline(false);  


BufferedImage pageImage = chart.createBufferedImage(CHART_WIDTH,CHART_HEIGHT, BufferedImage.TYPE_INT_BGR, info);
ImageIO.write(pageImage, IMAGE_TYPE, baos);
baos.flush();
if (null != baos) {
ptsImageBytes = baos.toByteArray();
}

ImageIO.write(pageImage, IMAGE_TYPE, out);

}

}

private IntervalXYDataset createIntervalXYDataset() {

TimeSeriesCollection result=new TimeSeriesCollection();
TimeSeries s1=new TimeSeries("Daily Holding Time"); 

if (holdTimeRptList != null && holdTimeRptList.size() > 0
&& graphList != null && graphList.size() > 0) {

for (String graph : graphList) {
s1 = new TimeSeries(graph);
for (reportDto reportDto : holdTimeRptList) {
if (reportDto != null && reportDto.getHoldTime() != 0 && reportDto.getGraphTime() != null && reportDto.getGraphTime().equalsIgnoreCase(graph)) {    

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");  
Hour mint=new Hour(formatter.parse(reportDto.getGraphHoldEntry())); 
s1.addOrUpdate(mint, Integer.valueOf(reportDto.getHoldTime())); 

}
}
result.addSeries(s1);
}   
}   

return result;
}

例如,如果我将日期设定为24/10/2015 13:00:00,则时间范围为13:00:00至14:00:00,但我需要设定时间范围13:00:00到16:00:00有可能吗?

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。我已将TimeSeriesCollection,TimeSeries更改为TimePeriodValuesCollection,TimePeriodValues。

private IntervalXYDataset createIntervalXYDataset()  {


        TimePeriodValuesCollection result = new TimePeriodValuesCollection();
        TimePeriodValues s1 = new TimePeriodValues("Daily Holding Time");

        if (holdTimeRptList != null && holdTimeRptList.size() > 0
                && graphList != null && graphList.size() > 0) {

            for (String graph : graphList) {
                s1 = new TimePeriodValues(graph);
                for (reportDto reportDto : holdTimeRptList) {
                    if (reportDto != null
                            && reportDto.getHoldTime() != 0
                            && reportDto.getGraphTime() != null
                            && reportDto.getGraphTime().equalsIgnoreCase(
                                    graph)) {

                        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                        Date dt=formatter.parse(reportDto.getHoldEntry());
                        Date dt1=DateUtils.addHours(dt, 5);

                        s1.add(new SimpleTimePeriod(dt, dt1), Integer.valueOf(reportDto.getHoldTime()));

                    }
                }
                result.addSeries(s1);
            }                   
        }

        return result;
    }