JFreeChart问题求助(在线等指导)
先看图目标效果图:
我的实现:
//@@@@@@@
现在还差
1.排序,负数排下面,值是从hashtable的value取出的,在map里按value排序比较麻烦,jfreechart是否有自动排序的功能?
2.正数显示黑色,负数显示白色 ?
3.条的宽度?
4.每个条柱的宽度?
--------------------编程问答-------------------- 早上都没人看csdn么? --------------------编程问答-------------------- 排序已完成,还是HashMap 按value排序实现的。。。
/**
* 升降比的柱形图
*/
public String getBarChart(Map<String, Double> map, String msg) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//取出所有key value 并放入dataset
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
Double value = map.get(key);
dataset.addValue(value * 100, key, key);
}
String picTitle = ""; //统计图名称
String picName = ""; //生成图片名称
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
String y_M = String.valueOf(year) + month;
if (msg.equals("与上月比较")) {
picTitle = "本月甲乙类传染病与上月相比较";
picName = "compMon" + y_M + ".jpg";
} else if (msg.equals("与去年同期比较")) {
picTitle = "本月甲乙类传染病与去年同期相比较";
picName = "compYear" + y_M + ".jpg";
}
JFreeChart chart = ChartFactory.createBarChart(picTitle, // 标题
"病种", // X轴
"升降比(%)", // Y轴
dataset, // dataset
PlotOrientation.HORIZONTAL, // 图表方向:水平、垂直
true, // 是否显示图例
true, // 是否显示工具提示
true); // 是否生成URL
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
CategoryAxis domainAxis = categoryplot.getDomainAxis();
TextTitle textTitle = chart.getTitle();
//将X轴显示在下方
categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
//设置X轴参数格式
DecimalFormat df = new DecimalFormat("0.00");
numberaxis.setNumberFormatOverride(df);
// 柱图的呈现器
BarRenderer renderer = new BarRenderer();
renderer.setIncludeBaseInRange(true); // 显示每个柱的数值,并修改该数值的字体属性
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
renderer.setDrawBarOutline(true); // 设置柱子边框可见
renderer.setItemMargin(0.02);
renderer.setMaximumBarWidth(0.05);
renderer.setMinimumBarLength(15.5);
renderer.setSeriesPaint(1, Color.WHITE);
categoryplot.setRenderer(renderer); // 给柱图添加呈现器
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
ChartFrame frame = new ChartFrame(picTitle, chart);
frame.pack();
// frame.setVisible(true);
String path = this.getClass().getResource("").getPath();
int index = path.indexOf("WEB-INF");
String imgPath = path.substring(1, index) + "StatImg/";
try {
System.out.println("build picture...");
ChartUtilities.saveChartAsJPEG(new File(imgPath + picName), chart,
640, 480);
} catch (IOException e) {
e.printStackTrace();
}
return picName;
}
还剩下 柱子加宽, 正数黑色负数白色, 柱子边上的标注 。。。
求指导 --------------------编程问答-------------------- 颜色也搞定了 剩下 标注,和柱子宽度
前辈们教教我,我这太慢了。。。 --------------------编程问答-------------------- 有对应的属性设置的,你查查,我这还是去年看过的,忘了…… --------------------编程问答-------------------- void setItemMargin(double percent) 每个BAR之间的间隔
void setMaxBarWidth(double percent) 每个BAR的最大宽度
void setMinimumBarLength(double min) 最短的BAR长度,避免数值太小而显示不出
这三个方法我都设了啊! 没效果。。
补充:Java , Java EE