java柱状图(2)

来源:互联网 发布:javascript网站 编辑:程序博客网 时间:2024/05/01 21:35

上一篇中用纯java的方式写了一个柱状图,可是结果非常的难看,下面用第三方插件来搞定这个问题,就是jfreechart,据说java做界面简直就是搓到爆,仅仅就是好奇一下,just so so

好了,jfeechart安装的过程我就不说了,网上到处都是,仅仅给出我的代码和结果


import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.NumberTickUnit;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.junit.experimental.categories.Categories;public class b{/* * 这个程序用来显示上一个程序中的柱状图 * 利用第三方库jfreechart */public static void main(String[] argv){BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_BGR);TextTitle title = new TextTitle("柱状图");CategoryDataset data = getdata();JFreeChart chart = ChartFactory.createBarChart3D("柱状图", "数", "例子", data);ChartFrame frame = new ChartFrame("柱状图",chart);/*frame.pack();frame.setVisible(true);*/// 自定义设定背景色          // chart.setBackgroundPaint(Color.getHSBColor(23,192,223));          chart.setBackgroundPaint(Color.WHITE);          // 获得 plot:3dBar为CategoryPlot          CategoryPlot categoryPlot = chart.getCategoryPlot();          // 设定图表数据显示部分背景色          categoryPlot.setBackgroundPaint(Color.BLACK);          // 横坐标网格线          categoryPlot.setDomainGridlinePaint(Color.RED);          // 设置网格线可见          categoryPlot.setDomainGridlinesVisible(true);          // 纵坐标网格线          categoryPlot.setRangeGridlinePaint(Color.RED);          // 重要的类,负责生成各种效果          // BarRenderer3D renderer=(BarRenderer3D) categoryPlot.getRenderer();          // 获取纵坐标          NumberAxis numberaxis = (NumberAxis) categoryPlot.getRangeAxis();          // 设置纵坐标的标题字体和大小          numberaxis.setLabelFont(new Font("黑体", Font.CENTER_BASELINE, 24));          // 设置丛坐标的坐标值的字体颜色          numberaxis.setLabelPaint(Color.BLACK);          // 设置丛坐标的坐标轴标尺颜色          numberaxis.setTickLabelPaint(Color.RED);          // 坐标轴标尺颜色          numberaxis.setTickMarkPaint(Color.BLUE);          // 丛坐标的默认间距值          // numberaxis.setAutoTickUnitSelection(true);          // 设置丛坐标间距值          numberaxis.setAutoTickUnitSelection(false);          numberaxis.setTickUnit(new NumberTickUnit(150));          // 获取横坐标          CategoryAxis domainAxis = categoryPlot.getDomainAxis();          // 设置横坐标的标题字体和大小          domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 13));          // 设置横坐标的坐标值的字体颜色          domainAxis.setTickLabelPaint(Color.RED);          // 设置横坐标的坐标值的字体          domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 30));          // 设置横坐标的显示          domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));          // 这句代码解决了底部汉字乱码的问题          chart.getLegend().setItemFont(new Font("黑体", 0, 16));          // 设置图例标题          Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 50);          //TextTitle title = new TextTitle("项目状态分布");          title.getBackgroundPaint();          title.setFont(font);          // 设置标题的字体颜色          title.setPaint(Color.RED);          chart.setTitle(title);          frame.pack();          frame.setVisible(true);          /*Image image = frame.createImage(500, 500);        Graphics g = image.getGraphics();        String filename = "D:\\3.jpg";        File file = new File(filename);        if(!file.exists())        {        try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}        }        FileOutputStream s = null;try {s = new FileOutputStream(file);} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}        try {ImageIO.write(img, "JPEG", s);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}*/        }private static CategoryDataset getdata(){DefaultCategoryDataset data = new DefaultCategoryDataset();data.addValue(1, "1", "1");data.addValue(2, "2", "2");data.addValue(3, "3", "3");data.addValue(4, "4", "4");data.addValue(5, "5", "5");data.addValue(6, "6", "6");data.addValue(7, "7", "7");data.addValue(8, "8", "8");data.addValue(9, "9", "9");data.addValue(10, "10", "10");return data;}}


0 0