java图表-使用JfreeChart画散点图

使用JFreeChart在Java中绘制散点图教程
本文介绍了如何使用JFreeChart库在Java项目中创建散点图。首先,需要下载并导入jcommon和jfreechart库。然后,通过创建DefaultXYDataset,设置图表主题和数据,最后利用ChartFactory生成散点图并展示。

1.下载JFreeChart

         http://www.jfree.org/jfreechart/

2. 在项目中导入jcommon-1.0.23.jar 和 jfreechart-1.0l19.jar


3.使用JFreeChart画散点图


public void showAttr(LinkedList ac_cnlList,ArrayList all) {  
        
        DefaultXYDataset xydataset = new DefaultXYDataset(); 
        
        //创建主题样式  
        StandardChartTheme mChartTheme = new StandardChartTheme("CN");  
        //设置标题字体  
        mChartTheme.setExtraLargeFont(new Font("黑体", Font.BOLD, 20));  
        //设置轴向字体  
        mChartTheme.setLargeFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
        //设置图例字体  
        mChartTheme.setRegularFont(new Font("宋体", Font.CENTER_BASELINE, 15));  
        //应用主题样式  
        ChartFactory.setChartTheme(mChartTheme);  
        
        //根绝实际需求加载数据集到xydatasets中
    	for(int l = 0; l < all.size(); l++){
		
			int size = ((Set) all.get(l)).size();
			double [][]datas = new double[2][size];
			int m =0;
			for(Iterator it = ((Set) all.get(l)).iterator();it.hasNext();m++){
				HashMap line = ((HashMap)ac_cnlList.get((Integer) it.next()));
				double AC = (Double) line.get("AC");
				double CNL =  (Double) line.get("CNL");
				datas[0][m] = AC;    //x轴
				datas[1][m] = CNL;   //y轴
			
			}
			xydataset.addSeries(l, datas);  //l为类别标签
			
		}

        JFreeChart chart = ChartFactory.createScatterPlot("k2 =10,k3=20,kernel=2", "GR", "CNL", xydataset, PlotOrientation.VERTICAL, true, false, false);  
        ChartFrame frame = new ChartFrame("散点图", chart, true);  
        chart.setBackgroundPaint(Color.white);    
        chart.setBorderPaint(Color.GREEN);    
        chart.setBorderStroke(new BasicStroke(1.5f));    
        XYPlot xyplot = (XYPlot) chart.getPlot();    
    
        xyplot.setBackgroundPaint(new Color(255, 253, 246));    
        ValueAxis vaaxis = xyplot.getDomainAxis();    
        vaaxis.setAxisLineStroke(new BasicStroke(1.5f));    
    
        ValueAxis va = xyplot.getDomainAxis(0);    
        va.setAxisLineStroke(new BasicStroke(1.5f));    
    
        va.setAxisLineStroke(new BasicStroke(1.5f)); 	    // 坐标轴粗细    
        va.setAxisLinePaint(new Color(215, 215, 215));    // 坐标轴颜色    
        xyplot.setOutlineStroke(new BasicStroke(1.5f));   // 边框粗细    
        va.setLabelPaint(new Color(10, 10, 10));          // 坐标轴标题颜色    
        va.setTickLabelPaint(new Color(102, 102, 102));   // 坐标轴标尺值颜色    
        ValueAxis axis = xyplot.getRangeAxis();    
        axis.setAxisLineStroke(new BasicStroke(1.5f));    
    
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot    
                .getRenderer();    
        xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);    
        xylineandshaperenderer.setUseOutlinePaint(true);    
        NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();    
        numberaxis.setAutoRangeIncludesZero(false);    
        numberaxis.setTickMarkInsideLength(2.0F);    
        numberaxis.setTickMarkOutsideLength(0.0F);    
        numberaxis.setAxisLineStroke(new BasicStroke(1.5f));    
          
        frame.pack();  
        frame.setVisible(true);  
    } 


评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值