00001 <?php
00002
00014 class SRFjqPlotPie extends SMWAggregatablePrinter {
00015
00016 protected static $m_piechartnum = 1;
00017
00018 protected $m_width;
00019 protected $m_height;
00020 protected $m_charttitle;
00021
00026 protected function handleParameters( array $params, $outputmode ) {
00027 parent::handleParameters( $params, $outputmode );
00028
00029 $this->m_width = $this->m_params['width'];
00030 $this->m_height = $this->m_params['height'];
00031 $this->m_charttitle = $this->m_params['charttitle'];
00032 }
00033
00034 public function getName() {
00035 return wfMsg( 'srf_printername_jqplotpie' );
00036 }
00037
00038 protected function loadJavascriptAndCSS() {
00039 global $wgOut;
00040 $wgOut->addModules( 'ext.srf.jqplot' );
00041 $wgOut->addModules( 'ext.srf.jqplotpie' );
00042 }
00043
00049 protected function addResources() {
00050 if ( self::$m_piechartnum > 1 ) {
00051 return;
00052 }
00053
00054
00055 if ( class_exists( 'ResourceLoader' ) ) {
00056 $this->loadJavascriptAndCSS();
00057 return;
00058 }
00059
00060 global $wgOut, $srfgScriptPath;
00061 global $srfgJQPlotIncluded;
00062
00063 $wgOut->includeJQuery();
00064
00065 if ( !$srfgJQPlotIncluded ) {
00066 $srfgJQPlotIncluded = true;
00067 $wgOut->addScript( '<!--[if IE]><script language="javascript" type="text/javascript" src="' . $srfgScriptPath . '/jqPlot/excanvas.js"></script><![endif]-->' );
00068 $wgOut->addScriptFile( "$srfgScriptPath/jqPlot/jquery.jqplot.js" );
00069 }
00070
00071 $wgOut->addScriptFile( "$srfgScriptPath/jqPlot/jqplot.pieRenderer.js" );
00072
00073
00074 $wgOut->addExtensionStyle( "$srfgScriptPath/jqPlot/jquery.jqplot.css" );
00075 }
00076
00084 protected function getFormatOutput( array $data ) {
00085 $json = array();
00086
00087 foreach ( $data as $name => $value ) {
00088 $json[] = array( $name, $value );
00089 }
00090
00091 $pie_data_str = '[' . FormatJson::encode( $json ) . ']';
00092 $pieID = 'pie' . self::$m_piechartnum;
00093
00094 self::$m_piechartnum++;
00095
00096 $chartlegend = FormatJson::encode( $this->params['chartlegend'] );
00097 $legendlocation = FormatJson::encode( $this->params['legendlocation'] );
00098 $datalabels = FormatJson::encode( $this->params['datalabels'] );
00099 $datalabeltype = FormatJson::encode( $this->params['datalabeltype'] );
00100
00101 $js_pie =<<<END
00102 <script type="text/javascript">
00103 jQuery(document).ready(function(){
00104 jQuery.jqplot.config.enablePlugins = true;
00105 plot1 = jQuery.jqplot('$pieID', $pie_data_str, {
00106 title: '$this->m_charttitle',
00107 seriesDefaults: {
00108 renderer: jQuery.jqplot.PieRenderer,
00109 rendererOptions: {
00110 showDataLabels: $datalabels,
00111 dataLabels: $datalabeltype,
00112 sliceMargin:2
00113 }
00114 },
00115 legend: { show:$chartlegend, location: $legendlocation }
00116 });
00117 });
00118 </script>
00119 END;
00120 global $wgOut;
00121 $wgOut->addScript( $js_pie );
00122
00123 $this->isHTML = true;
00124
00125 return Html::element(
00126 'div',
00127 array(
00128 'id' => $pieID,
00129 'style' => Sanitizer::checkCss( "margin-top: 20px; margin-left: 20px; width: {$this->m_width}px; height: {$this->m_height}px;" )
00130 )
00131 );
00132 }
00133
00137 public function getParameters() {
00138 $params = parent::getParameters();
00139
00140 $params['height'] = new Parameter( 'height', Parameter::TYPE_INTEGER, 400 );
00141 $params['height']->setMessage( 'srf_paramdesc_chartheight' );
00142
00143
00144 $params['width'] = new Parameter( 'width', Parameter::TYPE_STRING, '400' );
00145 $params['width']->setMessage( 'srf_paramdesc_chartwidth' );
00146
00147 $params['charttitle'] = new Parameter( 'charttitle', Parameter::TYPE_STRING, ' ' );
00148 $params['charttitle']->setMessage( 'srf_paramdesc_charttitle' );
00149
00150 $params['distributionlimit']->setDefault( 13 );
00151
00152 $params['chartlegend'] = new Parameter( 'chartlegend', Parameter::TYPE_BOOLEAN, true );
00153 $params['chartlegend']->setMessage( 'srf-paramdesc-chartlegend' );
00154
00155 $params['legendlocation'] = new Parameter( 'legendlocation', Parameter::TYPE_STRING, 'ne' );
00156 $params['legendlocation']->setMessage( 'srf-paramdesc-legendlocation' );
00157 $params['legendlocation']->addCriteria( new CriterionInArray( 'nw','n', 'ne', 'e', 'se', 's', 'sw', 'w' ) );
00158
00159 $params['datalabels'] = new Parameter( 'datalabels', Parameter::TYPE_BOOLEAN, false );
00160 $params['datalabels']->setMessage( 'srf-paramdesc-datalabels' );
00161
00162 $params['datalabeltype'] = new Parameter( 'datalabeltype', Parameter::TYPE_STRING, ' ' );
00163 $params['datalabeltype']->setMessage( 'srf-paramdesc-datalabeltype' );
00164 $params['datalabeltype']->addCriteria( new CriterionInArray( 'percent','value', 'label' ) );
00165
00166
00167 return $params;
00168 }
00169
00170 }