论坛首页 入门技术论坛

JfreeChart 3D柱状图 带基准线的

浏览 2046 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-22   最后修改:2009-12-08
[/color]public static void drawInAndOutCalChart(String title,String bar1, String bar2,
IrisIoInterface io, Connection con, List intakeAndSportCal,
OnlineUser onlineUser, Log log, int drawBy) {
int size = intakeAndSportCal.size();
double[][] data = new double[2][size];

String[] rowKeys = { bar1, bar2 };
String[] columnKeys = new String[size];

String noDataMessage = "you have not any recoreds!";

// 将摄入卡路里与运动卡路里放入到data数组中去。
double bmr = getUserBMRByBlogId(con, log, io, onlineUser);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < size; j++) {
UserFoodRecordBean re = (UserFoodRecordBean) intakeAndSportCal
.get(j);
if (i == 0) {
data[i][j] = re.getCaloriesimple();
System.out.println("data[][] calsimple=" + data[i][j]);
} else {
if (drawBy == 1) {
bmr = bmr * 1;
} else if (drawBy == 2) {
bmr = bmr * 1;
}
data[i][j] = re.getSportTotalCalorie() + bmr;
}

System.out.println("drawsportCal= ^^^^^^^^^^^^^^^^^="
+ re.getSportTotalCalorie());

}
}

for (int x = 0; x < size; x++) {
UserFoodRecordBean re = (UserFoodRecordBean) intakeAndSportCal
.get(x);

columnKeys[x] = re.getUserfoodDate();
System.out.println("user_date=" + columnKeys[x]);
}

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);

JFreeChart chart = ChartFactory.createBarChart3D(title, null,
null, dataset, PlotOrientation.VERTICAL, true, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();

CategoryAxis domainAxis = plot.getDomainAxis();
// domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
plot.setNoDataMessage(noDataMessage);

String unit = "cal";
String label = "BMR=" + bmr + unit;
ValueMarker valuemarker = new ValueMarker(bmr);
valuemarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
valuemarker.setPaint(Color.PINK);
valuemarker.setStroke(new BasicStroke(1.0F));
valuemarker.setLabel(label);
valuemarker.setLabelFont(new Font("SansSerif", 0, 11));
valuemarker.setLabelPaint(Color.blue);
valuemarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
valuemarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
valuemarker.setLabelOffset(new RectangleInsets(5, 5, 5, 5));
float[] f = { 2, 4, 2, 4 };
valuemarker.setStroke(new BasicStroke(2.0f, 1, 1, 0, f, 1.0f)); // setStroke 将基准线设置为虚线,float[]数组实现。
plot.addRangeMarker(valuemarker, Layer.BACKGROUND);

ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
// 设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);

BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
// 设置每种卡路里代表的柱的颜色
renderer.setSeriesPaint(1, Color.red);
renderer.setSeriesPaint(0, Color.GREEN);

// 设置每种卡路里所包含的平行柱的之间距离
renderer.setItemMargin(0.05);
// 显示每个柱的数值,并修改该数值的字体属性
renderer
.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);

// 设置柱的透明度
plot.setForegroundAlpha(0.6f);
// 设置每种卡路里的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

HttpServletRequest request = io.getRequest();
String filename = "";
String graphURL = "";
try {
filename = ServletUtilities.saveChartAsPNG(chart, 750, 172, null,
io.getSession());
graphURL = request.getContextPath() + "/displayChart?filename="
+ filename;
System.out.println(filename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

io.setData("filename", filename, BeanShare.BEAN_SHARE_REQUEST);
io.setData("graphURL", graphURL, BeanShare.BEAN_SHARE_REQUEST);
io.setData("bmr", bmr, BeanShare.BEAN_SHARE_REQUEST);

}
[/size]
  • 大小: 12.7 KB
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics