`
eagletony
  • 浏览: 40718 次
  • 来自: ...
社区版块
存档分类
最新评论

jfreechart 柱图

阅读更多
package realtimeMonitor;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.json.JSONException;

public class DiskSpaceBar {
	public static void createBars() throws IOException, JSONException{

        CategoryDataset dataset = getDataSet();

        JFreeChart chart = ChartFactory.createBarChart3D("DISK_SPACE", // 图表标题
        "mountpoint", // 目录轴的显示标签
        "storedata", // 数值轴的显示标签
        dataset, // 数据集
        PlotOrientation.VERTICAL, // 图表方向:水平、垂直
        true,  // 是否显示图例(对于简单的柱状图必须是false)
        true,  // 是否生成工具
        true  // 是否生成URL链接
        );

        CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象

        //设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis

        org.jfree.chart.axis.CategoryAxis domainAxis = plot.getDomainAxis();

        domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%

        domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10

        domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)

        domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%

        //设定柱子的属性

        org.jfree.chart.axis.ValueAxis rangeAxis = plot.getRangeAxis();

        rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)



        //设置图表的颜色

        org.jfree.chart.renderer.category.BarRenderer3D renderer;

        renderer = new org.jfree.chart.renderer.category.BarRenderer3D();

        renderer.setBaseOutlinePaint(Color.red);

        renderer.setSeriesPaint(0, new Color(0, 255, 255));//计划柱子的颜色为青色

        renderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色

        renderer.setSeriesPaint(1,Color.magenta );//实报柱子的颜色为绿色

        renderer.setSeriesOutlinePaint(1,Color.red);//边框为红色
        
        renderer.setSeriesPaint(2, Color.green);//实报柱子的颜色为黄色

        renderer.setSeriesOutlinePaint(2,Color.red);//边框为红色

        renderer.setItemMargin(0.1);//组内柱子间隔为组宽的10%

        //显示每个柱的数值,并修改该数值的字体属性

        renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());

        renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗

        renderer.setItemLabelPaint(Color.black);//字体为黑色

        renderer.setItemLabelsVisible(true);

        plot.setRenderer(renderer);//使用我们设计的效果



        //设置纵横坐标的显示位置

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);//学校显示在下端(柱子竖直)或左侧(柱子水平)

        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); //人数显示在下端(柱子水平)或左侧(柱子竖直)


        
        //将饼图显示在图像界面上   
	        FileOutputStream ous = null;
	        String filePath="";//绝对路径
	        String webPath="WarnImages/diskSpaceBar.png";//"+request.getSession().getId()+"
	        try {
				filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
				filePath =filePath.replace("WEB-INF/classes/", "");
				filePath += webPath;
			//	System.out.println(filePath);
			} catch (URISyntaxException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
			        if (null != chart) {   
			        	ous = new FileOutputStream(filePath);
			            ChartUtilities.writeChartAsPNG(ous, chart,800,500);
			            //ous.flush();   
			        }  
		        } catch (IOException e) {
					e.printStackTrace();
				} // 生成图片
				finally {
					try {
						ous.close();// 最后关闭文件流
					} catch (IOException e) {
						e.printStackTrace();
					}
				} 

}

/**

 * 获取一个演示用的组合数据集对象

 * @return
 * @throws IOException 
 * @throws JSONException 

 */

private static CategoryDataset getDataSet() throws JSONException, IOException {
	
	Map map=ResolveJSONForShell.resolveJSONForDiskSpace();
	double[][] data=(double[][]) map.get("dataArg");
	List groupList=(List) map.get("groupList");
	List rowList=(List) map.get("rowList");
	
	
	String[] rowKeys = new String[rowList.size()];
	String[] columnKeys = new String[groupList.size()];
	for(int i=0;i<groupList.size();i++){
		columnKeys[i]=(String)groupList.get(i);
	}
	for(int j=0;j<rowList.size();j++){
		rowKeys[j]=(String)rowList.get(j);
	}
	
	
	CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 

	

        return dataset;

}

private static int randomNum()   
{      
    System.out.println((Math.random()*100+200));         
    return (int)(Math.random()*10+5);   
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics