`
编程足球
  • 浏览: 256938 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

报表 charts4j

阅读更多
1. 简介
charts4j is a Java chart library that enables developers to programmatically generate nearly all 1 the charts available in the Google Chart API . charts4j can be incorporated into any Internet enabled Java (e.g. Swing) or web application environment (e.g. JSPs / Servlets, Spring MVC, GWT, etc.)

Installation

Developers only need to include the charts4j.jar in their classpath. It is available in the charts4j.zip file. The application must also be connected to the Internet because the charts are ultimately rendered by the Google Chart API server
(上面copy于charts4j 1.3API)

Features
Supports many features of the Google Chart Tools
Hides the ugly details of creating the URL parameters that are necessary for communicating with the Google Chart Tools
Can be incorporated into any Internet enabled Swing or web application environment (JSP/Servlet, GWT, Spring MVC, etc.)
100% pure core Java solution. No need for special graphics libraries, etc.
Super-scalable & Lightweight. Only one 160Kb jar and an Internet connection required
Well documented
GWT client-side support.
JFreeChart Eastwood Servlet integration.
Works on Android
Best of all, it is FREE!
(上面来至官网).
http://code.google.com/p/charts4j/

[color=red]1. 例子(都在官网中下载的jar包中有这些example)[/color
1. LineChart
/**
 * The MIT License
 * Copyright (c) 2011 the original author or authors.
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jspsmart.charts4j;
import static com.googlecode.charts4j.Color.*;
import java.util.Arrays;
import org.junit.Test;
import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.DataUtil;
import com.googlecode.charts4j.Fills;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.Line;
import com.googlecode.charts4j.LineChart;
import com.googlecode.charts4j.LineStyle;
import com.googlecode.charts4j.LinearGradientFill;
import com.googlecode.charts4j.Plots;
import com.googlecode.charts4j.Shape;

/**
 *
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class LineChartExample {
    @Test
    public void example1() {
        // Defining lines
        final int NUM_POINTS = 25; // 定义点的个数
        final double[] competition = new double[NUM_POINTS];
        final double[] mywebsite = new double[NUM_POINTS];
        for (int i = 0; i < NUM_POINTS; i++) {
            competition[i] = 100-(Math.cos(30*i*Math.PI/180)*10 + 50)*i/20;
            mywebsite[i] = (Math.cos(30*i*Math.PI/180)*10 + 50)*i/20;
        }
        
//        public static Line newLine(Data data,Color color,String legend)
//        data - Data for this line. Cannot be null.   	数据源
//        color - Line color. Cannot be null.			标题盘指定的颜色颜色
//        legend - Legend for this line. Cannot be null.标题
        Color line1Color = Color.YELLOW;
        Line line1 = Plots.newLine(Data.newData(mywebsite), line1Color, "Line1 MARK");
        
//        第一个参数 lineThickness - Thickness of line. Must be > 0. 线条厚度
//        第二个参数 lengthOfLineSegment - Length of line segment. Must be >= 0. 每个线条长度
//        第三个参数 lengthOfBlankSegment - Length of blank segment. Must be >= 0.  线条间隙
        line1.setLineStyle(LineStyle.newLineStyle(2, 1, 1));
        
//        shape - Arrows, Diamonds, etc. Cannot be null.  形状
//        color - Color of the shape maker. Cannot be null. 颜色
//        size - The size of the shape marker. Must be > 0. 大小
        line1.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 4);
        line1.addShapeMarkers(Shape.DIAMOND, line1Color, 8);
        
        Color line2Color = Color.SKYBLUE;
        Line line2 = Plots.newLine(Data.newData(competition), line2Color, "Line2 MARK");
        line2.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
        line2.addShapeMarkers(Shape.DIAMOND, BLACK, 5);
        line2.addShapeMarkers(Shape.CIRCLE, line2Color, 10);


        // Defining chart.
        LineChart chart = GCharts.newLineChart(line1, line2);
        
//        地​​图以外的所有图表的可能最大面积是30万像素。
//        width - chart width. Must be > 0 and <= 1000.  长
//        height - chart height. Must be > 0 and <= 1000. 高
        chart.setSize(600, 500);
        
//        title - Chart title. Cannot be null.			标题
//        color - Chart title color. Cannot be null.	颜色
//        fontSize - Chart title font size. Must be > 0.大小
        chart.setTitle("Web Traffic|(in billions of hits)", WHITE, 14);
        
//        水平范围标记   
//        startPoint - the position on the y-axis at which the range starts where 0 is the bottom and 100 is the top.
//        endPoint - the position on the y-axis at which the range ends where 0 is the bottom and 100 is the top.
//        color - color of range marker. Cannot be null.
        chart.addHorizontalRangeMarker(40, 60, Color.newColor(RED, 30));
        chart.addVerticalRangeMarker(70, 90, Color.newColor(GREEN, 30));
        
//      xAxisStepSize - x step size. must be > 0.  x轴之间的间隙长度(百分比)
//      yAxisStepSize - y step size. must be > 0.	 y轴每条线之间的间隙长度	(百分比)
//      lengthOfLineSegment - length of line segment. must be >= 0.  每条黑线的长度
//      lengthOfBlankSegment - length of blank segment. must be >= 0. 每条线之间的间隔
        chart.setGrid(30, 10, 3, 2);

        // Defining axis info and styles  Axis 轴
        
//        textColor - color of text displayed along the axis. Cannot be null.	文本颜色
//        fontSize - font size of text displayed along the axis. Must be > 0.	文本大小
//        alignment - alignment of text along the axis with respect to the axis tick marks. Only works for horizontal axes. Cannot be null. 对齐方式
        AxisStyle axisStyle = AxisStyle.newAxisStyle(WHITE, 12, AxisTextAlignment.CENTER);
        
//        Labels that will be displayed along side the axis.	x轴显示标签
        AxisLabels xAxis = AxisLabelsFactory.newAxisLabels("Nov", "Dec", "Jan", "Feb", "Mar");
        xAxis.setAxisStyle(axisStyle);  // 添加标签样式
        
        AxisLabels xAxis2 = AxisLabelsFactory.newAxisLabels("2007", "2008", "2009", "2010", "2011");
        xAxis2.setAxisStyle(axisStyle);
        
        AxisLabels xAxis3 = AxisLabelsFactory.newAxisLabels("Month", 50.0);
        xAxis3.setAxisStyle(AxisStyle.newAxisStyle(Color.BLUE, 14, AxisTextAlignment.CENTER));

        AxisLabels yAxis = AxisLabelsFactory.newAxisLabels("0", "25", "50", "75", "100");
        yAxis.setAxisStyle(axisStyle);

        AxisLabels yAxis2 = AxisLabelsFactory.newAxisLabels("Hits", 50.0);
        yAxis2.setAxisStyle(AxisStyle.newAxisStyle(Color.RED, 14, AxisTextAlignment.CENTER));
        yAxis2.setAxisStyle(axisStyle);

        // Adding axis info to chart.
        chart.addXAxisLabels(xAxis);
        chart.addXAxisLabels(xAxis2);
        chart.addXAxisLabels(xAxis3);
        chart.addYAxisLabels(yAxis);
        chart.addYAxisLabels(yAxis2);

        // Defining background and chart fills. 背景颜色
        chart.setBackgroundFill(Fills.newSolidFill(Color.newColor("1F1D1D")));
        
        // 数据域颜色
        LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.newColor("363433"), 100);
        fill.addColorAndOffset(Color.BLUE, 0);
        chart.setAreaFill(fill);
        String url = chart.toURLString();
     
        // EXAMPLE CODE END. Use this url string in your web or
        System.out.println(url);
    }

    @Test
    public void example2() {
        // EXAMPLE CODE START
        // Defining Line
        final double[] sp500 = { 62.960, 74.560, 84.300, 92.200, 95.890, 
        							103.800, 91.600, 92.270, 96.870, 116.930, 
        							97.540, 67.160, 89.770, 106.880, 94.750, 
        							96.280, 107.840, 135.330, 122.300, 140.340, 
        							164.860, 166.260, 210.680, 243.370,247.840, 
        							277.080, 350.680, 328.710, 415.140, 438.820, 
        							468.660, 460.920, 614.120, 753.850, 970.840, 
        							1231.93, 1464.47, 1334.22, 1161.02, 879.390, 
        							1109.64, 1213.55, 1258.17, 1424.71, 1475.25 };
        
        final double INFLATION = 0.035;
        final double[] inflation = new double[sp500.length];
        inflation[0] = sp500[0];
        for (int i = 1; i < inflation.length; i++) {
            inflation[i] = inflation[i-1] *INFLATION + inflation[i-1];
        }

//         Line line1 = Plots.newLine(Data.newData(sp500), YELLOW, "S & P 500");
        Line line1 = Plots.newLine(DataUtil.scaleWithinRange(0,1500,sp500), YELLOW, "S & P 500");
        line1.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
        line1.addShapeMarkers(Shape.CIRCLE, YELLOW, 10);
        line1.addShapeMarkers(Shape.CIRCLE, BLACK, 7);
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,8);
        line1.addShapeMarker(Shape.HORIZONTAL_LINE, BLUE,3,17);
        line1.addShapeMarker(Shape.VERTICAL_LINE_FULL, BLUE,3,24);
        line1.addShapeMarker(Shape.VERTICAL_LINE_PARTIAL, BLUE,3,40);
        
        line1.setFillAreaColor(LIGHTYELLOW);

        Line line2 = Plots.newLine(DataUtil.scaleWithinRange(0,1500,inflation), LIMEGREEN, "Inflation");
        line2.setLineStyle(LineStyle.newLineStyle(3, 1, 0));
        
        //	可以使用addShapeMarkers进行多层迭代 一个点可以有多种形状和颜色叠加
        line2.addShapeMarkers(Shape.CIRCLE, Color.RED, 10);
        line2.addShapeMarkers(Shape.CIRCLE, Color.BLACK, 7);
        line2.addShapeMarkers(Shape.CIRCLE, Color.YELLOW, 4);
        line2.setFillAreaColor(LIGHTGREEN);


        // Defining chart.
        LineChart chart = GCharts.newLineChart(line1,line2);
        chart.setSize(600, 450);
        chart.setTitle("S & P 500|1962 - 2008", WHITE, 14);

        // Defining axis info and styles
        AxisStyle axisStyle = AxisStyle.newAxisStyle(WHITE, 12, AxisTextAlignment.CENTER);
        AxisLabels yAxis = AxisLabelsFactory.newNumericRangeAxisLabels(0, sp500[sp500.length-1]);
        yAxis.setAxisStyle(axisStyle);
        AxisLabels xAxis1 = AxisLabelsFactory.newAxisLabels(Arrays.asList("Fed Chiefs:","Burns","Volcker","Greenspan","Bernanke"), Arrays.asList(5,18,39,55,92));
        xAxis1.setAxisStyle(axisStyle);
        AxisLabels xAxis2 = AxisLabelsFactory.newNumericRangeAxisLabels(1962, 2008);
        xAxis2.setAxisStyle(axisStyle);
        AxisLabels xAxis3 = AxisLabelsFactory.newAxisLabels("Year", 50.0);
        xAxis3.setAxisStyle(AxisStyle.newAxisStyle(WHITE, 14, AxisTextAlignment.CENTER));

        // Adding axis info to chart.
        chart.addYAxisLabels(yAxis);
        chart.addXAxisLabels(xAxis1);
        chart.addXAxisLabels(xAxis2);
        chart.addXAxisLabels(xAxis3);
        chart.setGrid(100, 6.78, 5, 0);

        // Defining background and chart fills.
        chart.setBackgroundFill(Fills.newSolidFill(BLACK));
        chart.setAreaFill(Fills.newSolidFill(Color.newColor("708090")));
        String url = chart.toURLString();
        
        // EXAMPLE CODE END. Use this url string in your web or Internet application.
        System.out.println(url);
    }

}

根据上面生成的url
example1 的效果图如下

http://chart.apis.google.com/chart?cht=lc&chs=600x500&chts=FFFFFF,14&chxt=y,y,x,x,x&chls=2,1,1|3,1,0&chg=30.0,10.0,3,2&chco=FFFF00,87CEEB&chxp=1,50.0|4,50.0&chdl=Line1+MARK|Line2+MARK&chxr=1,0.0,100.0|4,0.0,100.0&chtt=Web+Traffic%7C%28in+billions+of+hits%29&chf=bg,s,1F1D1D|c,lg,0,363433,1.0,0000FF,0.0&chxs=0,FFFFFF,12,0|1,FFFFFF,12,0|2,FFFFFF,12,0|3,FFFFFF,12,0|4,0000FF,14,0&chxl=0:|0|25|50|75|100|1:|Hits|2:|Nov|Dec|Jan|Feb|Mar|3:|2007|2008|2009|2010|2011|4:|Month&chd=e:AAB4DhEzFxGnHrJRLhOZRmUpXCYZYpYAXCWfXCZIczhmmtrKuE,..-H8e7M6O5Y4U2u0exmuZrWo9nmnWn.o9pgo9m3jMeZZSU1R7&chm=r,FF00004C,0,0.40,0.60|R,0080004C,0,0.70,0.90|d,FFFFFF,0,-1,4,0|d,FFFF00,0,-1,8,0|d,000000,1,-1,5,0|o,87CEEB,1,-1,10,0


example2 的效果图如下
http://chart.apis.google.com/chart?cht=lc&chs=600x450&chxt=y,x,x,x&chts=FFFFFF,14&chls=3,1,0|3,1,0&chco=FFFF00,32CD32&chg=100.0,6.78,5,0&chdl=S+%26+P+500|Inflation&chf=bg,s,000000|c,s,708090&chxp=1,5,18,39,55,92|3,50.0&chtt=S+%26+P+500%7C1962+-+2008&chxr=0,0.0,1475.25|1,0.0,100.0|2,1962.0,2008.0|3,0.0,100.0&chxs=0,FFFFFF,12,0|1,FFFFFF,12,0|2,FFFFFF,12,0|3,FFFFFF,14,0&chxl=1:|Fed+Chiefs%3A|Burns|Volcker|Greenspan|Bernanke|3:|Year&chd=e:CsDMDmD8EGEbD6D8EIE.EKC3D1EkEDEHEmFxFOF.HCHGI.KYKlL0O9OBRtSuT.TqaNgKpa0j-e46xilhvVzx1r8x-7,CsCyC4C.DFDMDTDbDiDqDyD7EEENEWEgEqE0E.FKFWFiFuF7GIGWGkGzHCHSHiHzIFIXIqI9JRJmJ7KSKpLALZLzMN&chm=o,FFFF00,0,-1,10,0|o,000000,0,-1,7,0|v,0000FF,0,8,3,0|h,0000FF,0,17,3,0|V,0000FF,0,24,3,0|v,0000FF,0,40,3,0|B,FFFFE0,0,0,0|o,FF0000,1,-1,10,0|o,000000,1,-1,7,0|o,FFFF00,1,-1,4,0|B,90EE90,1,0,0

2.BarChart


/**
 *
 * The MIT License
 *
 * Copyright (c) 2011 the original author or authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jspsmart.charts4j;
import static com.googlecode.charts4j.Color.*;
import static com.googlecode.charts4j.UrlUtil.normalize;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.BeforeClass;
import org.junit.Test;

import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.BarChart;
import com.googlecode.charts4j.BarChartPlot;
import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.DataUtil;
import com.googlecode.charts4j.Fills;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.LinearGradientFill;
import com.googlecode.charts4j.Plots;

/**
 *
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class BarChartExample {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.ALL);
    }

    @Test
    public void example1() {
        // EXAMPLE CODE START
        // Defining data plots.
        BarChartPlot team1 = Plots.newBarChartPlot(Data.newData(25, 43, 12, 30), BLUEVIOLET, "Team A");
        BarChartPlot team2 = Plots.newBarChartPlot(Data.newData(8, 35, 11, 5), ORANGERED, "Team B");
        BarChartPlot team3 = Plots.newBarChartPlot(Data.newData(10, 20, 30, 30), LIMEGREEN, "Team C");

        // Instantiating chart.
        BarChart chart = GCharts.newBarChart(team1, team2, team3);

        // Defining axis info and styles
        AxisStyle axisStyle = AxisStyle.newAxisStyle(Color.RED, 13, AxisTextAlignment.CENTER);
        AxisLabels score = AxisLabelsFactory.newAxisLabels("Score", 50.0);
        score.setAxisStyle(axisStyle);
        AxisLabels year = AxisLabelsFactory.newAxisLabels("Year", 50.0);
        year.setAxisStyle(axisStyle);

        // Adding axis info to chart.  添加坐标轴信息
        chart.addXAxisLabels(AxisLabelsFactory.newAxisLabels("2002", "2003", "2004", "2005"));
        chart.addYAxisLabels(AxisLabelsFactory.newNumericRangeAxisLabels(0, 200,20));
        chart.addYAxisLabels(score);
        chart.addXAxisLabels(year);

        chart.setSize(600, 450);
        
//        Set the bar chart width.每条的宽度
        chart.setBarWidth(50);
        
//        Set the space within groups of bars.空格间隙
        chart.setSpaceWithinGroupsOfBars(20);
        
//        Are the bar charts stacked, or side-by-side.  叠加 还是在旁边
        chart.setDataStacked(true);
        
        chart.setTitle("Team Scores", BLACK, 20);
        
//        xAxisStepSize - x step size. must be > 0.  x轴之间的间隙长度
//        yAxisStepSize - y step size. must be > 0.	 y轴每条线之间的间隙长度	
//        lengthOfLineSegment - length of line segment. must be >= 0.  每条黑线的长度
//        lengthOfBlankSegment - length of blank segment. must be >= 0. 每条线之间的间隔
        chart.setGrid(10, 20, 3, 2);
        chart.setBackgroundFill(Fills.newSolidFill(ALICEBLUE));
        LinearGradientFill fill = Fills.newLinearGradientFill(0, LAVENDER, 100);
        fill.addColorAndOffset(WHITE, 0);
        chart.setAreaFill(fill);
        String url = chart.toURLString();
        // EXAMPLE CODE END. Use this url string in your web or Internet application.
	        System.out.println(url);
    }

    @Test
    public void example2() {
        // EXAMPLE CODE START
        // Defining data series.
        final int MAX_MEDALS = 51;	// 最大值
        
//        min - The minimum value in the scaling range.
//        max - The maximum value in the scaling range.
//        data - The data
        Data goldData= DataUtil.scaleWithinRange(0, MAX_MEDALS, Arrays.asList(MAX_MEDALS, 36, 23, 19, 16));
        Data silverData= DataUtil.scaleWithinRange(0, MAX_MEDALS, Arrays.asList(21, 38, 21, 13, 10));
        Data bronzeData= DataUtil.scaleWithinRange(0, MAX_MEDALS, Arrays.asList(28, 36, 28, 15, 15));
        
        BarChartPlot gold = Plots.newBarChartPlot(goldData, GOLD, "First LGF");
        BarChartPlot silver = Plots.newBarChartPlot(silverData, SILVER, "Second HQY");
        BarChartPlot bronze = Plots.newBarChartPlot(bronzeData, Color.BLACK, "Three CD");
        
        // 定义表格
        BarChart chart = GCharts.newBarChart(gold, silver,  bronze);

        // Defining axis info and styles  定义坐标数据和表格
        AxisStyle axisStyle = AxisStyle.newAxisStyle(BLACK, 13, AxisTextAlignment.CENTER);
       
        AxisLabels country = AxisLabelsFactory.newAxisLabels("Country", 50.0);
        country.setAxisStyle(axisStyle);
        
        AxisLabels countries = AxisLabelsFactory.newAxisLabels("Germany", "United Kingdom", "Russia", "USA", "China");
        countries.setAxisStyle(axisStyle);
        
        AxisLabels medals = AxisLabelsFactory.newAxisLabels("Medals", 50.0);
        medals.setAxisStyle(axisStyle);
        
        AxisLabels medalCount = AxisLabelsFactory.newNumericRangeAxisLabels(0, MAX_MEDALS,5);
        medalCount.setAxisStyle(axisStyle);


        // Adding axis info to chart.
        chart.addXAxisLabels(medalCount);
        chart.addXAxisLabels(medals);
        chart.addYAxisLabels(countries);
        chart.addYAxisLabels(country);
        chart.addTopAxisLabels(medalCount);
        
        chart.setHorizontal(true);
        chart.setSize(450, 650);
        chart.setSpaceBetweenGroupsOfBars(30);

        chart.setTitle("2008 Beijing Olympics Medal Count", BLACK, 16);
        
        ///51 is the max number of medals.
        chart.setGrid((50.0/MAX_MEDALS)*20, 600, 3, 2);
        chart.setBackgroundFill(Fills.newSolidFill(LIGHTGREY));
        LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.newColor("E37600"), 100);
        fill.addColorAndOffset(Color.newColor("DC4800"), 0);
        chart.setAreaFill(fill);
        String url = chart.toURLString();
        // EXAMPLE CODE END. Use this url string in your web or  Internet application.
        System.out.println(normalize(url));
    }
}

example1生成的url
http://chart.apis.google.com/chart?cht=bvs&chs=600x450&chxt=y,y,x,x&chbh=50,20,8&chts=000000,20&chtt=Team+Scores&chg=10.0,20.0,3,2&chxp=1,50.0|3,50.0&chco=8A2BE2,FF4500,32CD32&chdl=Team+A|Team+B|Team+C&chd=e:QAbhHrTN,FIWZHCDN,GaMzTNTN&chxs=1,FF0000,13,0|3,FF0000,13,0&chf=bg,s,F0F8FF|c,lg,0,E6E6FA,1.0,FFFFFF,0.0&chxl=1:|Score|2:|2002|2003|2004|2005|3:|Year&chxr=0,0.0,200.0,20.0|1,0.0,100.0|3,0.0,100.0

example2 生成的url
http://chart.apis.google.com/chart?chbh=23,4,30&chco=FFD700,C0C0C0,000000&chd=e:..tLc3X2UF,aWvraWQUMj,jItLjIS0S0&chdl=First+LGF|Second+HQY|Three+CD&chf=bg,s,D3D3D3|c,lg,0,E37600,1.0,DC4800,0.0&chg=19.6078431372549,600.0,3,2&chs=450x650&cht=bhg&chts=000000,16&chtt=2008+Beijing+Olympics+Medal+Count&chxl=0:|Germany|United+Kingdom|Russia|USA|China|1:|Country|4:|Medals&chxp=1,50.0|4,50.0&chxr=1,0.0,100.0|2,0.0,51.0,5.0|3,0.0,51.0,5.0|4,0.0,100.0&chxs=0,000000,13,0|1,000000,13,0|2,000000,13,0|3,000000,13,0|4,000000,13,0&chxt=y,y,t,x,x


3. PieChart
/**
 *
 * The MIT License
 *
 * Copyright (c) 2011 the original author or authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jspsmart.charts4j;
import static com.googlecode.charts4j.Color.*;
import org.junit.Test;

import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.PieChart;
import com.googlecode.charts4j.Slice;

/**
 *
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class PieChartExample {

	@Test
    public void example1() {
        // 定义 切片
//      percent - percent of pie. Must be >= 0.
//      color - color of slice.
//      sliceLabel - label associated with slice.	片标签
//      sliceLegend - legend associated with slice. 颜色旁边的片图例
        Slice s1 = Slice.newSlice(30, Color.newColor("CACACA"), "Safari", "Apple");
        Slice s2 = Slice.newSlice(10, Color.newColor("DF7417"), "Firefox", "Mozilla");
        Slice s3 = Slice.newSlice(10, Color.newColor("951800"), "Chrome", "Google");
        Slice s4 = Slice.newSlice(10, Color.newColor("01A1DB"), "Internet Explorer", "Microsoft");

        PieChart chart = GCharts.newPieChart(s1, s2, s3, s4);
        chart.setTitle("A Better Web", BLACK, 16);
        chart.setSize(500, 200);
//        是否开启3D效果
        chart.setThreeD(true);
        String url = chart.toURLString();
        // EXAMPLE CODE END. Use this url string in your web or Internet application.
        System.out.println(url);
    }

    @Test
    public void example2() {
        // EXAMPLE CODE START
        Slice s1 = Slice.newSlice(90, YELLOW, "Ms. Pac-Man");
        Slice s2 = Slice.newSlice(10, RED, "Red Lips");

        PieChart chart = GCharts.newPieChart(s1, s2);
        chart.setTitle("2D Pie Chart", BLACK, 16);
        chart.setSize(500, 200);
        String url = chart.toURLString();
        // EXAMPLE CODE END. Use this url string in your web or Internet application.
        System.out.println(url);
    }

}

example1 url
http://chart.apis.google.com/chart?cht=p3&chs=500x200&chts=000000,16&chd=e:gAKqKqKq&chtt=A+Better+Web&chco=CACACA,DF7417,951800,01A1DB&chdl=Apple|Mozilla|Google|Microsoft&chl=Safari|Firefox|Chrome|Internet+Explorer
example2 url
http://chart.apis.google.com/chart?cht=p&chd=e:5mGa&chs=500x200&chts=000000,16&chtt=2D+Pie+Chart&chco=FFFF00,FF0000&chl=Ms.+Pac-Man|Red+Lips

4. ScatterChart
/**
 *
 * The MIT License
 *
 * Copyright (c) 2011 the original author or authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jspsmart.charts4j;
import static com.googlecode.charts4j.Color.WHITE;
import org.junit.Test;

import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.Fills;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.LinearGradientFill;
import com.googlecode.charts4j.Plots;
import com.googlecode.charts4j.ScatterPlot;
import com.googlecode.charts4j.ScatterPlotData;
import com.googlecode.charts4j.Shape;

/**
 *
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class ScatterChartExample {
    @Test
    public void example1() {
        // 定义数据
        Data d1 = Data.newData(10, 50, 30, 45, 65, 95, 20, 80);
        Data d2 = Data.newData(20, 40, 40, 15, 85, 95, 80, 20);
        Data pointSizes = Data.newData(100, 30, 50, 75, 40, 35, 80, 100);
        
//        xData - x data. Cannot be null.
//        yData - y data. Cannot be null.
//        pointSizes - size of x,y data. Cannot be null.
        ScatterPlotData data = Plots.newScatterPlotData(d1, d2, pointSizes);
        
//        图例
        Color shapeColor = Color.newColor("FF471A");
        data.setLegend("表格提示");
        data.setColor(Color.YELLOW);
        data.addShapeMarkers(Shape.CIRCLE, shapeColor, 30);
        ScatterPlot chart = GCharts.newScatterPlot(data);
        chart.setSize(600, 450);
        chart.setGrid(20, 20, 3, 2);

        AxisLabels axisLabels = AxisLabelsFactory.newNumericRangeAxisLabels(0, 100);
        axisLabels.setAxisStyle(AxisStyle.newAxisStyle(WHITE, 13, AxisTextAlignment.CENTER));

        chart.addXAxisLabels(axisLabels);
        chart.addYAxisLabels(axisLabels);

        chart.setTitle("分散表格标题", WHITE, 16);
        
        // 填充背景颜色
        chart.setBackgroundFill(Fills.newSolidFill(Color.RED));
        
//        设置表格背景
        LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.BLUE, 100);
//        添加额外的填充渐变。
        fill.addColorAndOffset(Color.BLACK, 0);
        chart.setAreaFill(fill);
        String url = chart.toURLString();
        // EXAMPLE CODE END. Use this url string in your web or Internet application.
        System.out.println(url);
    }
}

example1 url
http://chart.apis.google.com/chart?cht=s&chxt=y,x&chs=600x450&chco=FFFF00&chts=FFFFFF,16&chg=20.0,20.0,3,2&chm=o,FF471A,0,-1,30,0&chxr=0,0.0,100.0|1,0.0,100.0&chxs=0,FFFFFF,13,0|1,FFFFFF,13,0&chdl=%E8%A1%A8%E6%A0%BC%E6%8F%90%E7%A4%BA&chf=bg,s,FF0000|c,lg,0,0000FF,1.0,000000,0.0&chd=e:GagATNczpm8yMzzM,MzZmZmJm2Z8yzMMz,..TNgAv.ZmWZzM..&chtt=%E5%88%86%E6%95%A3%E8%A1%A8%E6%A0%BC%E6%A0%87%E9%A2%98

5. Swing
/**
 * The MIT License
 *
 * Copyright (c) 2011 the original author or authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jspsmart.charts4j;
import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.junit.Ignore;

import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.LineChart;
import com.googlecode.charts4j.Plot;
import com.googlecode.charts4j.Plots;

/**
 * Example code for displaying a chart via Swing. This is an alternative
 * to displaying charts via JSP and Servlet technology.
 *
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
@Ignore("not a unit test, just a Java Swing example")
public class SwingExample {

    /**
     * main
     *
     * @param s
     *            args not read.
     * @throws IOException
     */
    public static void main(String...s) throws IOException {
        Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));
        LineChart chart = GCharts.newLineChart(plot);
        displayUrlString(chart.toURLString());
    }

    /**
     * Display the chart in a swing window.
     * @param urlString
     *            the url string to display.
     * @throws IOException
     */
    private static void displayUrlString(final String urlString) throws IOException{
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(ImageIO.read(new URL(urlString))));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}
分享到:
评论
2 楼 rdsgf 2014-06-05  
也就是导出。。。如何把生成的图片导出到本地,有没有接口?
1 楼 rdsgf 2014-06-05  
你好,请问生成的图片如何保存到本地?

相关推荐

    使用Fusion Charts制作报表(dom4j生成XML)

    这篇博客"使用Fusion Charts制作报表(dom4j生成XML)"主要介绍了如何利用Fusion Charts结合DOM4J库来生成XML数据,从而创建动态报表。 首先,我们需要理解Fusion Charts的核心概念。Fusion Charts提供了一系列预定...

    安卓图表报表相关-相当全相当不错的例子android图表各种XCL-Charts-master.rar

    《安卓图表报表应用详解——基于XCL-Charts的实践指南》 在移动应用开发中,数据可视化是一项重要的任务,尤其在安卓平台上,清晰、直观的图表报表能够帮助用户更好地理解和分析数据。本篇将深入探讨一款名为XCL-...

    docx4j-charts-api:以编程方式创建与 MS Office 兼容的图表

    在实际应用中,docx4j-charts-api可以被用于生成报表服务、数据分析工具或者任何需要将数据可视化的场景。例如,一个Web应用可以接收用户输入的数据,然后利用此API动态创建图表并将其嵌入到Word或PowerPoint模板中...

    Android报表打印研究.docx

    1. charts4j是基于Google Chart API的开源框架,因此在中国大陆可能无法正常使用,因为许多厂商屏蔽了Google Chart API。 2. android-ski-report是一个免费的解决方案,但具体功能和兼容性需要进一步调查。 3. ...

    Excel Report 万能报表平台4.45

    Excel Report Builder Excel Report 万能报表平台 Excel 使用WEB服务(webservice)访问远程数据库 使用本软件可以使你的应用系统(数据库)和excel相连。 可以把它嵌入到你的应用系统里,做为应用系统的...

    安卓图表报表相关-CircularButton.zip

    在实际应用中,制作图表报表通常会用到专门的库,如MPAndroidChart、AChartEngine、Charts4J等。这些库提供了丰富的图表类型,如柱状图、折线图、饼图等,并支持自定义样式和动画,可以方便地集成到Android应用中。...

    安卓图表报表相关-Android种类比较齐全的图表库.rar

    4. Charts4J:这是Google Charts的一个Java接口,可以在Android上使用Google Charts API来创建各种复杂的图表。 5. AnyChart:一个跨平台的JavaScript图表库,通过WebView组件在Android上也能使用,提供多种交互式...

    Dubbo监控中心的介绍与简易监控中心的安装.docx

    四、查看Dubbo简易监控中心报表数据 重新启动Dubbo-monitor简易监控中心后,可以进入Dubbo简易监控中心查看对应的报表数据,包括服务提供者和服务消费者的调用统计报表。 Dubbo监控中心和简易监控中心可以实时监控...

    J2EEweb项目

    开发者可能使用JDBC(Java Database Connectivity)API来与Oracle数据库进行交互,执行SQL语句,实现数据的增删改查和报表导出功能。 SHH(Secure SHell)可能指的是SSH协议,用于远程登录和安全管理,确保数据传输...

    15款经典图表软件推荐 创建最漂亮的图表

    4. Open Flash Chart:这是一个开源的Flash图表组件,用于生成动态图表,特别适合那些希望在网页中添加图表的开发者。 5. Free PHP Graph/Chart:FusionCharts是免费且开源的Flash图表组件,可用于创建各种Web应用...

    Excel Report Builder

    Excel Report Builder Excel Report 万能报表平台 Excel 使用WEB服务(webservice)访问远程数据库 使用本软件可以使你的应用系统(数据库)和excel相连。 可以把它嵌入到你的应用系统里... 报表安装包: ...

    Excel Report Builder4.45

    Excel Report Builder Excel Report 万能报表平台 Excel 使用WEB服务(webservice)访问远程数据库 使用本软件可以使你的应用系统(数据库)和excel相连。 可以把它嵌入到你的应用系统里,做为应用系统的...

    jexcelapi_2_6_12.zip

    4. **高级功能**:除了基本的读写操作,JExcelAPI还可以处理图表、数据验证、超链接等更复杂的Excel特性。例如,`charts`包提供了创建和修改Excel图表的功能。 三、JExcelAPI使用示例 以下是一个简单的使用...

    Dubbo监控中心的介绍与简易监控中心的安装

    - `dubbo.container`: 指定了容器类型,包括日志管理(log4j)、Spring容器(spring)、注册中心(registry)和Jetty服务器(jetty)。 - `dubbo.application.name`: 应用名称,便于管理和识别。 - `dubbo.application.owner...

    简单的导出Excel二维图

    在Delphi编程环境中,开发人员经常需要将应用程序的数据导出到Excel格式,以便于数据分析、报表制作或与其他软件共享。本教程将聚焦于一个相对简单的任务:如何在Delphi中导出Excel二维图,这对于初学者来说是一个很...

    asp.net(C#)操作excel(上路篇)

    myExcel.Cells[1, 4] = "普通报表"; // 写入数据 for (int i = 0; i (0); i++) { for (int j = 0; j (1); j++) { myExcel.Cells[i + 2, j + 1] = myData[i, j]; } } // 保存文件 myBook.SaveAs(output...

Global site tag (gtag.js) - Google Analytics