在php中使用FusionCharts
作者:zccst
数据格式有setDataURL(srcUrl)和setJSONData(jsonStr)两种
用法在官方demo和document都有详细解释。
在实际需求中,直接参考官方资料还是挺方便的。
fileUrl = '../path/to/xx.swf'; //swf文件路径
srcUrl = 'http://url?a=1&b=2'; //向后端请求的url。
一、setDataURL(srcUrl)方式
//前端:<div id="outsource_sta_1"></div>var myChart = new FusionCharts(fileUrl, "myChartId", "600", "500");myChart.setDataURL(srcUrl);myChart.render("outsource_sta_1");//后端:$xml = "<chart palette=\"2\" caption=\"平均处理时间统计\" xAxisName=\"机型\" yAxisName=\"平均处理时间\" showValues=\"1\" decimals=\"2\" formatNumberScale=\"0\" useRoundEdges=\"1\" showPercentValues='1' >";for($i = 1; $i < 6; $i++){ $data[] = array('label'=>'m'.$i, 'value'=>$i); $xml .= "<set label=\"'m'.$i\" value=\"$i\" />";}$xml .= "</chart>";print $xml;
批注1:本质是data.xml,可以直接丢一个data.xml格式的文件,也可以是一个url,该url的返回值是xml格式的数据。
批注2:有可能乱码。在yii中使用会乱码。但在extjs中没有。
二、setJSONData(jsonStr)
又分两种情况:
第一种情况是new FusionCharts(...)在前端,仅jsonStr从后端获取
例如:
//前端:<div id="outsource_sta_1"></div>$.post(srcUrl,null,function(r){ var myChart = new FusionCharts(fileUrl, "myChartId", "600", "500"); myChart.setJSONData(r); myChart.render("outsource_sta_1");},'json');//后端:$data = array();for($i = 1; $i < 6; $i++){ $data[] = array('label'=>'m'.$i, 'value'=>$i);}$chart = array();$chart['palette'] = 2;$chart['caption'] = "平均处理时间统计";$chart['xAxisName'] = "机型";$chart['yAxisName'] = "平均处理时间";$chart['showValues'] = 1;$chart['decimals'] = 2;$chart['formatNumberScale'] = 0;$chart['useRoundEdges'] = 1;$chart['showPercentValues'] = 1;$ret = array('chart'=>$chart,'data'=>$data);print json_encode($ret);
批注:$.post()的返回值是'json'类型。
第二种情况是所有都在后端,前端直接$(".outsource_content").html(r)
//前端:<div id="outsource_sta_1"></div>$.post(srcUrl,null,function(r){ $(".outsource_content").html(r);},'html');//后端:$data = array();for($i = 1; $i < 6; $i++){ $data[] = array('label'=>'m'.$i, 'value'=>$i);}$chart = array();$chart['palette'] = 2;$chart['caption'] = "平均处理时间统计";$chart['xAxisName'] = "机型";$chart['yAxisName'] = "平均处理时间";$chart['showValues'] = 1;$chart['decimals'] = 2;$chart['formatNumberScale'] = 0;$chart['useRoundEdges'] = 1;$chart['showPercentValues'] = 1;$ret = array('chart'=>$chart,'data'=>$data);$ret = json_encode($ret);$ret = self::generateChart('Column2D',$ret, 600,500, 'myid1', 'outsource_sta_1');$ret = self::wrapScript($ret);print $ret;public static function wrapScript($scripts){ $html = '<script type="text/javascript">'; $html.= "\n"; $html.= $scripts; $html.= "\n"; $html.= "</script>"; return $html;}public static function generateChart($type, $data, $width=0, $height=0, $myid='', $div_id=''){ if(!$type || !$data){ return ''; } $width = intval($width)?intval($width):800; $height = intval($height)?intval($height):400; $width = $width<600?600:$width; $height = $height<400?400:$height; $url = Yii::app()->baseUrl . "/resources/fusion/{$type}.swf"; $mychartid = $myid . 'a'; $script = <<<JAVASCRIPTvar {$myid} = new FusionCharts('{$url}','{$mychartid}', '{$width}','{$height}');{$myid}.setJSONData('{$data}');{$myid}.render('{$div_id}');JAVASCRIPT; return $script;}
批注1:$.post()的返回值是'html'类型。也可以测试其他类型是否可以正常显示。
但是不能是json格式。其本质是一些已经组建好的javascript,添加到制定div后就立即执行了。
批注2:针对第二种情况,也可以显示两个图表,(当然也可以显示多个)
后端在添加:
$ret2 = $ret;$ret2 = self::generateChart('Column2D', $ret2, 600,500,'myid2','outsource_sta_2');$ret2 = self::wrapScript($ret2);print $ret.$ret2;
三、setJSONData()的高级形式
高级的原因是:后端不只是传来渲染图表(fusionCharts)的完整js,还包括其他值,此时$.post()的返回值是'json'类型。
//前端:$.post(srcUrl,null,function(r){ console.log(r.v); $(".outsource_content").html(r.g);},'json');//后端://在上面的基础上$return = array('v'=>100,'g'=>$ret.$ret2);
批注1:100的位置可以放置任意负责的数据,而且还可以v1,v2等等。
批注2:此时$.post()的返回值是'json'类型。这个必须强调。

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具