JFreechart类柱状图

JFreechart 柱状图完整设置

2009-07-05 23:53:36|  分类: jfreechart |  标签:jfreechart     字号: 订阅

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class VoteChartAction extends BaseAction {
private static final long serialVersionUID = 2086339818803106472L;
private JFreeChart chart;
private static int supportCount = 0;
private static int blackballCount = 0;
private String voteType;

public JFreeChart getChart()
{
   chart = ChartFactory.createBarChart3D(
     "热点讨论投票结果", // 图表标题
     "", // 目录轴的显示标签
     "", // 数值轴的显示标签
     getDataSet(), // 数据集
     //PlotOrientation.HORIZONTAL , // 图表方向:水平
     PlotOrientation.VERTICAL , // 图表方向:垂直
     false, // 是否显示图例(对于简单的柱状图必须是false)
     true, // 是否生成工具
     true // 是否生成URL链接
     );
   //重新设置图标标题,改变字体
   chart.setTitle(new TextTitle("热点讨论投票结果", new Font("黑体", Font.ITALIC , 18)));
   //取得统计图标的第一个图例
   //LegendTitle legend = chart.getLegend(0);
   //修改图例的字体,必须把显示图例设置为true,否则会报空指针异常
   //legend.setItemFont(new Font("宋体", Font.BOLD, 14));

   //获得柱状图的Plot对象
   CategoryPlot plot = chart.getCategoryPlot();
   plot.setBackgroundPaint(Color.pink); // 设定图表数据显示部分背景色
   //取得横轴
   CategoryAxis categoryAxis = plot.getDomainAxis();
   //设置横轴显示标签的字体
   categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
   //分类标签以45度角倾斜
   //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
   categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
   //取得纵轴
   NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
   //设置纵轴显示标签的字体
   numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
   //设置最高的一个柱与图片顶端的距离
   numberAxis.setUpperMargin(0.1);
   //numberAxis.setFixedAutoRange(100);
   //设置整数显示
   //numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
   //numberAxis.setNegativeArrowVisible(true);
  
   //取最大数Math.max(supportCount, blackballCount)
   numberAxis.setUpperBound(1);
   numberAxis.setLowerBound(0.01);
   //设置百分比显示
   numberAxis.setNumberFormatOverride(new DecimalFormat("0%"));
   //numberAxis.setNumberFormatOverride(new DecimalFormat("0.00%"));
   //设置最小显示数,小于的话会显示在中间(正负)
   //numberAxis.setAutoRangeMinimumSize(1);
  
   plot.setNoDataMessage("没有可供使用的数据!");
   plot.setNoDataMessagePaint(Color.blue);
  
  
   BarRenderer3D renderer = new BarRenderer3D();
   //设置柱子宽度
   renderer.setMaximumBarWidth(0.1);
   //设置柱子高度
   renderer.setMinimumBarLength(0.2);
   //设置柱子的颜色
        renderer.setSeriesPaint(0, new Color(0, 0, 255));
       
        //设置柱子边框可见
        //renderer.setDrawBarOutline(true);
        //设置柱子默认的边框颜色,必须设置边框可见才起效
   //renderer.setBaseOutlinePaint(Color.gray);
        //设置分类柱子的边框色,覆盖默认的边框颜色,必须设置边框可见才起效
        //renderer.setSeriesOutlinePaint(0,Color.red);
        //设置柱子的纵横背景色
        //renderer.setWallPaint(Color.gray);
        //设置平行柱的之间距离
        renderer.setItemMargin(0.5);
        //显示每个柱的数值,并修改该数值的字体属性
        renderer.setIncludeBaseInRange(true);
        //将修改后的属性值保存到图中,这一步很重要,否则上面对颜色的设置都无效
        plot.setRenderer(renderer);
        
        //设置柱子的透明度,0.8相当于80%的透明度
        plot.setForegroundAlpha(0.8f);

   return chart;
}

//返回一个CategoryDataset实例
private static CategoryDataset getDataSet()
{
   DefaultCategoryDataset dataset = new DefaultCategoryDataset();
   int total = supportCount+blackballCount;
   if(total < 1) total = 1;
   dataset.addValue((double)supportCount/total, "" , "正方(" + supportCount + ")");
   dataset.addValue((double)blackballCount/total, "" , "反方(" + blackballCount + ")");
   //dataset.addValue(supportCount, "佛山" , "正方");
   //dataset.addValue(blackballCount, "佛山" , "反方");
   //dataset.addValue(supportCount , "广州" , "正方");
   //dataset.addValue(blackballCount , "广州" , "反方");
   return dataset;
}

public String showVoteChart()
{
   if("support".equals(voteType))
   {
    supportCount++;
   }
   else if("blackball".equals(voteType))
   {
    blackballCount++;
   }
   //supportCount = 0;//快速清零,不用重启系统
   //blackballCount = 0;//快速清零,不用重启系统
   this.chart = getChart();
   return SUCCESS;
}

/**
* @return the supportCount
*/
public static int getSupportCount()
{
   return supportCount;
}

/**
* @return the blackballCount
*/
public static int getBlackballCount()
{
   return blackballCount;
}

/**
* @return the voteType
*/
public String getVoteType()
{
   return voteType;
}

/**
* @param supportCount the supportCount to set
*/
public static void setSupportCount(int supportCount)
{
   VoteChartAction.supportCount = supportCount;
}

/**
* @param blackballCount the blackballCount to set
*/
public static void setBlackballCount(int blackballCount)
{
   VoteChartAction.blackballCount = blackballCount;
}

/**
* @param voteType the voteType to set
*/
public void setVoteType(String voteType)
{
   this.voteType = voteType;
}

}

=============================================

struts 配置

==========================

<package name="chart" extends="jfreechart-default">
   <action name="show_vote_chart" class="com.guosen.weblogger.web.action.VoteChartAction" method="showVoteChart">
    <result name="success" type="chart">
     <param name="width">420</param>
     <param name="height">250</param>
    </result>
   </action>
</package>

========================

页面代码

========================

<div style="width:420px;height:250px;padding-left:100px;">
    <iframe id="votechart" name="votechart" src="${base}/chart/show_vote_chart.action" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" style="height:100%;width:100%"></iframe>
   </div>
   <div style="width:420px;height:25px;padding-left:100px;text-align:center;font-size:16px;">
    <a href="#this" οnclick="changeChart('support')">支持</a>&nbsp;&nbsp;
    <a href="#this" οnclick="changeChart('blackball')">反对</a>
   </div>
   <script type="text/javascript">
    function changeChart(type)
    {
     if(type == "support")
     {
      coos.$id("votechart").src = "${base}/chart/show_vote_chart.action?voteType=support";
     }
     else if(type == "blackball")
     {
      coos.$id("votechart").src = "${base}/chart/show_vote_chart.action?voteType=blackball";
     }
   
    }
   </script>
   <hr />

引自:http://dreampeter.blog.163.com/blog/static/7980324200965115336881/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值