r73677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73676‎ | r73677 | r73678 >
Date:20:01, 24 September 2010
Author:yaron
Status:deferred
Tags:
Comment:
Replaced 'autoscale' with manual computation of the tick values for numbers, to fix handling for small and negative numbers
Modified paths:
  • /trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotBar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotBar.php
@@ -94,6 +94,8 @@
9595 $labels = array();
9696 // print all result rows
9797 $count = 0;
 98+ $max_number = 0;
 99+ $min_number = 0;
98100 while ( $row = $res->getNext() ) {
99101 $name = $row[0]->getNextObject()->getShortWikiText();
100102 foreach ( $row as $field ) {
@@ -105,7 +107,13 @@
106108 $nr = $object->getNumericValue();
107109 }
108110 $count++;
109 -
 111+ if ( $nr > $max_number ) {
 112+ $max_number = $nr;
 113+ }
 114+ if ( $nr < $min_number ) {
 115+ $min_number = $nr;
 116+ }
 117+
110118 if ( $this->m_bardirection == 'horizontal' ) {
111119 $numbers[] = "[$nr, $count]";
112120 } else {
@@ -124,7 +132,7 @@
125133 $labels_axis ="xaxis";
126134 $numbers_axis = "yaxis";
127135 $angle_val = -40;
128 - $barmargin= 30;
 136+ $barmargin = 6;
129137 if ( $this->m_bardirection == 'horizontal' ) {
130138 $labels_axis ="yaxis";
131139 $numbers_axis ="xaxis";
@@ -133,6 +141,43 @@
134142 }
135143 $barwidth = 20; // width of each bar
136144 $bardistance = 4; // distance between two bars
 145+
 146+ // Calculate the tick values for the numbers, based on the
 147+ // lowest and highest number. jqPlot has its own option for
 148+ // calculating ticks automatically - "autoscale" - but it
 149+ // currently (September 2010) fails for numbers less than 1,
 150+ // and negative numbers.
 151+ // If both max and min are 0, just escape now.
 152+ if ( $max_number == 0 && $min_number == 0 ) {
 153+ return null;
 154+ }
 155+ // Make the max and min slightly larger and bigger than the
 156+ // actual max and min, so that the bars don't directly touch
 157+ // the top and bottom of the graph
 158+ if ( $max_number > 0 ) { $max_number += .001; }
 159+ if ( $min_number < 0 ) { $min_number -= .001; }
 160+ if ( $max_number == 0 ) {
 161+ $multipleOf10 = 0;
 162+ $maxAxis = 0;
 163+ } else {
 164+ $multipleOf10 = pow( 10, floor( log( $max_number, 10 ) ) );
 165+ $maxAxis = ceil( $max_number / $multipleOf10 ) * $multipleOf10;
 166+ }
 167+ if ( $min_number == 0 ) {
 168+ $negativeMultipleOf10 = 0;
 169+ $minAxis = 0;
 170+ } else {
 171+ $negativeMultipleOf10 = -1 * pow( 10, floor( log( -1 * $min_number, 10 ) ) );
 172+ $minAxis = ceil( $min_number / $negativeMultipleOf10 ) * $negativeMultipleOf10;
 173+ }
 174+ $numbers_ticks = '';
 175+ $biggerMultipleOf10 = max( $multipleOf10, -1 * $negativeMultipleOf10 );
 176+ $lowestTick = floor( $minAxis / $biggerMultipleOf10 + .001 );
 177+ $highestTick = ceil( $maxAxis / $biggerMultipleOf10 - .001 );
 178+ for ( $i = $lowestTick; $i <= $highestTick; $i++ ) {
 179+ $numbers_ticks .= ($i * $biggerMultipleOf10) . ", ";
 180+ }
 181+
137182 $js_bar =<<<END
138183 <script type="text/javascript">
139184 jQuery.noConflict();
@@ -141,6 +186,9 @@
142187 plot1 = jQuery.jqplot('$barID', [[$numbers_str]], {
143188 title: '{$this->m_charttitle}',
144189 seriesColors: ['$this->m_barcolor'],
 190+ seriesDefaults: {
 191+ fillToZero: true
 192+ },
145193 series: [ {
146194 renderer: jQuery.jqplot.BarRenderer, rendererOptions: {
147195 barDirection: '{$this->m_bardirection}',
@@ -158,7 +206,7 @@
159207 }
160208 },
161209 $numbers_axis: {
162 - autoscale: true,
 210+ ticks: [$numbers_ticks],
163211 label: '{$this->m_numbersaxislabel}'
164212 }
165213 }

Status & tagging log