OpenFlashChart2Widget

OpenFlashChart2Widget is a simple wrapper for OpenFlashChart2 objects. It was inspired by the extension YiiOpenFlashChart, but doesn’t try to provide a new API to the open flash chart library. Instead, the Widget that comes with this extension takes an object, created using open flash chart’s PHP language binding. This has the benefit, that this API is already documented, tutorials and examples are available, and the open flash chart community knows the API and can help.

[size="5"]Requirements[/size]

I don’t know for sure what the minimal requirements are. I developed the extension using Yii 1.1.5, but since it doesn’t do anything fancy, I’d guess it will also work with much older versions of Yii.

However, the demo application, that is included in the extension, uses action parameter binding. So this will need at least Yii v1.1.4

[size="5"]Installation[/size]

  • Extract folder "OpenFlashChart2Widget" in the release file to protected/extensions

  • Configure your application to find OpenFlashChart2Loader

[size="5"]Usage[/size]

First of all, let the OpenFlashChart2Loader initialize everything




OpenFlashChart2Loader::load();



Second, create the open flash chart object using their PHP language binding




  $bar = new bar_dome();

  $bar->set_values( array(9,8,7,6,5,4,3,2,1) );

  

  $chart = new open_flash_chart();

  $chart->set_title( new title("MyChart") );

  $chart->add_element( $bar );



Third, instantiate the widget and pass the $chart object to it.




$this->widget(

  'application.extensions.OpenFlashChart2Widget.OpenFlashChart2Widget',

  array(

    'chart' => $chart,

    'width' => '100%'

  )

);



[size="5"]Resources[/size]

[size="5"]Change Log[/size]

March 27, 2011

Minor Bug fix release - v1.0.1

  • loader doesn’t touch php include path any longer

  • demo app validates user input

February 21, 2011

  • Initial release

I need your help, I have an error :

Error calling method on NPObject

i reload openflashchart2widget via javascript




<script type="text/javascript">

function reload()

{

  tmp = findSWF("openFlashChart2_0");

  x = tmp.load();

}

function findSWF(movieName) {

  if (navigator.appName.indexOf("Microsoft")!= -1) {

    return window[movieName];

  } else {

    return document[movieName];

  }

}

</script>



thank you

I’m sorry, but for me this looks like a generic flash-related error, nothing specific to the widget. And honestly, I don’t know very much about working with flash.

But may I ask what you’re trying to do? Because just in case you’re trying to modify what the chart displays, then the homepage of the original openfalshchart2 component might help. They have a example, where they toggle the content of a chart, and at least to me it looks similar to what you’re trying to do. The only difference is, that they pass new data to the tmp.load() method.

So, if you try the following:




<script type="text/javascript">


function reload()

{

  tmp = findSWF("openFlashChart2_0");

  x = tmp.load( open_flash_chart_data("someNonExisitingId") );

}


[...]


</script>



Then your graph should be replaces with an example bar graph.

Thanks Ben for your reply. I had that error, when I use ajax to reload (or load) flash chart.

I had fixed my problem.

this is my code.

in my controller:




public function actionIndex()

	{

	  if (isset($_GET['yearFrom']))

	  {

		  Yii::app()->user->setState('yearFrom',(int)$_GET['yearFrom']);

		  unset($_GET['yearFrom']);  // would interfere with pager and repetitive page size change

	  }

	  if (isset($_GET['yearTo']))

	  {

		  Yii::app()->user->setState('yearTo',(int)$_GET['yearTo']);

		  unset($_GET['yearTo']);  // would interfere with pager and repetitive page size change

	  }

	  if (isset($_GET['account']))

	  {

		  Yii::app()->user->setState('yearTo',(int)$_GET['yearTo']);

		  unset($_GET['yearTo']);  // would interfere with pager and repetitive page size change

	  }

	  OpenFlashChart2Loader::load();

	  $bar = new bar_filled( '#E2D66A', '#577261' );


	  if (isset($_POST['yearfrom']))

	  {

		$yearfrom = (int)$_POST['yearfrom'];

		$yearto = (int)$_POST['yearto'];

		$chart = new open_flash_chart();

		$chart->set_title( new title("General Ledger") );

		$accounts=Account::model()->findAllByAttributes(array('accountcode'=>$_POST['accountcode']));

		$xarray = array();

		$maxvalue = 0;

		$minvalue=0;

		foreach($accounts as $account)

		{

		  $connection=Yii::app()->db;

		  $sql = 'select sum(Debit-Credit) as currvalue from genledger where accountid = '.$account->accountid.' and year(postdate) between '.$_POST['yearfrom'].' and '.$_POST['yearto'].' group by year(postdate)';

		  $command=$connection->createCommand($sql);

		  $dataReader=$command->query();

		  $datas=array();

		  foreach($dataReader as $row)

		  {

			if ($row['currvalue'] != null)

			{

			$datas[]= (float) $row['currvalue'];

			}

			else

			  $datas[]= 0;

		  }

		  if (count($datas)>0) {

			$maxvalue = max($datas);

			$minvalue = min($datas);

		  }

		  $bar->set_values( $datas );

		  $chart->add_element( $bar );

		}

		for($i=$yearfrom;$i<=$yearto;$i++) {

		  $xarray[]=(string)$i;

		}

		$x = new x_axis();

		$x->set_labels_from_array($xarray);

		$chart->set_x_axis( $x );

		$y = new y_axis();

		if ($minvalue < 0) {

		$y->set_range( $minvalue, $maxvalue, 10);

		} else {

		  $y->set_range( 0, $maxvalue, 10);

		}

		$chart->set_y_axis( $y );

		echo CJSON::encode(array(

			  'status'=>'success',

			  'data'=>$chart->toPrettyString()

			));

	  }

	  else {

	  $this->render('index',array(

		  'model'=>$model,

		  'chart'=>$chart

	  ));

	  }

	}



