Java圖表類庫使用介紹:jCharts

jopen 10年前發布 | 17K 次閱讀 jCharts 圖表/報表制作

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();  
    }  
    }  
}

效果圖如下:

Java圖表類庫使用介紹:jCharts

總體來說,jCharts可以完成基本的圖表繪制,并可以自己擴展代碼來完成更高級的圖表和報表功能。

本文鏈接:http://www.codeceo.com/article/jcharts-java.html
本文作者:碼農網 – 小峰

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!