/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* JFreeChart下载地址: http://www.jfree.org/jfreechart/index.html
*
*--------------------
* BarChart3DDemo2.java
* --------------------
*/
package org.jfree.chart.demo;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPosition;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.CategoryLabelWidthType;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.text.TextBlockAnchor;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Log;
import org.jfree.util.PrintStreamLogTarget;
/**
* 展示如何创建水平3D棒状图的Demo
* 使用数据来自{@link CategoryDataset}.
*
*/
public class BarChart3DDemo2 extends ApplicationFrame {
/**
* 建立一个新的demo.
*
* @param title 窗口标题.
*/
public BarChart3DDemo2(final String title) {
super(title);
// 创建图表...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(23.0, "Series 1", "London");
dataset.addValue(14.0, "Series 1", "New York");
dataset.addValue(14.0, "Series 1", "Istanbul");
dataset.addValue(14.0, "Series 1", "Cairo");
dataset.addValue(13.0, "Series 2", "London");
dataset.addValue(19.0, "Series 2", "New York");
dataset.addValue(19.0, "Series 2", "Istanbul");
dataset.addValue(19.0, "Series 2", "Cairo");
dataset.addValue(7.0, "Series 3", "London");
dataset.addValue(9.0, "Series 3", "New York");
dataset.addValue(9.0, "Series 3", "Istanbul");
dataset.addValue(9.0, "Series 3", "Cairo");
final JFreeChart chart = createChart(dataset);
//把图表放到面板容器...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* 先建个表再说.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createBarChart3D(
"3D Bar Chart Demo 2", // 表题
"Category", // 域坐标标签
"Value", // 横坐标标签
dataset, // 数据
PlotOrientation.HORIZONTAL, // 方向
true, // 包含摘要
true, // 工具提示
false // urls
);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setForegroundAlpha(1.0f);
// 分类标签左对齐...
final CategoryAxis axis = plot.getDomainAxis();
final CategoryLabelPositions p = axis.getCategoryLabelPositions();
final CategoryLabelPosition left = new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
TextAnchor.CENTER_LEFT, 0.0,
CategoryLabelWidthType.RANGE, 0.30f
);
axis.setCategoryLabelPositions(CategoryLabelPositions.replaceLeftPosition(p, left));
return chart;
}
/**
* demo程序的起始点.
*
* @param args ignored.
*/
public static void main(final String[] args) {
Log.getInstance().addTarget(new PrintStreamLogTarget());
final BarChart3DDemo2 demo = new BarChart3DDemo2("3D Bar Chart Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
本文所使用的版本为:jfreechart-1.0.0-rc1.zip 可
可以到:http://www.jfree.org/jfreechart/index.html
凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。
|