JFreechart类柱状图
?? //分类标签以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" 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" onclick="changeChart('support')">支持</a>
??? <a href="#this" onclick="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/