原创|其它|编辑:郝浩|2012-10-22 17:57:27.000|阅读 1827 次
概述:统计图像的实例,来看看FusionCharts强大的图表效果。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
FusionCharts是一套很有用的统计图生成工具,本文主要介绍一下FusionCharts大概的执行流程:
1.我们先从数据库取数据;
2.用php代码做一个生成xml的函数,把数据生成xml;
3.用一段js读取xml,送到FusionCharts,生成统计图表。
说起来还是挺烦琐的,所以还是开始做吧。这里要做两个函数,makexml(),render(),方便我们的调用。
view plaincopy to clipboardprint?
function makexml($data)
{
$strXML .= "";
foreach ($data as $k=>$v)
{
$strXML .= "<set name='".$v['Option']."' value='".$v['Num']."' />"; //省掉了color
}
return $strXML;
}
function makexml($data)
{
$strXML .= "";
foreach ($data as $k=>$v)
{
$strXML .= "<set name='".$v['Option']."' value='".$v['Num']."' />"; //省掉了color
}
return $strXML;
}
注意!FusionCharts根据生成统计表的不同分了两种xml格式,如下所示,大家可以参考Gallery/Data文件夹里面。可能我说漏了也说不定。所以上面那个函数是对应生成第一种。
view plaincopy to clipboardprint?
<graph caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' decimalPrecision='0' formatNumberScale='0' chartRightMargin='30'>
<set name='Jan' value='462' color='AFD8F8' />
<set name='Feb' value='857' color='F6BD0F' />
<set name='Mar' value='671' color='8BBA00' />
<set name='Apr' value='494' color='FF8E46'/>
<set name='May' value='761' color='008E8E'/>
</graph>
<graph caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' decimalPrecision='0' formatNumberScale='0' chartRightMargin='30'>
<set name='Jan' value='462' color='AFD8F8' />
<set name='Feb' value='857' color='F6BD0F' />
<set name='Mar' value='671' color='8BBA00' />
<set name='Apr' value='494' color='FF8E46'/>
<set name='May' value='761' color='008E8E'/>
</graph>
view plaincopy to clipboardprint?
<graph xaxisname='Continent' yaxisname='Export' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='Global Export' subcaption='In Millions Tonnes per annum pr Hectare' >
<categories font='Arial' fontSize='11' fontColor='000000'>
<category name='N. America' hoverText='North America'/>
<category name='Asia' />
<category name='Europe' />
<category name='Australia' />
<category name='Africa' />
</categories>
<dataset seriesname='Rice' color='FDC12E'>
<set value='30' />
<set value='26' />
<set value='29' />
<set value='31' />
<set value='34' />
</dataset>
<dataset seriesname='Wheat' color='56B9F9'>
<set value='67' />
<set value='98' />
<set value='79' />
<set value='73' />
<set value='80' />
</dataset>
<dataset seriesname='Grain' color='C9198D' >
<set value='27' />
<set value='25' />
<set value='28' />
<set value='26' />
<set value='10' />
</dataset>
</graph>
<graph xaxisname='Continent' yaxisname='Export' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='Global Export' subcaption='In Millions Tonnes per annum pr Hectare' >
<categories font='Arial' fontSize='11' fontColor='000000'>
<category name='N. America' hoverText='North America'/>
<category name='Asia' />
<category name='Europe' />
<category name='Australia' />
<category name='Africa' />
</categories>
<dataset seriesname='Rice' color='FDC12E'>
<set value='30' />
<set value='26' />
<set value='29' />
<set value='31' />
<set value='34' />
</dataset>
<dataset seriesname='Wheat' color='56B9F9'>
<set value='67' />
<set value='98' />
<set value='79' />
<set value='73' />
<set value='80' />
</dataset>
<dataset seriesname='Grain' color='C9198D' >
<set value='27' />
<set value='25' />
<set value='28' />
<set value='26' />
<set value='10' />
</dataset>
</graph>
接下来是render()函数
view plaincopy to clipboardprint?
function render($caption, $XAxisName, $chartSWF, $XML, $chartId='char', $chartWidth=265, $chartHeight=200, $debugMode=false, $registerWithJS=true)
{
//animation=1: 开启flash,0:关闭flash效果 caption:表头标题 XAxisName:x轴标题 decimalPrecision:坐标显示小数位数 baseFontSize:字体大小 formatNumberScale:格式化坐标 numberPrefix:在数值前面加符号 showValues:显示数值
$strXML = "<graph caption='$caption' XAxisName='$XAxisName' palette='2' decimalPrecision='0' baseFontSize='12' animation='1' formatNumberScale='1' numberPrefix='' showValues='0' >";
$strXML .= $XML;
$strXML .= "</graph>";
$tempData = "//Provide entire XML data using dataXML methodnttchart_$chartId.setDataXML("$strXML");";
// Set up necessary variables for the RENDERCAHRT
$chartIdDiv = $chartId . "Div";
$ndebugMode = boolToNum($debugMode);
$nregisterWithJS = boolToNum($registerWithJS);
// create a string for outputting by the caller
$render_chart = "
<!-- START Script Block for Chart $chartId -->
<div id='$chartIdDiv' align='center'>
Chart.
</div>
<mce:script type="text/javascript"><!--
//Instantiate the Chart
var chart_$chartId = new FusionCharts("Charts/$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
$tempData
//Finally, render the chart.
chart_$chartId.render("$chartIdDiv");
// --></mce:script>
<!-- END Script Block for Chart $chartId -->";
return $render_chart;
}
function render($caption, $XAxisName, $chartSWF, $XML, $chartId='char', $chartWidth=265, $chartHeight=200, $debugMode=false, $registerWithJS=true)
{
//animation=1: 开启flash,0:关闭flash效果 caption:表头标题 XAxisName:x轴标题 decimalPrecision:坐标显示小数位数 baseFontSize:字体大小 formatNumberScale:格式化坐标 numberPrefix:在数值前面加符号 showValues:显示数值
$strXML = "<graph caption='$caption' XAxisName='$XAxisName' palette='2' decimalPrecision='0' baseFontSize='12' animation='1' formatNumberScale='1' numberPrefix='' showValues='0' >";
$strXML .= $XML;
$strXML .= "</graph>";
$tempData = "//Provide entire XML data using dataXML methodnttchart_$chartId.setDataXML("$strXML");";
// Set up necessary variables for the RENDERCAHRT
$chartIdDiv = $chartId . "Div";
$ndebugMode = boolToNum($debugMode);
$nregisterWithJS = boolToNum($registerWithJS);
// create a string for outputting by the caller
$render_chart = "
<!-- START Script Block for Chart $chartId -->
<div id='$chartIdDiv' align='center'>
Chart.
</div>
<mce:script type="text/javascript"><!--
//Instantiate the Chart
var chart_$chartId = new FusionCharts("Charts/$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
$tempData
//Finally, render the chart.
chart_$chartId.render("$chartIdDiv");
// --></mce:script>
<!-- END Script Block for Chart $chartId -->";
return $render_chart;
}
接下来就到了调用过程,简单的几句话,假设数据放在result数组里。
view plaincopy to clipboardprint?
foreach ($results as $key => $value) {
$arr[] = array('Option'=>$value['name'],'Num'=>$value['num']);
}
$xml = makexml($arr);
$fusionchart = 'Pie3D.swf';
$output = render($title,'',$fusionchart,$xml,$id);//每个图的id要不一样的,一样的话就。。。
foreach ($results as $key => $value) {
$arr[] = array('Option'=>$value['name'],'Num'=>$value['num']);
}
$xml = makexml($arr);
$fusionchart = 'Pie3D.swf';
$output = render($title,'',$fusionchart,$xml,$id);//每个图的id要不一样的,一样的话就。。。
最后在html加入js文件调用代码,把output打印出来就搞定了~
view plaincopy to clipboardprint? <mce:script type="text/javascript" src="tool/admin/vendors/FusionCharts/FusionCharts.js" mce_src="tool/admin/vendors/FusionCharts/FusionCharts.js"></mce:script> <?php echo $output ?> <mce:script type="text/javascript" src="tool/admin/vendors/FusionCharts/FusionCharts.js" mce_src="tool/admin/vendors/FusionCharts/FusionCharts.js"></mce:script> <?php echo $output ?>
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:网络转载