在Java中使用Oracle Chart Builder输出图表

发表于:2007-07-04来源:作者:点击数: 标签:
可随意转载,但请注明出处及作者 SonyMusic 2003.05.15 ========================================================================== 在Java中使用 Oracle Chart Builder输出图表 package test.chart; import java.awt.Color; import java.awt.Font; import
可随意转载,但请注明出处及作者
SonyMusic
2003.05.15
==========================================================================
在Java中使用Oracle Chart Builder输出图表


package test.chart;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import oracle.charts.axischart.AxisChart;
import oracle.charts.types.AnnotationDesc;
import oracle.charts.types.ChartException;
import oracle.charts.types.LineDesc;
import oracle.charts.types.BarDesc;
import oracle.charts.types.MarkerDesc;
import oracle.charts.types.NumAxisDesc;

/**
* @author SonyMusic
*
* 简单的测试了Oracle Chart的用法.
* 下载地址:http://otn.oracle.com/software/tech/java/servlets/content.html
*/
public class TestChart1 {

    //月份,其实就是X轴的标尺内容
    public final static String annualDates[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

    //这是实际数据
    public final static double p2002[] = { 1768, 1891, 1707, 1630, 1678, 1761 };
    public final static double p2001[] = { 1697, 1787, 1684, 1870, 1847, 1852, 1858, 1901, 2016, 2068, 1863, 1891 };

    public static void main(String[] args) {

        // 嵌入图表的窗口(frame)的高和宽
        int chartHeight = 400;
        int chartWidth = 600;

        Frame f = new Frame();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                e.getWindow().dispose();
            }
        });
        f.setSize(chartWidth + 10, chartHeight + 40);
        f.setTitle("Oracle Chart测试窗口");

        try {

            // 创建AxisChart对象
            AxisChart axisCh = new AxisChart();

            //
            // 图表的高和宽
            axisCh.setSize(chartWidth, chartHeight);

            //
            // 将时间戳放入X轴
            axisCh.setXSeries(annualDates);

            //
            // 将价格数字插入到Y轴,并有对应的名称
            String y2001 = "2001年";
            String y2002 = "2002年";
            axisCh.setYSeries(y2001, p2001);
            axisCh.setYSeries(y2002, p2002);

            //
            // 定义Line类型的图表,并设置线条颜色宽度,标记的类型和颜色
            //还可以定义其它类型,如:BarDesc
            LineDesc ld2001 = new LineDesc();
            ld2001.setLineColor(Color.RED);
            ld2001.setMarkerType(MarkerDesc.MARKER_CIRCLE);
            ld2001.setLineWidth(1);
            ld2001.setMarkerColor(Color.GREEN);

            LineDesc ld2002 = new LineDesc();
            ld2002.setLineColor(Color.BLUE);
            ld2002.setMarkerType(MarkerDesc.MARKER_DIAMOND);
            ld2002.setLineWidth(1);
            ld2002.setMarkerColor(Color.BLACK);

            //将两个线加到图表中去,并与名称对应上
            axisCh.setSeriesGraphic(y2001, ld2001);
            axisCh.setSeriesGraphic(y2002, ld2002);

            //设置图表的背景色
            AnnotationDesc ad = axisCh.getAnnotationDesc();
            ad.setForeground(Color.CYAN);

            //设置图表Y轴标尺的颜色、间隔,以及范围
            NumAxisDesc nad = new NumAxisDesc();
            nad.setColor(Color.black);
            nad.setLabelIncrement(100);
            nad.setExtent(1200, 2500);

            axisCh.setChartAttributes(nad);

            //
            // 设置标题, 子标题, 和脚注. 及其字体
            axisCh.getTitle().setText("2002年与2001年同期现款价格比较");
            axisCh.getTitle().setFont(new Font("宋体", Font.PLAIN, 18));
            axisCh.getSubtitle().setText("钢坯(单价:元/吨)");
            axisCh.getFootnote().setText("数据来源:销售科             时间(月)");

            //
            // 将图表添加到窗口中
            f.add(axisCh);

            //还可以输出到一个OutputStream中--以下的out
            //以下用到了Acme中的Gif Encode组件
            /*
            BufferedImage image = new BufferedImage(chartWidth, chartHeight,
                                                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = (Graphics2D) image.getGraphics();
            axisCh.drawBuffer(g);

            GifEncoder ge = new GifEncoder(image, out);
            ge.encode();
            */

        }
        catch (ChartException e) {
            System.out.println(e.getMessage());
        }

        f.show();

    }
}

原文转自:http://www.ltesting.net