最近开发需要使用 jfreechart,网上的东西不算太多,这个jar虽然免费,但是资料收费,智能自己研究了
整体思路上,先创建出chart,在获取plot,在plot上面逐个添加坐标,最后将坐标很数据集对map映射,得到结果了、
将坐标信息,添加到xyseries中
public static void initSeries(XYSeries series,Map<Integer,Integer> data){ for(Map.Entry<Integer, Integer> entry : data.entrySet()){ series.add(entry.getKey(),entry.getValue()); } }
实现,多个坐标纵轴
public static JFreeChart getMuitiAxisersfinal(List<String> names,Map<String,Map<Integer,Integer>> datas,Map <String,String> whichAxis,List <String> axisName){ Color []colors={Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW,Color.ORANGE,Color.PINK}; JFreeChart chart = null; XYPlot plot; boolean iniflag=true; if(names==null||names.size()==0){ return null; } if( axisName==null|| axisName.size()==0){ return null; } if(datas==null||datas.size()==0){ return null; } for(int i=0;i<names.size();i++){ XYSeries series = new XYSeries(new NameComparable<String>(names.get(i))); Map<Integer,Integer>data=datas.get(names.get(i)); initSeries(series,data); XYSeriesCollection dataset=new XYSeriesCollection(); dataset.addSeries(series); if(iniflag){ chart = ChartFactory.createXYLineChart("MultiAxis", "X axis", ""+axisName.get(0), dataset, PlotOrientation.VERTICAL, true, true, false); iniflag=false; for(int j=1;j<axisName.size();j++){ NumberAxis axis2 = new NumberAxis("The"+axisName.get(j)+" Axis"); int y=i>colors.length-1?i%colors.length:i; axis2.setAxisLinePaint(colors[y]); axis2.setLabelPaint(colors[y]); axis2.setTickLabelPaint(colors[y]); plot = chart.getXYPlot(); plot.setRangeAxis(j, axis2); } } plot = chart.getXYPlot(); int y=i>colors.length-1?i%colors.length:i; plot.setDataset(i, dataset); String atAxisName= whichAxis.get(names.get(i)); plot.mapDatasetToRangeAxis(i, axisName.indexOf(atAxisName)); XYLineAndShapeRenderer render2 = new XYLineAndShapeRenderer(); render2.setSeriesPaint(i, colors[y]); plot.setRenderer(i, render2); //可能理解有点问题 } TextTitle copyright = new TextTitle(" Test "); copyright.setPosition(RectangleEdge.BOTTOM); copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT); copyright.setFont(new Font("黑体", 12, 12)); chart.addSubtitle(copyright); chart.getLegend().setItemFont(new Font("黑体", 12, 12)); return chart; }
最后是测试数据
public static void main(String []args){ List<String> list=new ArrayList<String>(); list.add("系列一"); list.add("系列二"); list.add("系列三"); Map <String,Map<Integer,Integer>> map=new HashMap<String,Map<Integer,Integer>>(); Map<Integer,Integer> data=new HashMap<Integer,Integer>(); data.put(2011, 100); data.put(2013, 200); data.put(2015, 400); Map<Integer,Integer> dat2a=new HashMap<Integer,Integer>(); dat2a.put(2011, 20); dat2a.put(2014, 40); dat2a.put(2017, 30); Map<Integer,Integer> dat3a=new HashMap<Integer,Integer>(); dat3a.put(2015, 2); dat3a.put(2016, 4); dat3a.put(2017, 6); map.put("系列一", data); map.put("系列二", dat2a); map.put("系列三", dat3a); Map<String,String> axisToname=new HashMap<String,String>(); axisToname.put("系列一","Test1"); axisToname.put("系列二", "Test2"); axisToname.put("系列三", "Test3"); List <String>axitName=new ArrayList<String>(); axitName.add("坐标一"); axitName.add("坐标二"); Map<String,String> whichAxis=new HashMap<String,String>(); whichAxis.put("系列一","坐标一"); whichAxis.put("系列二","坐标一"); whichAxis.put("系列三","坐标二"); JFreeChart chart= getMuitiAxisersfinal(list,map,whichAxis,axitName); ChartFrame frame = new ChartFrame("多坐标轴", chart); frame.pack(); frame.setVisible(true); }
我也是个新手,刚刚学校jfreechart,希望和大家一起交流