要想繪製出漂亮的圖表,就必須瞭解圖表的構成部分,將圖表進行分解成N個部分。
而後再對每個部分進行渲染,設置樣式:包括背景色、輪廓線條樣式和顏色、填充顏色、字體大小、樣式、顏色。同時,須要確保在整個項目中,圖表的樣式風格總體統一,統一,和諧才能打造漂亮、乾淨、專業的外觀.java
美化後,Jfreechart圖形效果展現:數組
柱狀圖界面:dom
代碼:這是我封裝的工具類,而後繪製圖表就很是簡單了,並且美觀專業!ssh
package util; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Paint; import java.awt.Rectangle; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.DateTickUnit; import org.jfree.chart.axis.DateTickUnitType; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.block.BlockBorder; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.labels.StandardXYItemLabelGenerator; import org.jfree.chart.labels.StandardXYToolTipGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.DefaultDrawingSupplier; import org.jfree.chart.plot.PieLabelLinkStyle; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.Plot; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.renderer.category.StackedBarRenderer; import org.jfree.chart.renderer.category.StandardBarPainter; import org.jfree.chart.renderer.xy.StandardXYBarPainter; import org.jfree.chart.renderer.xy.XYBarRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.time.Day; import org.jfree.data.time.TimeSeries; import org.jfree.ui.RectangleInsets; import org.jfree.ui.TextAnchor; /** * Jfreechart工具類 * <p> * 解決中午亂碼問題<br> * 用來建立類別圖表數據集、建立餅圖數據集、時間序列圖數據集<br> * 用來對柱狀圖、折線圖、餅圖、堆積柱狀圖、時間序列圖的樣式進行渲染<br> * 設置X-Y座標軸樣式 * <p> * * * @author chenchangwen * @since:2014-2-18 * */ public class ChartUtils { private static String NO_DATA_MSG = "數據加載失敗"; private static Font FONT = new Font("宋體", Font.PLAIN, 12); public static Color[] CHART_COLORS = { new Color(31,129,188), new Color(92,92,97), new Color(144,237,125), new Color(255,188,117), new Color(153,158,255), new Color(255,117,153), new Color(253,236,109), new Color(128,133,232), new Color(158,90,102),new Color(255, 204, 102) };// 顏色 static { setChartTheme(); } public ChartUtils() { } /** * 中文主題樣式 解決亂碼 */ public static void setChartTheme() { // 設置中文主題樣式 解決亂碼 StandardChartTheme chartTheme = new StandardChartTheme("CN"); // 設置標題字體 chartTheme.setExtraLargeFont(FONT); // 設置圖例的字體 chartTheme.setRegularFont(FONT); // 設置軸向的字體 chartTheme.setLargeFont(FONT); chartTheme.setSmallFont(FONT); chartTheme.setTitlePaint(new Color(51, 51, 51)); chartTheme.setSubtitlePaint(new Color(85, 85, 85)); chartTheme.setLegendBackgroundPaint(Color.WHITE);// 設置標註 chartTheme.setLegendItemPaint(Color.BLACK);// chartTheme.setChartBackgroundPaint(Color.WHITE); // 繪製顏色繪製顏色.輪廓供應商 // paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE }; // 繪製器顏色源 DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE); chartTheme.setDrawingSupplier(drawingSupplier); chartTheme.setPlotBackgroundPaint(Color.WHITE);// 繪製區域 chartTheme.setPlotOutlinePaint(Color.WHITE);// 繪製區域外邊框 chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 連接標籤顏色 chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE); chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12)); chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X座標軸垂直網格顏色 chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y座標軸水平網格顏色 chartTheme.setBaselinePaint(Color.WHITE); chartTheme.setCrosshairPaint(Color.BLUE);// 不肯定含義 chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 座標軸標題文字顏色 chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度數字 chartTheme.setBarPainter(new StandardBarPainter());// 設置柱狀圖渲染 chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染 chartTheme.setItemLabelPaint(Color.black); chartTheme.setThermometerPaint(Color.white);// 溫度計 ChartFactory.setChartTheme(chartTheme); } /** * 必須設置文本抗鋸齒 */ public static void setAntiAlias(JFreeChart chart) { chart.setTextAntiAlias(false); } /** * 設置圖例無邊框,默認黑色邊框 */ public static void setLegendEmptyBorder(JFreeChart chart) { chart.getLegend().setFrame(new BlockBorder(Color.WHITE)); } /** * 建立類別數據集合 */ public static DefaultCategoryDataset createDefaultCategoryDataset(Vector<Serie> series, String[] categories) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Serie serie : series) { String name = serie.getName(); Vector<Object> data = serie.getData(); if (data != null && categories != null && data.size() == categories.length) { for (int index = 0; index < data.size(); index++) { String value = data.get(index) == null ? "" : data.get(index).toString(); if (isPercent(value)) { value = value.substring(0, value.length() - 1); } if (isNumber(value)) { dataset.setValue(Double.parseDouble(value), name, categories[index]); } } } } return dataset; } /** * 建立餅圖數據集合 */ public static DefaultPieDataset createDefaultPieDataset(String[] categories, Object[] datas) { DefaultPieDataset dataset = new DefaultPieDataset(); for (int i = 0; i < categories.length && categories != null; i++) { String value = datas[i].toString(); if (isPercent(value)) { value = value.substring(0, value.length() - 1); } if (isNumber(value)) { dataset.setValue(categories[i], Double.valueOf(value)); } } return dataset; } /** * 建立時間序列數據 * * @param category * 類別 * @param dateValues * 日期-值 數組 * @param xAxisTitle * X座標軸標題 * @return */ public static TimeSeries createTimeseries(String category, Vector<Object[]> dateValues) { TimeSeries timeseries = new TimeSeries(category); if (dateValues != null) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); for (Object[] objects : dateValues) { Date date = null; try { date = dateFormat.parse(objects[0].toString()); } catch (ParseException e) { } String sValue = objects[1].toString(); double dValue = 0; if (date != null && isNumber(sValue)) { dValue = Double.parseDouble(sValue); timeseries.add(new Day(date), dValue); } } } return timeseries; } /** * 設置 折線圖樣式 * * @param plot * @param isShowDataLabels * 是否顯示數據標籤 默認不顯示節點形狀 */ public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels) { setLineRender(plot, isShowDataLabels, false); } /** * 設置折線圖樣式 * * @param plot * @param isShowDataLabels * 是否顯示數據標籤 */ public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels, boolean isShapesVisible) { plot.setNoDataMessage(NO_DATA_MSG); plot.setInsets(new RectangleInsets(10, 10, 0, 10), false); LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setStroke(new BasicStroke(1.5F)); if (isShowDataLabels) { renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance())); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi } renderer.setBaseShapesVisible(isShapesVisible);// 數據點繪製形狀 setXAixs(plot); setYAixs(plot); } /** * 設置時間序列圖樣式 * * @param plot * @param isShowData * 是否顯示數據 * @param isShapesVisible * 是否顯示數據節點形狀 */ public static void setTimeSeriesRender(Plot plot, boolean isShowData, boolean isShapesVisible) { XYPlot xyplot = (XYPlot) plot; xyplot.setNoDataMessage(NO_DATA_MSG); xyplot.setInsets(new RectangleInsets(10, 10, 5, 10)); XYLineAndShapeRenderer xyRenderer = (XYLineAndShapeRenderer) xyplot.getRenderer(); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyRenderer.setBaseShapesVisible(false); if (isShowData) { xyRenderer.setBaseItemLabelsVisible(true); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi } xyRenderer.setBaseShapesVisible(isShapesVisible);// 數據點繪製形狀 DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis(); domainAxis.setAutoTickUnitSelection(false); DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 1, new SimpleDateFormat("yyyy-MM")); // 第二個參數是時間軸間距 domainAxis.setTickUnit(dateTickUnit); StandardXYToolTipGenerator xyTooltipGenerator = new StandardXYToolTipGenerator("{1}:{2}", new SimpleDateFormat("yyyy-MM-dd"), new DecimalFormat("0")); xyRenderer.setBaseToolTipGenerator(xyTooltipGenerator); setXY_XAixs(xyplot); setXY_YAixs(xyplot); } /** * 設置時間序列圖樣式 -默認不顯示數據節點形狀 * * @param plot * @param isShowData * 是否顯示數據 */ public static void setTimeSeriesRender(Plot plot, boolean isShowData) { setTimeSeriesRender(plot, isShowData, false); } /** * 設置時間序列圖渲染:可是存在一個問題:若是timeseries裏面的日期是按照天組織, 那麼柱子的寬度會很是小,和直線同樣粗細 * * @param plot * @param isShowDataLabels */ public static void setTimeSeriesBarRender(Plot plot, boolean isShowDataLabels) { XYPlot xyplot = (XYPlot) plot; xyplot.setNoDataMessage(NO_DATA_MSG); XYBarRenderer xyRenderer = new XYBarRenderer(0.1D); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); if (isShowDataLabels) { xyRenderer.setBaseItemLabelsVisible(true); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); } StandardXYToolTipGenerator xyTooltipGenerator = new StandardXYToolTipGenerator("{1}:{2}", new SimpleDateFormat("yyyy-MM-dd"), new DecimalFormat("0")); xyRenderer.setBaseToolTipGenerator(xyTooltipGenerator); setXY_XAixs(xyplot); setXY_YAixs(xyplot); } /** * 設置柱狀圖渲染 * * @param plot * @param isShowDataLabels */ public static void setBarRenderer(CategoryPlot plot, boolean isShowDataLabels) { plot.setNoDataMessage(NO_DATA_MSG); plot.setInsets(new RectangleInsets(10, 10, 5, 10)); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setMaximumBarWidth(0.075);// 設置柱子最大寬度 if (isShowDataLabels) { renderer.setBaseItemLabelsVisible(true); } setXAixs(plot); setYAixs(plot); } /** * 設置堆積柱狀圖渲染 * * @param plot */ public static void setStackBarRender(CategoryPlot plot) { plot.setNoDataMessage(NO_DATA_MSG); plot.setInsets(new RectangleInsets(10, 10, 5, 10)); StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); plot.setRenderer(renderer); setXAixs(plot); setYAixs(plot); } /** * 設置類別圖表(CategoryPlot) X座標軸線條顏色和樣式 * * @param axis */ public static void setXAixs(CategoryPlot plot) { Color lineColor = new Color(31, 121, 170); plot.getDomainAxis().setAxisLinePaint(lineColor);// X座標軸顏色 plot.getDomainAxis().setTickMarkPaint(lineColor);// X座標軸標記|豎線顏色 } /** * 設置類別圖表(CategoryPlot) Y座標軸線條顏色和樣式 同時防止數據沒法顯示 * * @param axis */ public static void setYAixs(CategoryPlot plot) { Color lineColor = new Color(192, 208, 224); ValueAxis axis = plot.getRangeAxis(); axis.setAxisLinePaint(lineColor);// Y座標軸顏色 axis.setTickMarkPaint(lineColor);// Y座標軸標記|豎線顏色 // 隱藏Y刻度 axis.setAxisLineVisible(false); axis.setTickMarksVisible(false); // Y軸網格線條 plot.setRangeGridlinePaint(new Color(192, 192, 192)); plot.setRangeGridlineStroke(new BasicStroke(1)); plot.getRangeAxis().setUpperMargin(0.1);// 設置頂部Y座標軸間距,防止數據沒法顯示 plot.getRangeAxis().setLowerMargin(0.1);// 設置底部Y座標軸間距 } /** * 設置XY圖表(XYPlot) X座標軸線條顏色和樣式 * * @param axis */ public static void setXY_XAixs(XYPlot plot) { Color lineColor = new Color(31, 121, 170); plot.getDomainAxis().setAxisLinePaint(lineColor);// X座標軸顏色 plot.getDomainAxis().setTickMarkPaint(lineColor);// X座標軸標記|豎線顏色 } /** * 設置XY圖表(XYPlot) Y座標軸線條顏色和樣式 同時防止數據沒法顯示 * * @param axis */ public static void setXY_YAixs(XYPlot plot) { Color lineColor = new Color(192, 208, 224); ValueAxis axis = plot.getRangeAxis(); axis.setAxisLinePaint(lineColor);// X座標軸顏色 axis.setTickMarkPaint(lineColor);// X座標軸標記|豎線顏色 // 隱藏Y刻度 axis.setAxisLineVisible(false); axis.setTickMarksVisible(false); // Y軸網格線條 plot.setRangeGridlinePaint(new Color(192, 192, 192)); plot.setRangeGridlineStroke(new BasicStroke(1)); plot.setDomainGridlinesVisible(false); plot.getRangeAxis().setUpperMargin(0.12);// 設置頂部Y座標軸間距,防止數據沒法顯示 plot.getRangeAxis().setLowerMargin(0.12);// 設置底部Y座標軸間距 } /** * 設置餅狀圖渲染 */ public static void setPieRender(Plot plot) { plot.setNoDataMessage(NO_DATA_MSG); plot.setInsets(new RectangleInsets(10, 10, 5, 10)); PiePlot piePlot = (PiePlot) plot; piePlot.setInsets(new RectangleInsets(0, 0, 0, 0)); piePlot.setCircular(true);// 圓形 // piePlot.setSimpleLabels(true);// 簡單標籤 piePlot.setLabelGap(0.01); piePlot.setInteriorGap(0.05D); piePlot.setLegendItemShape(new Rectangle(10, 10));// 圖例形狀 piePlot.setIgnoreNullValues(true); piePlot.setLabelBackgroundPaint(null);// 去掉背景色 piePlot.setLabelShadowPaint(null);// 去掉陰影 piePlot.setLabelOutlinePaint(null);// 去掉邊框 piePlot.setShadowPaint(null); // 0:category 1:value:2 :percentage piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}"));// 顯示標籤數據 } /** * 是否是一個%形式的百分比 * * @param str * @return */ public static boolean isPercent(String str) { return str != null ? str.endsWith("%") && isNumber(str.substring(0, str.length() - 1)) : false; } /** * 是否是一個數字 * * @param str * @return */ public static boolean isNumber(String str) { return str != null ? str.matches("^[-+]?(([0-9]+)((([.]{0})([0-9]*))|(([.]{1})([0-9]+))))$") : false; } }
Serie工具
package util; import java.io.Serializable; import java.util.Vector; /** * 系列:名字和數據集合 構成一條曲線</br> 能夠將serie看做一根線或者一根柱子: * * <p> * 參照JS圖表來描述數據:</br> series: [{ name: 'Tokyo', data: [7.0, 6.9, 9.5, 14.5] * },</br> { name: 'New York', data: [-0.2, 0.8, 5.7, 11.3} ]</br> * </p> * * @author ccw * @date 2014-6-4 */ public class Serie implements Serializable { private static final long serialVersionUID = 1L; private String name;// 名字 private Vector<Object> data;// 數據值 public Serie() { } /** * * @param name * 名稱(線條名稱) * @param data * 數據(線條上的全部數據值) */ public Serie(String name, Vector<Object> data) { this.name = name; this.data = data; } /** * * @param name * 名稱(線條名稱) * @param array * 數據(線條上的全部數據值) */ public Serie(String name, Object[] array) { this.name = name; if (array != null) { data = new Vector<Object>(array.length); for (int i = 0; i < array.length; i++) { data.add(array[i]); } } } public String getName() { return name; } public void setName(String name) { this.name = name; } public Vector<Object> getData() { return data; } public void setData(Vector<Object> data) { this.data = data; } }
在來看看Highchart圖表,對比一下,有木有以爲很像啊!字體
有木有以爲swing和Java2D很強大啊,學好swing,須要想象力!ui