Sign in to follow this  
nemoestudio

Fusioncharts Free And Codeigniter

Recommended Posts

post-10242-058217500 1287071763_thumb.pngHello People, I've been searching all over to get FusionCharts Free over CodeIgniter and I think I almost got It. I need a little push over this: Once I load the view i get this message "No data to display": Here's what I've done so far:

 

1. I use this code as a library named fusioncharts.php, an place it in:

codeigniter >> system
->
application
->
libraries

 

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Page: FusionCharts.php
// Author: InfoSoft Global (P) Ltd.
// This page contains functions that can be used to render FusionCharts.

class Fusioncharts {

   public function __construct(){

   }

   public function encodeDataURL($strDataURL, $addNoCacheStr=false) {
       //Add the no-cache string if required
       if ($addNoCacheStr==true) {
           // We add ?FCCurrTime=xxyyzz
           // If the dataURL already contains a ?, we add &FCCurrTime=xxyyzz
           // We replace : with _, as FusionCharts cannot handle : in URLs
	if (strpos(strDataURL,"?")<>0)
		$strDataURL .= "&FCCurrTime=" . Date("H_i_s");
	else
		$strDataURL .= "?FCCurrTime=" . Date("H_i_s");
       }
// URL Encode it
return urlencode($strDataURL);
   }

   public function datePart($mask, $dateTimeStr) {
       @list($datePt, $timePt) = explode(" ", $dateTimeStr);
       $arDatePt = explode("-", $datePt);
       $dataStr = "";
       // Ensure we have 3 parameters for the date
       if (count($arDatePt) == 3) {
           list($year, $month, $day) = $arDatePt;
           // determine the request
           switch ($mask) {
               case "m": return $month;
               case "d": return $day;
               case "y": return $year;
           }
           // default to mm/dd/yyyy
           return (trim($month . "/" . $day . "/" . $year));
       }

       return $dataStr;
	}

   public function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) {

if ($strXML=="")
           $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
       else
           $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";

       // Set up necessary variables for the RENDERCAHRT
       $chartIdDiv = $chartId . "Div";
       $ndebugMode = $this->boolToNum($debugMode);
       $nregisterWithJS = $this->boolToNum($registerWithJS);

   // create a string for outputting by the caller
       $render_chart = <<<RENDERCHART

                       <!-- START Script Block for Chart $chartId -->
                       <div id="$chartIdDiv" align="center">
                             Chart.
                     </div>
                     <script type="text/javascript">	
                         //Instantiate the Chart	
                         var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
                         $tempData
                         //Finally, render the chart.
                         chart_$chartId.render("$chartIdDiv");
                     </script>	
                     <!-- END Script Block for Chart $chartId -->
RENDERCHART;

       return $render_chart;
   }

   public function renderChartHTML($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode) {
       // Generate the FlashVars string based on whether dataURL has been provided
       // or dataXML.
       $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $this->boolToNum($debugMode);
       if ($strXML=="")
           // DataURL Mode
           $strFlashVars .= "&dataURL=" . $strURL;
       else
           //DataXML Mode
           $strFlashVars .= "&dataXML=" . $strXML;

       $HTML_chart = <<<HTMLCHART
                     <!-- START Code Block for Chart $chartId -->
                    	<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="$chartWidth" height="$chartHeight" id="$chartId">
                           <param name="allowScriptAccess" value="always" />
                           <param name="movie" value="$chartSWF"/>		
                           <param name="FlashVars" value="$strFlashVars" />
                           <param name="quality" value="high" />
                           <embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
                         </object>
                         <!-- END Code Block for Chart $chartId --> 
HTMLCHART;

       return $HTML_chart;
   }

   // boolToNum function converts boolean values to numeric (1/0)
   public function boolToNum($bVal) {
       return (($bVal==true) ? 1 : 0);
   }

   public function setDataXML($arrData,$caption = '',$numberPrefix) {
       $strXML = "<chart caption='".$caption."' numberPrefix='".$numberPrefix."' formatNumberScale='0'>";
//Convert data to XML and append
foreach ($arrData as $arSubData)
	$strXML .= "<set label='" . $arSubData[1] . "' value='" . $arSubData[2] . "' />";

//Close <chart> element
$strXML .= "</chart>";

       return $strXML ;
   }

}
?>

 

2. I created a controller named tes.php

 

<?php

  class Tes extends Controller {

   	public $swfCharts ;

   	public function __construct() {
       	parent::Controller() ;
       	$this->load->helper('url') ;
       	$this->load->library('fusioncharts') ;

       	$this->swfCharts  = base_url().'public/flash/FCF_MSColumn3D.swf' ;
   	}

   	public function index() {
       	//Store Name of Products
       	$arrData[0][1] = "Product A";
       	$arrData[1][1] = "Product B";
       	$arrData[2][1] = "Product C";
       	$arrData[3][1] = "Product D";
       	$arrData[4][1] = "Product E";
       	$arrData[5][1] = "Product F";

       	//Store sales data
       	$arrData[0][2] = 567500;
       	$arrData[1][2] = 815300;
       	$arrData[2][2] = 556800;
       	$arrData[3][2] = 734500;
       	$arrData[4][2] = 676800;
       	$arrData[5][2] = 648500;

       	$strXML    	= $this->fusioncharts->setDataXML($arrData,'','Rp') ;

       	$data['graph'] = $this->fusioncharts->renderChart($this->swfCharts,'',$strXML,"productSales", 600, 300, false, false) ;

       	$this->load->view('chart_view3',$data) ;
   	}
}
?>



 

 

