Applying Fusioncharts in PHP

Source: Internet
Author: User
Using Fusioncharts in PHP
Author: zccst

The data format has Setdataurl (Srcurl) and Setjsondata (JSONSTR) Two kinds of
Usage is explained in detail in both the official demo and document.
In the actual demand, the direct reference to the official information is very convenient.


FILEURL = '. /path/to/xx.swf '; SWF file path
Srcurl = ' http://url?a=1&b=2 '; The URL of the back-end request.

First, Setdataurl (Srcurl) Way
Front end: var mychart = new Fusioncharts (FILEURL, "Mychartid", "N", "$"), Mychart.setdataurl (Srcurl); Mychart.render (" Outsource_sta_1 ");//backend: $xml ="
 
  
   
  "; for ($i = 1; $i < 6; $i + +) {    $data [] = array (' label ' = = ' m '. $i, ' Val UE ' = + $i);    $xml. = "
  
   ";} $xml. = "
 
  ";p rint $xml;

Annotation 1: The essence is data.xml, you can directly drop a data.xml-formatted file, or a URL, the return value of the URL is XML-formatted data.
Comment 2: There may be garbled characters. Use in Yii is garbled. But not in the ExtJS.


Second, Setjsondata (JSONSTR)
There are two kinds of situations:
The first case is new Fusioncharts (...). On the front end, only Jsonstr is fetched from the back end
For example:
Front End: $.post (Srcurl,null,function (r) {    var mychart = new Fusioncharts (FILEURL, "Mychartid", "G", "$");    Mychart.setjsondata (r);    Mychart.render ("Outsource_sta_1");}, ' json ');//backend: $data = array (); for ($i = 1; $i < 6; $i + +) {    $data [] = Array (' Label ' + ' m '. $i, ' value ' = $i);} $chart = Array (); $chart [' palette '] = 2; $chart [' caption '] = "Average processing time statistics"; $chart [' xaxisname '] = "model"; $chart [' yaxisname '] = " Average processing time "; $chart [' showvalues '] = 1; $chart [' decimals '] = 2; $chart [' formatnumberscale '] = 0; $chart [' useroundedges '] = 1;$ chart[' showpercentvalues ' = 1; $ret = Array (' chart ' = $chart, ' data ' = $data);p rint json_encode ($ret);

Annotation: The return value of $.post () is the ' JSON ' type.


The second case is all on the backend, front-end directly $ (". Outsource_content"). HTML (R)
Front End: $.post (Srcurl,null,function (R) {$ (". Outsource_content"). HTML (R);}, ' html ');//backend: $data = array (); for ($i = 1; $i & Lt 6; $i + +) {$data [] = array (' label ' = = ' m '. $i, ' value ' = $i);} $chart = Array (); $chart [' palette '] = 2; $chart [' caption '] = "Average processing time statistics"; $chart [' xaxisname '] = "model"; $chart [' yaxisname '] = " Average processing time "; $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);p rint $ret;p ublic    static function Wrapscript ($scripts) {$html = ' "; 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 = <<
 
  
The return value of the annotation 1:$.post () is the ' HTML ' type. You can also test whether other types are displayed properly.
However, it cannot be in JSON format. It is essentially a set of JavaScript that has been added to the DIV and executed immediately.

Annotation 2: For the second case, you can also display two charts, (of course, you can display more than one)


Back end in Add:
$ret 2 = $ret; $ret 2 = Self::generatechart (' column2d ', $ret 2, 600,500, ' Myid2 ', ' outsource_sta_2 '); $ret 2 = self:: Wrapscript ($ret 2);p rint $ret. $ret 2;



Iii. Advanced forms of Setjsondata ()
The advanced reason is that the backend is not just the full JS of the rendered chart (Fusioncharts), but also other values, at which point the return value of $.post () is the ' JSON ' type.
Front End: $.post (Srcurl,null,function (r) {    console.log (R.V);    $ (". Outsource_content"). HTML (R.G);}, ' json ');//backend://On the above basis $return = Array (' V ' =>100, ' g ' = = $ret. $ret 2);

The location of the annotation 1:100 can place any responsible data, and it can be v1,v2 and so on.

Annotation 2: At this point the return value of $.post () is the ' JSON ' type. This must be emphasized.


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.