in my view:




<?php

$this->breadcrumbs=array(

	'Genledgers',

);

$yearfrom=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultYearFrom']);

$yearto=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultYearTo']);

?>

<script type="text/javascript">

function findSWF(movieName) {

  if (navigator.appName.indexOf("Microsoft")!= -1) {

    return window[movieName];

  } else {

    return document[movieName];

  }

}

</script>

<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog

    'id'=>'helpdialog',

    'options'=>array(

        'title'=>'Help',

        'autoOpen'=>false,

        'modal'=>true,

        'width'=>550,

        'height'=>470,

    ),

));?>

<?php echo $this->renderPartial('_help'); ?>

<?php $this->endWidget();?>

<h1>General Ledger</h1>

<?php

$imghelp=CHtml::image(Yii::app()->request->baseUrl.'/images/help.png');

echo CHtml::link($imghelp,'#',array(

   'style'=>'cursor: pointer; text-decoration: underline;',

   'onclick'=>"$('#helpdialog').dialog('open');",

   'title'=>Yii::t('app','help')

));?>

<?php 

$this->widget(

  'application.extensions.OpenFlashChart2Widget.OpenFlashChart2Widget',

  array(

    'chart' => $chart,

    'width' => '100%',

	'height' => 400,

  )

);

?>

<form action="index.php?r=genledger/index" method="POST">

Year From : <?php echo CHtml::textField('yearfrom',$yearfrom,array('size'=>'5',

        // change 'user-grid' to the actual id of your grid!!

	  ));  ?>

To : <?php echo CHtml::textField('yearto',$yearto,array('size'=>'5',

        // change 'user-grid' to the actual id of your grid!!

	  ));  ?>

Account Code : <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

	'name'=>'accountcode',

	'value'=>'',

	'source'=>$this->createUrl('genledger/getaccount'),

	// additional javascript options for the autocomplete plugin

	'options'=>array(

			'showAnim'=>'fold',

	),

));  ?><br/><?php echo CHtml::ajaxSubmitButton('Submit',

		array('genledger/index'),

	  array(

	  'success'=>'function(data)

		{

			var x = eval("(" + data + ")");

			if (x.status == "success")

			{

			  [b]tmp = findSWF("openFlashChart2_0");

			  tmp.load(x.data);[/b]

			}

        }')); ?>

</form>

<div id="message"></div>



I’m glad it’s working now. And thanks for sharing your code. In fact, lazy loading/ updating chart data has always been on my TODO list, but I never found time to try it. :)

Just configured anonymous read access for the repository. It contains the widgets source code as well as the demos source code, which I didn’t prepare as download earlier.

Hi , I’m newbie ,

I try a demo but show error like below

An error occured while initializing yii. Please have a look at the file "C:\AppServ\www\demo\initializeYii.php" and fix its settings. Without modifications, it assumes yii is in your php include path.

Help to explain me please …

Foonmod

Alias "OpenFlashChart2Widget.OpenFlashChart2Widget" is invalid. Make sure it points to an existing PHP file.

Above is the error message I have got once I try to open a demo.

any suggestion please…

foonmod

I can fix it already …

Thank you

foonmod

Hi,

Does anybody know how to get those graphs exported into PDF/Image/Excel/…?

Thanks

A.Miguel