3. And the view:

 

 


<html>
<head>
<title>Dynamic Graphs with JQuery and FusionCharts</title>
<script LANGUAGE="Javascript" SRC="<?php echo base_url()?>javascripts/FusionCharts.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
</head>
<body>

   	<?php
       	echo $graph ;
   	?>


</body>
</html>

 

4. Java script is placed in: javascripts/FusionCharts.js

5. Swf Charts in: public/flash

 

Perhaps is an XML issue, but I've really worked over this and I haven't figured it out,

 

there is similar a similar issue in this thread, but it didn't help

 

http://forum.fusionc...h__1#entry30695

 

Thanks a lot, best regards!

Edited by nemoestudio

Share this post


Link to post
Share on other sites

I've solved It, heres the function inside the controller:


/* here's the array*/

           	$j = 0;
           	foreach($categories as $row){
           	$data['name_bar'][$j]= $row->name;; 
           	$data['percent_bar'][$j]= $row->percent;
           	$j++; 
            	} 

 

And here is the view:

 

 # Create object for Column 3D chart
 $FC = new FusionCharts("Column3D","650","400");

 # Setting Relative Path of chart swf file.
 $FC->setSwfPath(base_url()."public/flash/");

 # Store chart attributes in a variable
 $strParam="caption=Resumen Herramienta de AutoAssesment;xAxisName=Categoria;yAxisName=Porcentaje de Cumplimiento;decimalPrecision=0; formatNumberScale=0;numberSuffix=%;";

 # Set chart attributes
 $FC->setChartParams($strParam);

 # Add chart data along with category names
 $k=0; 
 foreach ($name as $row): 
 $FC->addChartData($percent_bar[$k],"name=".$name_bar[$k]); 
 $k++;  	
 endforeach;

 # Render chart
 $FC->renderChart();

 

I Hope it could help!

Share this post


Link to post
Share on other sites
Guest Basundhara Ghosal

Hi,

 

Thanks for sharing your idea.

 

We are glad that your issue is resolved.

 

Happy FusionCharting. :)

Share this post


Link to post
Share on other sites

Hello nemoestudio,

 

I just did what you mentioned in this post from the beginning.

But I too got the same nodata issue.

You told that you solved by adding two blocks of code one in controller and the other in the view.

 

When I added those two changes, I am getting the following errors.

 

 

1. Call to undefined method Fusioncharts::setSwfPath()

this is from the view and code is

  # Create object for Column 3D chart
 $FC = new FusionCharts("Column3D","650","400");

 # Setting Relative Path of chart swf file.
 $FC->setSwfPath(base_url()."public/flash/");

 # Store chart attributes in a variable
 $strParam="caption=Resumen Herramienta de AutoAssesment;xAxisName=Categoria;yAxisName=Porcentaje de Cumplimiento;decimalPrecision=0; formatNumberScale=0;numberSuffix=%;";

 # Set chart attributes
 $FC->setChartParams($strParam);

 # Add chart data along with category names
 $k=0; 
 foreach ($name as $row): 
 $FC->addChartData($percent_bar[$k],"name=".$name_bar[$k]); 
 $k++;      
 endforeach;

 # Render chart
 $FC->renderChart();

 

 

2. Message: Undefined variable: categories

and

Message: Invalid argument supplied for foreach()

 

occuring at

 


/* here's the array*/

           	$j = 0;
           	foreach($categories as $row){
           	$data['name_bar'][$j]= $row->name;; 
           	$data['percent_bar'][$j]= $row->percent;
           	$j++; 
        		} 

 

 

Can you please tell me where the problem is?

 

 

Thanks,

Raj

 

 

I've solved It, heres the function inside the controller:


/* here's the array*/

           	$j = 0;
           	foreach($categories as $row){
           	$data['name_bar'][$j]= $row->name;; 
           	$data['percent_bar'][$j]= $row->percent;
           	$j++; 
        		} 

 

And here is the view:

 

 # Create object for Column 3D chart
 $FC = new FusionCharts("Column3D","650","400");

 # Setting Relative Path of chart swf file.
 $FC->setSwfPath(base_url()."public/flash/");

 # Store chart attributes in a variable
 $strParam="caption=Resumen Herramienta de AutoAssesment;xAxisName=Categoria;yAxisName=Porcentaje de Cumplimiento;decimalPrecision=0; formatNumberScale=0;numberSuffix=%;";

 # Set chart attributes
 $FC->setChartParams($strParam);

 # Add chart data along with category names
 $k=0; 
 foreach ($name as $row): 
 $FC->addChartData($percent_bar[$k],"name=".$name_bar[$k]); 
 $k++;  	
 endforeach;

 # Render chart
 $FC->renderChart();

 

I Hope it could help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this