报表 Openchart2

[size=medium][color=red]1. 简介[/color][/size]
Openchart2 is a full-featured 2D plotting solution for Java applications.

Graph Types
Openchart2 supports a variety of chart types, including:

1. Bar Charts - including Stacked Bar Charts
2. Pie Charts
3. Radar Charts
4. Scatter Plots - including Line Charts, Point Charts, or combinations
5. Dot Plots - for the visualization of (x,y,z) data

The Openchart2 rendering system allows multiple types of plots to be drawn on the same view, enabling unlike graphs to appear together.

Simple Data Passing
Openchart2 allows for a variety of techniques to be used for passing data into the plotting routines. Simple arrays or full database sources can provide data to the plotting routines.

Dynamic Zooming
All chart types support dynamic zooming. Sample Swing chart panels are provided in the library with complete mouse-controlled dynamic zoom implemented for ease of use.
上面信息来源于官网[url]http://approximatrix.com/products/openchart2/features[/url]

[size=medium][color=red]2.例子[/color][/size]

[color=green]1. BarChart[/color]

package com.Openchart2;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.approximatrix.charting.model.ObjectChartDataModel;
import com.approximatrix.charting.model.ChartDataModel;
import com.approximatrix.charting.swing.ChartPanel;
import com.approximatrix.charting.render.BarChartRenderer;
import com.approximatrix.charting.render.PieChartRenderer;
import com.approximatrix.charting.CoordSystem;

public class BarChart extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;

/** 每个bar对应的label */
String[] names = {"aaa","bbb","ccc"};

/** 每个bar对应的值 */
double[] values = {10.0,30.0,60.0};

/** Initializes the dialog and its chart in a ChartPanel dialog */
@SuppressWarnings("deprecation")
public BarChart() {
super();

// 对这个dialog进行初始化
this.setTitle("Bar 的例子");
this.setSize(500, 400);
this.setResizable(true);

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
this.setContentPane(contentPane);

// 定义当点击x的时候窗口是Hide还是Destroy
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

/**
* 初始化必要的数据 存放在二维数组中
* data[0][i] 每个系列的数(就像这次结果的one two three four 四个系列)
* data[i][0] 每个系列中具体的值 和 name进行一一对应
*
* eg:
* data[0][0] data[1][0] data[2][0] 都是names[0] 的值
* data[0][1] data[1][1] data[2][1] 都是names[1] 的值
*
*
* data[0][0] data[0][1] data[0][2] 都是series_names[0]一组中的数据
* data[1][0] data[1][1] data[1][2] 都是series_names[1]一组中的数据
*
*
*/
Double[][] data = new Double[values.length][names.length];
for(int i=0;i<values.length;i++) {
for (int j = 0; j < names.length; j++) {
data[i][j] = new Double(values[i]);
}
}
// We need to pass a title for this series as well
String[] series_names = {"one","two","three","four"};

// 根据传递的数据 创建ChartDataModel 数据模型
ChartDataModel model = new ObjectChartDataModel(data, names, series_names);

// 根据对象模型创建坐标系统
CoordSystem coord = new CoordSystem(model);

// 创建一个默认的图形面板
ChartPanel chart_panel = new ChartPanel(model,"Openchart2 图形测试");

// 设置图形面板的坐标
chart_panel.setCoordSystem(coord);

// 指定要生成的图形BarChartRenderer 还是 PieChartRenderer 等等..
chart_panel.addChartRenderer(new BarChartRenderer(coord,model), 0);
// chart_panel.addChartRenderer(new PieChartRenderer(coord,model),0);
// 定义位置
contentPane.add(chart_panel,BorderLayout.CENTER);

model.setManualScale(true);
model.setMinimumValue(new Double(0.0));
model.setMaximumValue(new Double(100.0));

}

/**
* mian 函数
* @param args
*/
public static void main(String[] args) {
BarChart temp = new BarChart();
temp.setVisible(true);
}

/**
* 事件监听
*/
public void actionPerformed(ActionEvent e) {

}
}



结果如图:
[img]http://lingf.iteye.com/upload/picture/pic/100933/8f00976f-95cf-3fe4-8ab4-a67afa1c8032.jpg[/img]

[color=cyan]2. PieChart[/color]

package com.Openchart2;
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import com.approximatrix.charting.DefaultChart;
import com.approximatrix.charting.CoordSystem;
import com.approximatrix.charting.model.ObjectChartDataModel;
import com.approximatrix.charting.render.PieChartRenderer;

public class PieChart extends JApplet {

// 定义char的基本数据
private static final long serialVersionUID = 1L;
private static final String[] markDate = {"one","Two","Three","Four","Five","Six"};
private static final int[] amount = {10,20,30, 40,50,60};
DefaultChart chart = null;

/** Constructor for our pie chart applet
*
*/
@SuppressWarnings("deprecation")
public PieChart() {
int data[][] = new int[amount.length][1];
for(int i=0;i<amount.length;i++) {
data[i][0] = amount[i];
}

// 暂时无作用,仅仅初始化模型时候会用到 x轴坐标值
String[] column_id = {"column1","column2"};

// 创建一个数据模型
// data - the array of values. The first index specifies the datasets,
// the last one is the value index.
// columns - the array of x-axis values. The length of the datasets and
// the length of the column should be equal and the columns should be ordered.
// rows - the DataSet titles 每个date对应的titles
ObjectChartDataModel model = new ObjectChartDataModel(data, column_id, markDate);

// model - the ChartDataModel 数据模型
// title - the title String 标题
chart = new DefaultChart(model,"饼状图例子");

// 根据数据模型创建一个饼状图
// model - the DataModel that should be rendered
PieChartRenderer renderer = new PieChartRenderer(model);

// render - the ChartRenderer
// z - the z-coordinate, the highest coordinate is in front.
chart.addChartRenderer(renderer, 0);

// A coordinate system is necessary for any chart, regardless of type. A pie chart,
// however, really doesn't need to use one. After creating the default CoordSystem
// object, turn off all axis drawing since we don't need to see the axes.
CoordSystem coord = new CoordSystem(model);
coord.setPaintAxes(true); // 是否显示x-y轴坐标
chart.setCoordSystem(coord);

// Our graph is now constructed
}

/** Override the paint routine to draw the graph
*/
public void paint(Graphics g) {
super.paint(g);
chart.setBounds(new Rectangle(this.getWidth(), this.getHeight()));
chart.render((Graphics2D)g);
}
}

[img]http://lingf.iteye.com/upload/picture/pic/100939/9bb3671d-9e75-3877-9f26-05b4a7387d0c.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值