按时间为jfreechart数据源

public String getDataSet(
@PathVariable int type,
@RequestParam(value = "stratdate", required = false) Date stratdate,
@RequestParam(value = "enddate", required = false) Date enddate,
HttpSession session, HttpServletRequest request, Model model) {
if (stratdate == null) {
model.addAttribute("info", "请输入开始时间");
return "";
}
if (enddate == null) {
model.addAttribute("info", "请输入结束时间");
return "";
}
if (enddate.before(stratdate)) {
model.addAttribute("info", "开始时间应小于结束时间");
return "";
}
Calendar stratCalendar = Calendar.getInstance();
stratCalendar.setTime(stratdate);
int stratYear = stratCalendar.get(Calendar.YEAR);
int stratMonth = stratCalendar.get(Calendar.MONTH) + 1;
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(enddate);
int endYear = endCalendar.get(Calendar.YEAR);
int endMonth = endCalendar.get(Calendar.MONTH) + 1;
Library library = libraryService.get(libraryId);
String filename = "";
if (String.valueOf(stratYear).equals(String.valueOf(endYear))) {
if (String.valueOf(stratMonth).equals(String.valueOf(endMonth))) {
int monthIndex = 31;
if (stratMonth == 2) {
if (stratYear % 4 == 0 && stratYear % 100 != 0
|| stratYear % 400 == 0)
monthIndex = 29;
else
monthIndex = 28;
}
if (stratMonth == 1 || stratMonth == 3 || stratMonth == 5
|| stratMonth == 7 || stratMonth == 8
|| stratMonth == 10 || stratMonth == 12) {
monthIndex = 31;
}
if (stratMonth == 4 || stratMonth == 6 || stratMonth == 9
|| stratMonth == 11) {
monthIndex = 30;
}
double[] totals = new double[monthIndex];
for (int day = 0; day < totals.length; day++) {
int total = visitlogService.sumByTime(stratYear,
stratMonth, day, libraryId);
if (total == 0) {
totals[day] = 0.0;
} else {
totals[day] = total;
}
}
if (type == 1) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int day = 0; day < totals.length; day++) {
dataset.addValue(totals[day], "", new Day(day + 1,
stratMonth, stratYear));
}
}
if (type == 2) {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
for (int day = 0; day < totals.length; day++) {
defaultpiedataset.setValue(new Day(day + 1, stratMonth,
stratYear), new Double(totals[day]));
}
}
if (type == 3) {
TimeSeries chinaTs = new TimeSeries("日访问量");
chinaTs.clear();
for (int day = 0; day < totals.length; day++) {
chinaTs.add(new TimeSeriesDataItem(new Day(day + 1,
stratMonth, stratYear), new Double(totals[day])));
}
}
} else {
double[] totals = new double[12];
for (int mon = 0; mon < 12; mon++) {
int total = visitlogService.sumByTime(stratYear, mon + 1,
libraryId);
if (total == 0) {
totals[mon] = 0.0;
} else {
totals[mon] = total;
}
}
if (type == 1) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int mon = 0; mon < 12; mon++) {
dataset.addValue(totals[mon], "", new Month(mon + 1,
stratYear));
}
filename = vImageBarByLibrary("月",
dataset, session);
}
if (type == 2) {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
for (int mon = 0; mon < 12; mon++) {
defaultpiedataset.setValue(
new Month(mon + 1, stratYear), new Double(
totals[mon]));
}
}
if (type == 3) {
TimeSeries chinaTs = new TimeSeries("月访问量");
chinaTs.clear();
for (int mon = 0; mon < 12; mon++) {
chinaTs.add(new TimeSeriesDataItem(new Month(mon + 1,
stratYear), new Double(totals[mon])));
}
}
}
} else {
double[] totals = new double[endYear - stratYear + 1];
int i = 0;
for (int year = stratYear; year < stratYear + totals.length; year++) {
int total = visitlogService.sumByTime(year, libraryId);
if (total == 0) {
totals[i] = 0.0;
} else {
totals[i] = total;
}
i++;
}
if (type == 1) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int j = 0;
for (int year = stratYear; year < stratYear + totals.length; year++) {
dataset.addValue(totals[j], "", new Year(year));
j++;
}
}
if (type == 2) {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
int j = 0;
for (int year = stratYear; year < stratYear + totals.length; year++) {
defaultpiedataset.setValue(new Year(year), new Double(
totals[j]));
j++;
}
}
if (type == 3) {
TimeSeries chinaTs = new TimeSeries("年访问量");
chinaTs.clear();
int j = 0;
for (int year = stratYear; year < stratYear + totals.length; year++) {
chinaTs.add(new TimeSeriesDataItem(new Year(year),
new Double(totals[j])));
j++;
}
}
}
return "/visitlog/showimage";
}

若开始时间和结束时间年不相同时,生成的图表以年为单位; 若相同,月份不相同时,生成的图表以月为单位; 若月份相同,生成的图表以日为单位;

你可能感兴趣的:(jfreechart,J#)