JFreeChart柱狀圖單組柱子的不同顏色顯示
JFreeChart柱狀圖中單組柱子用不同顏色來顯示的實現方法是自定義一個Renderer來繼承BarRenderer類,然后重載getItemPaint(int i,int j)方法。
實現效果如下:
實現代碼如下:
public class CustomRenderer extends org.jfree.chart.renderer.category.BarRenderer { /** * */ private static final long serialVersionUID = 784630226449158436L; private Paint[] colors; //初始化柱子顏色 private String[] colorValues = { "#AFD8F8", "#F6BD0F", "#8BBA00", "#FF8E46", "#008E8E", "#D64646" }; public CustomRenderer() { colors = new Paint[colorValues.length]; for (int i = 0; i < colorValues.length; i++) { colors[i] = Color.decode(colorValues[i]); } } //每根柱子以初始化的顏色不斷輪循 public Paint getItemPaint(int i, int j) { return colors[j % colors.length]; } }
public class CreateJFreeChartBarColor { /** * 創建JFreeChart Bar Chart(柱狀圖) */ public static void main(String[] args) { // 步驟1:創建CategoryDataset對象(準備數據) CategoryDataset dataset = createDataset(); // 步驟2:根據Dataset 生成JFreeChart對象,以及做相應的設置 JFreeChart freeChart = createChart(dataset); // 步驟3:將JFreeChart對象輸出到文件,Servlet輸出流等 saveAsFile(freeChart, "E:\\bar.png", 500, 400); } // 保存為文件 public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); // 保存為PNG文件 ChartUtilities.writeChartAsPNG(out, chart, weight, height); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // do nothing } } } } // 根據CategoryDataset生成JFreeChart對象 public static JFreeChart createChart(CategoryDataset categoryDataset) { JFreeChart jfreechart = ChartFactory.createBarChart("學生統計圖", // 標題 "學生姓名", // categoryAxisLabel (category軸,橫軸,X軸的標簽) "年齡", // valueAxisLabel(value軸,縱軸,Y軸的標簽) categoryDataset, // dataset PlotOrientation.VERTICAL, false, // legend false, // tooltips false); // URLs Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); jfreechart.setTextAntiAlias(false); jfreechart.setBackgroundPaint(Color.white); CategoryPlot plot = jfreechart.getCategoryPlot();// 獲得圖表區域對象 // 設置橫虛線可見 plot.setRangeGridlinesVisible(true); // 虛線色彩 plot.setRangeGridlinePaint(Color.gray); // 數據軸精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); // vn.setAutoRangeIncludesZero(true); DecimalFormat df = new DecimalFormat("#0.0"); vn.setNumberFormatOverride(df); // 數據軸數據標簽的顯示格式 // x軸設置 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 軸標題 domainAxis.setTickLabelFont(labelFont);// 軸數值 // Lable(Math.PI/3.0)度傾斜 // domainAxis.setCategoryLabelPositions(CategoryLabelPositions // .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(6.00f);// 橫軸上的 Lable // 是否完整顯示 // 設置距離圖片左端距離 domainAxis.setLowerMargin(0.1); // 設置距離圖片右端距離 domainAxis.setUpperMargin(0.1); // 設置 columnKey 是否間隔顯示 // domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis); // 設置柱圖背景色(注意,系統取色的時候要使用16位的模式來查看顏色編碼,這樣比較準確) plot.setBackgroundPaint(new Color(255, 255, 204)); // y軸設置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(labelFont); rangeAxis.setTickLabelFont(labelFont); // 設置最高的一個 Item 與圖片頂端的距離 rangeAxis.setUpperMargin(0.15); // 設置最低的一個 Item 與圖片底端的距離 rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis); // 解決中文亂碼問題(關鍵) TextTitle textTitle = jfreechart.getTitle(); textTitle.setFont(new Font("黑體", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); vn.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); vn.setLabelFont(new Font("黑體", Font.PLAIN, 12)); // jfreechart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12)); // 使用自定義的渲染器 CustomRenderer renderer = new CustomRenderer(); // 設置柱子寬度 renderer.setMaximumBarWidth(0.2); // 設置柱子高度 renderer.setMinimumBarLength(0.2); // 設置柱子邊框顏色 renderer.setBaseOutlinePaint(Color.BLACK); // 設置柱子邊框可見 renderer.setDrawBarOutline(true); // 設置每個地區所包含的平行柱的之間距離 renderer.setItemMargin(0.5); jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // 顯示每個柱的數值,并修改該數值的字體屬性 renderer.setIncludeBaseInRange(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer); // 設置柱的透明度 plot.setForegroundAlpha(1.0f); // 背景色 透明度 plot.setBackgroundAlpha(0.5f); return jfreechart; } // 創建CategoryDataset對象 public static CategoryDataset createDataset() { double[][] data = new double[][] { { 25, 24, 40, 12, 33, 33 } }; String[] rowKeys = { "" }; String[] columnKeys = { "張三", "李四", "王五", "馬六", "陳七", "趙八" }; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); return dataset; } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!