pChart 2.x forum
http://wiki.pchart.net/forum/

drawScatterBestFit() bug
http://wiki.pchart.net/forum/viewtopic.php?f=5&t=2105
Page 1 of 1

Author:  ares [ Tue Jul 26, 2011 10:17 pm ]
Post subject:  drawScatterBestFit() bug

Hi, jean-damien.

In using the best fit functionality of the scatter chart, I've come across a bug in which the best fit line isn't properly truncated if it extends beyond the top and bottom boundaries of the graph area. It turns out the calculations to manipulate the X coordinates aren't properly taking into account the slope of the plotted line in the graph coordinate system.

Ive modified my pScatter.class.php at approximately line 829 as follows:

if ((($n*$Sxx) == ($Sx*$Sx))) {
$X1 = $this->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMin"],$SerieXAxis);
$X2 = $X1;
$Y1 = $this->pChartObject->GraphAreaY1;
$Y2 = $this->pChartObject->GraphAreaY2;
} else {
$M = (($n*$Sxy)-($Sx*$Sy)) / (($n*$Sxx)-($Sx*$Sx));
$B = (($Sy)-($M*$Sx))/($n);

$X1 = $this->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMin"],$SerieXAxis);
$Y1 = $this->getPosArray($M * $Data["Axis"][$SerieXAxis]["ScaleMin"] + $B,$SerieYAxis);
$X2 = $this->getPosArray($Data["Axis"][$SerieXAxis]["ScaleMax"],$SerieXAxis);
$Y2 = $this->getPosArray($M * $Data["Axis"][$SerieXAxis]["ScaleMax"] + $B,$SerieYAxis);

$RealM = -($Y2-$Y1)/($X2-$X1);

if ( $Y1 < $this->pChartObject->GraphAreaY1 ) { $X1 = $X1 + ($this->pChartObject->GraphAreaY1-$Y1/$RealM); $Y1 = $this->pChartObject->GraphAreaY1; }
if ( $Y1 > $this->pChartObject->GraphAreaY2 ) { $X1 = $X1 + ($Y1-$this->pChartObject->GraphAreaY2)/$RealM; $Y1 = $this->pChartObject->GraphAreaY2; }
if ( $Y2 < $this->pChartObject->GraphAreaY1 ) { $X2 = $X2 - ($this->pChartObject->GraphAreaY1-$Y2)/$RealM; $Y2 = $this->pChartObject->GraphAreaY1; }
if ( $Y2 > $this->pChartObject->GraphAreaY2 ) { $X2 = $X2 - ($Y2-$this->pChartObject->GraphAreaY2)/$RealM; $Y2 = $this->pChartObject->GraphAreaY2; }
}


in lieu of the original set of if statements there. It also takes into consideration if the best fit line ends up being vertical.

Please feel free to incorporate this into your code base.

Author:  jean-damien [ Wed Aug 03, 2011 12:09 pm ]
Post subject:  Re: drawScatterBestFit() bug

Hi,

I've merged this modification in the next release code. Thank you ;o)

JD.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/