热门关键字:
jquery > jquery教程 > java > java生成柱状图的2种方式源代码

java生成柱状图的2种方式源代码

3626
作者:管理员
发布时间:2014/3/7 11:31:31
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=402

此文章是由jquery学堂4群(76287629)的网友【厦门-java-小汪】分享到群共享的,觉得对网友们的学习有帮助就把它整理到JquerySchool网站上分享了,希望大家好好利用哦。。。


java生成柱状图的2种方式源代码


方式1

package com.zmkj.business.ge.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

public class ZZT {
	 private static Font mFont = new Font("宋体", Font.PLAIN, 12);
	  
	  public static void main(String[] args){
	    double[] value = {7.00d,1.25d,0.83d,1.38d,2.35d};
	    createimage(value,7.00d);
	  }
	  
	  private static void createimage(double[] value,double maxvalue){
	    try{
	      int ranwidth = 33,ranheight = 112;
	      int width = 764, height = 168;
	      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	      Graphics g = image.getGraphics();
	      g.setColor(new Color(255, 255,255));
	      g.fillRect(1, 1, width - 1, height - 1);
	      g.setColor(new Color(255, 255,255));
	      g.drawRect(0, 0, width - 1, height - 1);
	      
	      g.setFont(mFont);
	      g.setColor(new Color(255, 26,135));
	      
	      Color c = new Color(227, 227, 227);
	      for(int i= 0 ;i<5;i++){
	        if(i==0){
	          g.setColor(c);
	          g.fillRect(72, 54, 33, ranheight);
	          g.setColor(c);
	          g.drawRect(72, 54, 33, ranheight);
	        }else{
	          g.setColor(c);
	          g.fillRect(72+(ranwidth+110)*i, 54, 33, ranheight);
	          g.setColor(c);
	          g.drawRect(72+(ranwidth+110)*i, 54, 33, ranheight);
	        }
	      }
	      
	      for(int i= 0 ;i<value.length;i++){
	        Color myc = null;
	        if(i == 0){
	          myc = new Color(75,178,209);
	        }else if(i==1){
	          myc = new Color(243,152,1);
	        }else if(i == 2){
	          myc = new Color(90,159,105);
	        }else if(i == 3){
	          myc = new Color(144,130,189);
	        }else{
	          myc = new Color(154,190,66);
	        }
	        if(i==0){
	          g.setColor(myc);
	          g.fillRect(72, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
	          g.setColor(myc);
	          g.fillRect(72, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
	        }else{
	          g.setColor(myc);
	          g.fillRect(72+(ranwidth+110)*i, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
	          g.setColor(myc);
	          g.drawRect(72+(ranwidth+110)*i, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
	        }
	      }
	      c = new Color(181, 181, 181);
	      for(int i = 0;i<5;i++){
	        if(i==0){
	          g.setColor(c);
	          g.setFont(mFont);
	          g.drawString(value[i]+"s", 72,48);
	        }else{
	          g.setColor(c);
	          g.setFont(mFont);
	          g.drawString(value[i]+"s", 72+(ranwidth+110)*i,48);
	        }
	      }
	      
	      g.dispose();
	      String filename = "e:\\createimg.jpg";
	      File file = new File(filename);
	      if(!file.exists()){
	        file.createNewFile();
	      }
	      FileOutputStream s = new FileOutputStream(file);
	      ImageIO.write(image, "JPEG", s);
	      
	    }catch(Exception e){
	      e.printStackTrace();
	    }
	  }
}

方式2

package com.zmkj.business.ge.action;

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

public class ZZT2 {
	/**
	* @param args
	* @throws IOException 
	*柱状图
	*/
	public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub

//	     创建类别图(Category)数据对象

	       DefaultCategoryDataset dataset =new DefaultCategoryDataset();

	       dataset.addValue(100,"北京","苹果");

	       dataset.addValue(100,"上海","苹果");

	       dataset.addValue(100,"广州","苹果");

	       dataset.addValue(200,"北京","梨子");

	       dataset.addValue(200,"上海","梨子");

	       dataset.addValue(200,"广州","梨子");

	       dataset.addValue(300,"北京","葡萄");

	       dataset.addValue(350,"上海","葡萄");

	       dataset.addValue(300,"广州","葡萄");

	       dataset.addValue(10,"北京","香蕉");

	       dataset.addValue(60,"上海","香蕉");

	       dataset.addValue(120,"广州","香蕉");

	       dataset.addValue(440,"北京","荔枝");

	       dataset.addValue(499,"上海","荔枝");

	       dataset.addValue(500,"广州","荔枝");

	JFreeChart chart=ChartFactory.createBarChart3D("水果产量图","还是乱码??","水果(Y轴上的字)", dataset, PlotOrientation.VERTICAL,true,true,true);
	chart.getLegend().setItemFont(new Font("微软雅黑",Font.PLAIN,12));

	Font font=new Font("微软雅黑",Font.CENTER_BASELINE,20); 

	//解决乱码
	CategoryPlot plot = (CategoryPlot) chart.getPlot();
	CategoryAxis categoryAxis = plot.getDomainAxis(); 
	categoryAxis.setTickLabelFont(new Font("SimSun", Font.PLAIN,12));
	categoryAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));//设置x坐标轴的文字
//	       categoryAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
	NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
	numberAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));// 设置y坐标轴的文字      
	//解决乱码
	TextTitle title=new TextTitle("水果产量图");
	chart.getLegend().setItemFont(new Font("微软雅黑",Font.PLAIN,12));
	chart.setTitle(title);
	title.setFont(font);
	FileOutputStream imgjpg=null;
	try {
	imgjpg=new FileOutputStream("e:\\ffffffffffff.jpg");
	ChartUtilities.writeChartAsJPEG(imgjpg,1.0f,chart,640,480,null); 
	imgjpg.close();
	} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}

	}

	//有一处乱码一直没有解决 如果有你知道的话 请告诉我 谢谢

}





如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:java生成柱状图 java图表控件 java数据统计控件
友荐云推荐