jCharts:Java圖表類庫使用介紹
本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃!
jCharts是一款基於Java的圖表繪製類庫,jCharts包含了多種圖表格式,包括線型圖、餅圖、柱形圖和點圖等。
jCharts的特點
- 100%基於Java,繼承其跨平臺的特性。
- 圖表中的文字,包括x軸和y軸上的文字,均可以設定字型。
- 圖表資料構造非常簡單。
jCharts使用案例
柱形圖繪製
完整的Java程式碼:
package com.jiuxing.mobile; import java.awt.Color; import java.awt.Font; import java.awt.Paint; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.krysalis.jcharts.axisChart.AxisChart; import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition; import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer; import org.krysalis.jcharts.chartData.AxisChartDataSet; import org.krysalis.jcharts.chartData.DataSeries; import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries; import org.krysalis.jcharts.encoders.ServletEncoderHelper; import org.krysalis.jcharts.properties.AxisProperties; import org.krysalis.jcharts.properties.BarChartProperties; import org.krysalis.jcharts.properties.ChartProperties; import org.krysalis.jcharts.properties.DataAxisProperties; import org.krysalis.jcharts.properties.LegendProperties; import org.krysalis.jcharts.properties.PropertyException; import org.krysalis.jcharts.properties.util.ChartFont; import org.krysalis.jcharts.types.ChartType; public class BarChartServlet extends HttpServlet { // ---all of my charts serviced by this Servlet will have the same properties. private BarChartProperties barChartProperties; // ---all of my charts serviced by this Servlet will have the same properties. protected LegendProperties legendProperties; protected AxisProperties axisProperties; protected ChartProperties chartProperties; protected int width = 950; protected int height = 360; /********************************************************************************************** * **********************************************************************************************/ public void init() { this.legendProperties = new LegendProperties(); this.chartProperties = new ChartProperties(); // 圖形的XY軸的屬性物件 this.axisProperties = new AxisProperties(false); // 圖表橫座標和縱座標範圍的字型,大小,顏色設定物件 ChartFont axisScaleFont = new ChartFont(new Font( "Georgia Negreta cursiva", Font.PLAIN, 13), new Color(18, 189, 255)); axisProperties.getXAxisProperties().setScaleChartFont(axisScaleFont); axisProperties.getYAxisProperties().setScaleChartFont(axisScaleFont); // Bar圖形的橫座標和縱座標的標題字型和顏色 ,大小的設定物件 ChartFont axisTitleFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 14), new Color(18, 189, 255)); axisProperties.getXAxisProperties().setTitleChartFont(axisTitleFont); axisProperties.getXAxisProperties().setShowEndBorder(false); axisProperties.getYAxisProperties().setTitleChartFont(axisTitleFont); axisProperties.getYAxisProperties().setShowEndBorder(false); DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties .getYAxisProperties(); try { // 設定使用者定義的縱軸縱座標的 間隔範圍 dataAxisProperties.setUserDefinedScale(0, 3000); } catch (PropertyException propertyException) { propertyException.printStackTrace(); } dataAxisProperties.setRoundToNearest(3); // 設定圖形標題的字型,顏色,大小 ChartFont titleFont = new ChartFont(new Font("Georgia Negreta cursiva", Font.PLAIN, 14), new Color(18, 189, 255)); // 生成影像的屬性物件 this.chartProperties.setTitleFont(titleFont); // Bar圖形的屬性類 this.barChartProperties = new BarChartProperties(); ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer(false, false, true, -1); valueLabelRenderer.setValueLabelPosition(ValueLabelPosition.ON_TOP); // 是否設定顯示的縱座標標籤垂直,true為是,flase為水平 valueLabelRenderer.useVerticalLabels(true); barChartProperties.addPostRenderEventListener(valueLabelRenderer); } /********************************************************************************************** * **********************************************************************************************/ public void service(HttpServletRequest req, HttpServletResponse httpServletResponse) throws ServletException, IOException { try { // 設定橫座標標籤 String[] xAxisLabels = { "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }; // 設定橫座標的單位 String xAxisTitle = "Years"; // 設定縱座標的標題 String yAxisTitle = "Problems"; // 設定圖形的標題 String title = "Micro$oft At Work"; // 圖形所需要的資料物件 IAxisDataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle, yAxisTitle, title); // 設定條形資料 double[][] data = new double[][] { { 1500, 6880, 4510, 2600, 0, 1580, 0, 9555, 11000, 0, 7500, 5880, 3510, 2600, 1200, 1580, 9000, 8555, 9000, 3120 } }; // 條形區域 形示的標籤 String[] legendLabels = { "8" }; // String[] legendLabels = null; // 條形區域繪製的顏色設定物件 Paint[] paints = new Paint[] { new Color(18, 189, 255) }; dataSeries.addIAxisPlotDataSet(new AxisChartDataSet(data, legendLabels, paints, ChartType.BAR, this.barChartProperties)); // 產生一個 chart物件 AxisChart axisChart = new AxisChart(dataSeries, this.chartProperties, this.axisProperties, this.legendProperties, this.width, this.height); // 輸出設定好的chart圖形 ServletEncoderHelper.encodeJPEG13(axisChart, 1.0f, httpServletResponse); } catch (Throwable throwable) { // HACK do your error handling here... throwable.printStackTrace(); } } }
效果圖如下:
總體來說,jCharts可以完成基本的圖表繪製,並可以自己擴充套件程式碼來完成更高階的圖表和報表功能。
本文連結:http://www.codeceo.com/article/jcharts-java.html
本文作者:碼農網 – 小峰
[ 原創作品,轉載必須在正文中標註並保留原文連結和作者等資訊。]
相關文章
- Java智慧圖表類庫JChartLib使用介紹Java
- java集合類介紹Java
- JFreeChart:多功能Java圖表類庫Java
- java之Object類介紹JavaObject
- 基於SWT的Java圖表類庫SWTChartJava
- ECharts資料圖表使用介紹 超詳細Echarts
- http代理使用分類介紹HTTP
- Excel圖表製作方法介紹Excel
- Java併發程式設計:Thread類的使用介紹Java程式設計thread
- java ShutdownHook介紹與使用JavaHook
- PHP中 ADOdb 類庫介紹(轉)PHP
- UML類圖介紹&類的六大關係
- Java中的AI庫大全介紹JavaAI
- Java Selenide 介紹&使用JavaIDE
- Java中的13個原子操作類介紹Java
- 簡單介紹C#使用物件序列化類庫MessasgePackC#物件
- Google guava工具類的介紹和使用GoGuava
- gfx-rs/hal跨平臺圖形抽象庫使用介紹抽象
- 樸素貝葉斯分類流程圖介紹流程圖
- MySQL表與表之間的SQL Joins圖介紹MySql
- java.util.Arrays 快速使用介紹Java
- Java介紹Java
- 基於xmpp openfire smack開發之smack類庫介紹和使用[2]Mac
- TypeScript class 類介紹TypeScript
- QTime類介紹QT
- Java版ECharts圖表庫ECharts-JavaJavaEcharts
- Java課堂 自我介紹思維導圖Java
- domutils 工具庫的使用方法介紹
- Java Base64編碼使用介紹Java
- JAVA String.format 方法使用介紹JavaORM
- Guava庫介紹Guava
- 介紹oracle外部表Oracle
- Android 開發:使用繪製基金圖表類(帶快取的圖表類)Android快取
- percona-toolkit效能類工具的使用介紹
- 前端的圖表繪製框架Konva-基本介紹前端框架
- python類的介紹Python
- ASIHTTPRequest類庫簡介和使用說明HTTP
- 【原創】開源Math.NET基礎數學類庫使用(01)綜合介紹