谁知道怎么让jfreeChart的柱状折线图的柱状y轴和折线的y轴一致

莫莫水心 发布于 2014/07/17 16:49
阅读 830
收藏 0
public class DualaxisChartLeft {
private PlotOrientation plotOrientation = PlotOrientation.VERTICAL;
private Map<String,Object>  map = null;
public DualaxisChartLeft(Map<String,Object>  map) {
this.map = map;
}


public DualaxisChartLeft(Map<String,Object>  map,PlotOrientation plotOrientation) {
this.plotOrientation = plotOrientation;
this.map = map;
}

@SuppressWarnings("unchecked")
public JFreeChart createChart() {
Map<String,Object> barMap = (Map<String, Object>) map.get("BAR");


Vector<Serie> seriesNetProfit = new Vector<Serie>();
seriesNetProfit.add(new Serie((String)barMap.get("LABEL"), (String[])barMap.get("VALUES")));


Vector<Serie> seriesPayoutRatio = new Vector<Serie>();

Map<String,Object> lineMap = (Map<String, Object>) map.get("LINE");
if(lineMap!=null){
Set<String> set = lineMap.keySet();
Iterator<String> it = set.iterator();
String key = null;
while(it.hasNext()){
key = it.next();
seriesPayoutRatio.add(new Serie(key, (String[])lineMap.get(key)));
}
}

DefaultCategoryDataset datasetNetProfit = ChartUtils.createDefaultCategoryDataset(seriesNetProfit, (String[])barMap.get("CATEGORIES"));
JFreeChart chart = ChartFactory.createBarChart(map.get("TITLE")==null?"":(String)map.get("TITLE"), "", (String)map.get("AXIS_LABEL"), datasetNetProfit,plotOrientation,         // 显示方向
true,                 // 是否显示图例
true,                 // 是否显示 tooltip
                false);     


ChartUtils.setAntiAlias(chart);// 抗锯齿
ChartUtils.setBarRenderer(chart.getCategoryPlot(), false);
// 设置坐标轴
ChartUtils.setXAixs(chart.getCategoryPlot());
ChartUtils.setYAixs(chart.getCategoryPlot());
// linechart
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
DefaultCategoryDataset datasetPayoutRatio = ChartUtils.createDefaultCategoryDataset(seriesPayoutRatio, (String[])barMap.get("CATEGORIES"));
categoryplot.setDataset(1, datasetPayoutRatio);
categoryplot.mapDatasetToRangeAxis(1, 1);
// X轴刻度
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
// 右侧Y轴
NumberAxis numberaxis = new NumberAxis("");
categoryplot.setRangeAxis(1, numberaxis);
// 隐藏Y刻度
numberaxis.setAxisLineVisible(false);
numberaxis.setTickMarksVisible(false);
numberaxis.setVisible(false);
// 设置折线图样式
LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
lineRenderer.setSeriesPaint(0, new Color(255, 185, 1));
lineRenderer.setBaseShapesVisible(true);// 数据点绘制形状
categoryplot.setRenderer(1, lineRenderer);

categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);// 绘制Z-index, 将折线图在前面
chart.getLegend().setPosition(RectangleEdge.TOP);//标注在顶部
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
return chart;
}


public static void main(String[] args) {
Map<String,Object>  map = new HashMap<String,Object>();
//Y轴标签
map.put("AXIS_LABEL", "成绩");
//柱状
Map<String,Object> barMap = new HashMap<String,Object>();
barMap.put("CATEGORIES", new String[]{ "语文", "数学", "英语", "物理", "化学", "生物" });
barMap.put("VALUES", new String[]{ "111", "120", "119", "98", "78", "77" });
barMap.put("LABEL", "你的成绩");
map.put("BAR", barMap);
//线条
Map<String,Object> lineMap = new HashMap<String,Object>();
lineMap.put("最高分", new String[]{ "115", "144", "136.5", "100", "99", "88"});
lineMap.put("年级平均", new String[]{ "82", "80", "94", "59", "60", "58"});
lineMap.put("A层次平均", new String[]{ "100", "120", "120", "83", "80", "72"});
map.put("LINE", lineMap);

DualaxisChartLeft dual = new DualaxisChartLeft(map);
ChartUtils.createChartTmpPng(dual.createChart(), 420, 240);
}

加载中
0
扁-哥
扁-哥

numberaxis.setVisible(false); 这一行去掉

这个没有必要一致把这个刻度是根据数据来的,没有可比性!

如果实在想一致的话,你直接把左侧的ValueAlis给右侧的就行了


// 右侧Y轴
NumberAxis numberaxis = new NumberAxis("");
categoryplot.setRangeAxis(1, categoryplot.getRangeAxis());

返回顶部
顶部