两大Web图表工具PK:JreeChart和ChartDirector

我最近在做项目的时候,要用到图表,其中画图表工具有好多种,今天我们就对ChartDirectorJreeChart这2种进行学习和比较,掌握技术路径,即掌握安装配置方式,接口,调用方法,例子等。

下载了ChartDirector,挺简单的,照着提供的jsp的例子,改一下数据、横坐标内容就马上能运行了,提供的函数命名也很规范,一看大概就知道用途,挺好理解的,生成的图表也很漂亮。

下载JreeChart,这个东西配置也挺简单的,图表质量不如ChartDirector好看,而且生成的图片的大小还大。

二、ChartDirector与JreeChart的介绍与比较

官方网址

ChartDirector英文官网: http://www.advsofteng.com/
中文介绍http://www.evget.com/zh-CN/product/515/feature.aspx

JreeChart: http://www.jfree.org/jfreechart/index.html

基本介绍

两款都是流行的Web图表工具:

ChartDirector:
ChartDirector控 件使用方便,快捷,灵活,功能强大,交互性强。在Web服务器以及嵌入式应用程序开发中,它是一种非常理想的工具,拥有丰富的图表图形组件库。支持多种图 表样式,如圆形图表(饼状图),圆环图,柱形图(条形图),直线图,曲线图,梯级线图,趋势线图,曲线拟合图,线间色图,区域图,散布图(散形图),泡沫 图等。采用多线程结构,特别应用于具有高性能要求的服务器端应用程序开发。拥有基于API(应用编程接口)的对象,允许用户控制和定制图表细节,从而设计 出用户满意的图表。

JreeChart:
JFreeChart是 一个开源的 JAVA 项目,它主要用来开发各种各样的图表,这些图表包括:饼图、柱状图 ( 普通柱状图以及堆栈柱状图 ) 、线图、区域图、分布图、混合图、甘特图以及一些仪表盘等等。在这些不同式样的图表上可以满足目前商业系统的要求。 JFreeChart 是一种基于 JAVA 语言的图表开发技术。 JFreeChart 可用于 Servlet 、 JSP 、 Applet 、 Java Appication 环境中,通过 JDBC 可动态显示任何数据库数据,结合 Itext 可以输出至 PDF 文件。

收费情况

ChartDirector: 商业;价格根据使用权限不同在500元到800元之间;也可以免费使用,只是在画出来的图形下面都有一条它的广告条。网上有破解方法,破解后图形下面不再出现它的广告条。

JreeChart: 开源;但是文档要花钱买,400元;

支持语言

ChartDirector: 支持很多种语言,例如.NET, Java, ASP, COM,VB, PHP, Perl, Python,Ruby, ColdFusion, C++等;

JreeChart: Java;

图表比较

ChartDirector: 图表特别精细,漂亮;
样例库:http://www.advsofteng.com/gallery.html

JreeChart: 画出来的图形不够精细,看起来有些模糊;图表的文字边缘、颜色和颜色的分界也比较模糊。
样例库:http://www.jfree.org/jfreechart/samples.html

对中文问题支持的比较

ChartDirector: 中文的问题,比较容易解决。
JreeChart: 虽然有字体的解决办法,但仍然存在问题。他使用的默认字体显示出来的中文会很模糊,你可能需要修改源代码。

开发使用易用性比较

从自己分别使用它们用jsp显示柱状图的例子来看,两者的开发的易用性差不多,都是设置一下数据、横坐标等就可以了。

三、分别用ChartDirector和JFreeChart画柱状图的JSP程序示例

  • 用ChartDirector在JSP中画统计图

下面是一个柱状图的例子:

范例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@page import="ChartDirector.*" %>
 <%
//The data for the bar chart
    double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
    String[] labels = {"Mon", "Tue",
    "Wed", "Thu", "Fri"};
//Create a XYChart object of size 300 x 280 pixels
    XYChart c = new XYChart(300, 280);
//Set the plotarea at (45, 30) and of size 200 x 200 pixels
    c.setPlotArea(45, 30, 200, 200);
//Add a title to the chart
    c.addTitle("Weekly Server Load");
//Add a title to the y axis
    c.yAxis().setTitle("MBytes");
//Add a title to the x axis
    c.xAxis().setTitle("Work Week 25");
//Add a bar chart layer with green (0x00ff00)
 bars using the given data
    c.addBarLayer(data, 0xff00).set3D();
//Set the labels on the x axis.
    c.xAxis().setLabels(labels);
//output the chart
    String chart1URL = c.makeSession(request, "chart1");
//include tool tip for the chart
    String imageMap1 = c.getHTMLImageMap("", "",
    "title='{xLabel}: {value} MBytes'")
    ;
    %>
    <html>
    <body topmargin="5" leftmargin="5"
    rightmargin="0">
    <div style="font-size:18pt; font-family:verdana;
    font-weight:bold">
    3D Bar Chart
    </div>
    <hr color="#000080">
    <a href="viewsource.jsp?file=<%=request.
    getServletPath()%>">
    <font size="2" face="Verdana"
    >View Chart Source Code</font>
    </a>
    </div>
    <br>
    <img src='<%=response.encodeURL("getchart.jsp?"+chart1URL)%>'
    usemap="#map1" border="0">
    <map name="map1"><%=imageMap1%></map>
    </body>
    </html>  
  • 用JFreeChart画柱状图的范例

这个范例说明如何用JFreeChart画简单的柱状图,下面是一个JSP的简单范例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.awt.*, java.text.*, java.util.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.labels.
StandardCategoryItemLabelGenerator" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.*" %>
<%@ page import="org.jfree.chart.servlet.ServletUtilities" %>
<%@ page import="org.jfree.data.DefaultCategoryDataset" %>
<%@ page import="org.jfree.ui.TextAnchor" %>
<%
//The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels = {"Mon", "Tue",
 "Wed", "Thu", "Fri"};
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < data.length; i++) {
dataset.addValue(data[i], null, labels[i]);
}
JFreeChart chart = ChartFactory.createBarChart3D(
"Weekly Server Load", "Work Week 25",
"MBytes", dataset,
PlotOrientation.VERTICAL, false, false, false);
chart.setBackgroundPaint(new Color(0xE1E1E1));
CategoryPlot plot = chart.getCategoryPlot();
// 设置Y轴显示整数
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryAxis domainAxis = plot.getDomainAxis();
//设置距离图片左端距离
domainAxis.setLowerMargin(0.05);
BarRenderer3D renderer = new BarRenderer3D();
//设置柱的颜色
renderer.setSeriesPaint(0, new Color(0xff00));
plot.setRenderer(renderer);
String filename = ServletUtilities.saveChartAsPNG(
chart, 300, 280, null, session);
String graphURL = request.getContextPath() +
"/displayChart?filename=" + filename;
%>
<html>
<body topmargin="5"
leftmargin="5" rightmargin="0">
<div style="font-size:18pt;
font-family:verdana; font-weight:bold">
3D Bar Chart
</div>
<br>
<img src="<%= graphURL %>" border=0>
</body>
</html>
欢迎加群交流控件经验:301644590
原文地址:https://www.cnblogs.com/flashcharts/p/2935275.html