java源代码:
public class BarChartAction extends DIBaseAction {
public String execute() throws Exception {
//5.0图形的宽和 高
int width = 800;
int height = 360;
//4.0图例的属性对象
LegendProperties legendProperties;
legendProperties = new LegendProperties();
/***************************************************************************************************************/
//3.0图形的XY轴的属性对象
AxisProperties axisProperties;
axisProperties = new AxisProperties( false );
//图表横坐标和纵坐标范围的字体,大小,颜色设置对象
ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black );
axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont );
axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont );
//Bar图形的横坐标和纵坐标的标题字体和颜色 ,大小的设置对象
ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black );
axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont );
axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont );
/***************************************************************************************************************/
DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties();
try
{
//设置用户定义的纵轴纵坐标的 间隔范围
dataAxisProperties.setUserDefinedScale( -3000, 3000 );
}
catch( PropertyException propertyException )
{
propertyException.printStackTrace();
}
dataAxisProperties.setRoundToNearest(2);
/***************************************************************************************************************/
//2.0生成图像的属性对象
ChartProperties chartProperties;
chartProperties = new ChartProperties();
//设置图形标题的字体,颜色,大小
ChartFont titleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 14 ), Color.black );
chartProperties.setTitleFont( titleFont );
/***************************************************************************************************************/
/***************************************************************************************************************/
//Bar图形的属性类
BarChartProperties barChartProperties;
barChartProperties = new BarChartProperties();
ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 );
valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.ON_TOP );
//是否设置显示的纵坐标标签垂直,true为是,flase为水平
valueLabelRenderer.useVerticalLabels( false );
barChartProperties.addPostRenderEventListener( valueLabelRenderer );
/***************************************************************************************************************/
try
{
//设置横坐标标签
String[] xAxisLabels = {"1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004"};
//设置横坐标的单位
String xAxisTitle = "Years";
//设置纵坐标的标题
String yAxisTitle = "Problems";
//设置图形的标题
String title = "Micro$oft At Work";
/***************************************************************************************************************/
//1.0图形所需要的数据对象
IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
//设置条形数据
double[][] data = new double[][]{{1500, 6880, 4510, 2600, -1200, -1580, 7000, 4555, 4000, 6120}};
//条形区域 形示的标签
String[] legendLabels = {"Bugs"};
//条形区域绘制的颜色设置对象
Paint[] paints = new Paint[]{Color.yellow};
dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties ) );
/***************************************************************************************************************/
//产生一个 chart对象
AxisChart axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, width, height );
//输出设置好的chart图形
ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, response );
}
catch( Throwable throwable )
{
throwable.printStackTrace();
}
return SUCCESS;
}
}
JCharts和JFreeChart比较
JFreeChart
官方网址: http://www.jfree.org/jfreechart/
功能比较完善, 文档需要花钱购买。
最新版本1.0.10(2008年6月), 活跃度较高。
基础图形库, 适用于各种Java环境。
JFreeChart显示效果好一些。
JFreeChart提供了对更多种常用图形的支持。
JFreeChart适合于对图形需求较多并且要求图形具有较强的扩展性的项目。
JCharts
官方网址: http://jcharts.sourceforge.net/
功能比较完善, 有在线文档, 但是文档不是很全面。
最新文档版 0.7.0 (2003年03月)
最后一次更新在2004年,1.0.0 - alpha, 活跃度较低.
基础图形库, 适用于各种Java环境.
JCharts显示效果差一些。
JCharts支持的图形太少。
JCharts更适用于相对小型的项目。