用JfreeChart實現條形柱狀圖表
用Jfree實現條形柱狀圖表,java代碼實現。可經常用于報表的制作,代碼自動生成后可以自由查看。可以自由配置圖表的各個屬性,用來達到自己的要求和目的
import org.jfree.chart.;
import org.jfree.chart.plot.;
import org.jfree.chart.labels.;
import org.jfree.data.category.;import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import org.jfree.ui.;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DatasetUtilities;import com.dao.DBConnection;
public class JfreeChart {
public static void main(String[] args) throws IOException, SQLException { // TODO Auto-generated method stub double[][] data = new double[][] { { 1230, 1110, 1120, 1210, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860, 800 }, { 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860, 800, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860, 800 } }; String[] rowKeys = { "a", "b" }; String[] columnKeys = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }; CategoryDataset dataset = DatasetUtilities.createCategoryDataset( rowKeys, columnKeys, data); JFreeChart chart = ChartFactory.createBarChart(getNextDay() + "日交易量", "時間(/h)", "交易量", dataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot plot = chart.getCategoryPlot(); // 設置網格背景顏色 plot.setBackgroundPaint(Color.white); // 設置網格豎線顏色 plot.setDomainGridlinePaint(Color.pink); // 設置網格橫線顏色 plot.setRangeGridlinePaint(Color.pink); // 顯示每個柱的數值,并修改該數值的字體屬性 BarRenderer renderer = new BarRenderer(); // renderer.setBaseItemLabelGenerator(new // StandardCategoryItemLabelGenerator()); // renderer.setBaseItemLabelsVisible(true); // 默認的數字顯示在柱子中,通過如下兩句可調整數字的顯示 // 注意:此句很關鍵,若無此句,那數字的顯示會被覆蓋,給人數字沒有顯示出來的問題 // renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( // ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); // renderer.setItemLabelAnchorOffset(10D); // 設置每個地區所包含的平行柱的之間距離 renderer.setItemMargin(0.05); plot.setRenderer(renderer); // 設置地區、銷量的顯示位置 // 將下方的“標注”放到上方 // plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); // 將默認放在左邊的“銷量”放到右方 // plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); drawToOutputStream("src//a.jpg", chart); } /** * step3: 輸出圖表到指定的磁盤 * * @param destPath * @param chart */ public static void drawToOutputStream(String destPath, JFreeChart chart) { FileOutputStream fos = null; try { fos = new FileOutputStream(destPath); // ChartUtilities.writeChartAsJPEG( ChartUtilities.writeChartAsPNG(fos, // 指定目標輸出流 chart, // 圖表對象 1000, // 寬 500, // 高 null); // ChartRenderingInfo信息 } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getNextDay() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); // 得到前一天 Date date = calendar.getTime(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // System.out.println(df.format(date)); return df.format(date); }
} </pre>
顯示生成結果:
![]()
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!