JFreeChart最佳實(shí)踐:柱狀圖
作者:何楓abc
本文將介紹作者通過(guò)Java最佳圖形解決方案JFreeChart實(shí)現(xiàn)柱狀圖的詳細(xì)過(guò)程。
這幾天由于客觀因素(天冷和停電)和主觀因素(項(xiàng)目吃緊,每天都加班到9點(diǎn),回來(lái)也就不想搞了)。就這樣耽擱了好幾天的時(shí)間了!不過(guò)偶然的機(jī)會(huì)在網(wǎng)上換到了一篇整理好的API,所以整理常用的柱狀圖就容易多了。里面的封裝方法太多了,一般是很難記住的.很多都要實(shí)踐了才知道其作用,這里就直接上代碼了。也好之后備用鞏固學(xué)習(xí)。
package com.huawei.jfreechart;
- import java.awt.Color;
- import java.awt.Font;
- 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.plot.PlotOrientation;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- /**
- * @name 何楓
- * @date 2010-12-17
- * @action createBarChart3DTest.java
- * @time 下午10:35:52
- * @package_name com.huawei.jfreechart
- * @project_name jfreechartTest
- */
- public class createBarChart3DTest {
- private static CategoryDataset getDataSet() {
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(200, "計(jì)劃", "清華大學(xué)");
- dataset.addValue(400, "實(shí)報(bào)", "清華大學(xué)");
- dataset.addValue(360, "計(jì)劃", "天津大學(xué)");
- dataset.addValue(520, "實(shí)報(bào)", "天津大學(xué)");
- dataset.addValue(100, "計(jì)劃", "北京大學(xué)");
- dataset.addValue(150, "實(shí)報(bào)", "北京大學(xué)");
- dataset.addValue(280, "計(jì)劃", "復(fù)旦大學(xué)");
- dataset.addValue(300, "實(shí)報(bào)", "復(fù)旦大學(xué)");
- return dataset;
- }
- public static void main(String[] args) {
- CategoryDataset dataset = getDataSet();
- JFreeChart chart = ChartFactory.createBarChart3D("水果產(chǎn)量圖", "水果產(chǎn)品","銷售數(shù)量", dataset, PlotOrientation.VERTICAL, true, true, true);
- ChartFrame frame = new ChartFrame("水果產(chǎn)量圖 ", chart, true);
- // 自定義設(shè)定背景色
- // chart.setBackgroundPaint(Color.getHSBColor(23,192,223));
- chart.setBackgroundPaint(Color.WHITE);
- // 獲得 plot:3dBar為CategoryPlot
- CategoryPlot categoryPlot = chart.getCategoryPlot();
- // 設(shè)定圖表數(shù)據(jù)顯示部分背景色
- categoryPlot.setBackgroundPaint(Color.BLACK);
- // 橫坐標(biāo)網(wǎng)格線
- categoryPlot.setDomainGridlinePaint(Color.RED);
- // 設(shè)置網(wǎng)格線可見(jiàn)
- categoryPlot.setDomainGridlinesVisible(true);
- // 縱坐標(biāo)網(wǎng)格線
- categoryPlot.setRangeGridlinePaint(Color.RED);
- // 重要的類,負(fù)責(zé)生成各種效果
- // BarRenderer3D renderer=(BarRenderer3D) categoryPlot.getRenderer();
- // 獲取縱坐標(biāo)
- NumberAxis numberaxis = (NumberAxis) categoryPlot.getRangeAxis();
- // 設(shè)置縱坐標(biāo)的標(biāo)題字體和大小
- numberaxis.setLabelFont(new Font("黑體", Font.CENTER_BASELINE, 24));
- // 設(shè)置叢坐標(biāo)的坐標(biāo)值的字體顏色
- numberaxis.setLabelPaint(Color.BLACK);
- // 設(shè)置叢坐標(biāo)的坐標(biāo)軸標(biāo)尺顏色
- numberaxis.setTickLabelPaint(Color.RED);
- // 坐標(biāo)軸標(biāo)尺顏色
- numberaxis.setTickMarkPaint(Color.BLUE);
- // 叢坐標(biāo)的默認(rèn)間距值
- // numberaxis.setAutoTickUnitSelection(true);
- // 設(shè)置叢坐標(biāo)間距值
- numberaxis.setAutoTickUnitSelection(false);
- numberaxis.setTickUnit(new NumberTickUnit(150));
- // 獲取橫坐標(biāo)
- CategoryAxis domainAxis = categoryPlot.getDomainAxis();
- // 設(shè)置橫坐標(biāo)的標(biāo)題字體和大小
- domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 13));
- // 設(shè)置橫坐標(biāo)的坐標(biāo)值的字體顏色
- domainAxis.setTickLabelPaint(Color.RED);
- // 設(shè)置橫坐標(biāo)的坐標(biāo)值的字體
- domainAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 30));
- // 設(shè)置橫坐標(biāo)的顯示
- domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));
- // 這句代碼解決了底部漢字亂碼的問(wèn)題
- chart.getLegend().setItemFont(new Font("黑體", 0, 16));
- // 設(shè)置圖例標(biāo)題
- Font font = new java.awt.Font("黑體", java.awt.Font.CENTER_BASELINE, 50);
- TextTitle title = new TextTitle("項(xiàng)目狀態(tài)分布");
- title.getBackgroundPaint();
- title.setFont(font);
- // 設(shè)置標(biāo)題的字體顏色
- title.setPaint(Color.RED);
- chart.setTitle(title);
- frame.pack();
- frame.setVisible(true);
- }
- }
原文鏈接:http://hefeng1987-net-163-com.iteye.com/blog/846041
【編輯推薦】
責(zé)任編輯:林師授
來(lái)源:
何楓abc的博客