Hi,
I think that the _chart_adjust_resolution is incorrect because it doesn't consider the minimum value. Essentially, the aim is to turn each value ($v) into a percentage (0-100) so that the min $v = 0 and max $v = 100.
Therefore the algorithm should be:
newValue = (( value - min) / (max - min)) * 100
To get this to work I made the following changes:
1) make a new function : _chart_get_min()
2) correct _chart_get_max() so that the initial value is 0 and not 1 (otherwise doesn't work with graphs with values <1
3) Modify _chart_adjust_resolution
My code fragements are as shown, but to be honest this isn't tested or cleaned up at the moment because I'm not sure of all the ins & outs of the code:
/**
* Return the min value of a single level array.
*/
function _chart_get_min($array) {
rsort($array, SORT_NUMERIC);
$min = is_array($array[count($array)-1]) ? 0 : $array[count($array)-1];
if (count($array)){
foreach($array AS $k => $v){
if (is_array($v)){
rsort($v, SORT_NUMERIC);
if ($v[count($v)-1] < $min){
$min= $v[v];
}
}
}
}
return $min;
}
/**
* Return the max value of a single level array.
*/
function _chart_get_max($array) {
rsort($array, SORT_NUMERIC);
$max = is_array($array[0]) ? 0 : $array[0];
if (count($array)){
foreach($array AS $k => $v){
if (is_array($v)){
rsort($v, SORT_NUMERIC);
if ($v[0] > $max){
$max = $v[0];
}
}
}
}
return $max;
}
function _chart_adjust_resolution($chart_id, &$data, $max_value = NULL) {
static $max;
static $min;
if (count($data)){
// Set max data value
if (!isset($max[$chart_id])){
$max[$chart_id] = isset($max_value) ? $max_value : _chart_get_max($data);
}
// Set min data value
$min[$chart_id] = _chart_get_min($data);
// Encoding resolution
$resoluton = 100;
// When the max is larger than the resolution
// we need to scale down the values
if ($max[$chart_id] > $resoluton){
$divider = round($max[$chart_id] / $resoluton, 1);
}
else {
$multiplier = round($resoluton / $max[$chart_id], 1);
}
foreach($data AS $k => $v){
if (is_array($v)){
_chart_adjust_resolution($chart_id, $data[$k]);
}
else {
if (is_numeric($v)){
// Adjust values
$new = 0.00;
$new = floor(( ($v - $min[$chart_id]) / ($max[$chart_id] - $min[$chart_id] ) ) * 100.00);
$data[$k] = $new;
}
}
}
}
}
Can this be checked and updated in the release?
thanks
Rich
p.s. for some reason I have to set adjust_resolution or the graph doesn't display. Maybe there is a bug somewhere else? I'll keep looking
Comments
Comment #1
13rac1 commentedDupe of #234127: Negative Data values?. Comment added in that issue referencing this code.
Comment #2
Pierre.Vriens commentedBecause of comment #13 in #234127: Negative Data values? I'm changing the status of this issue, hoping to get an answer to this question I have: which of the various suggestions in this and the other issue do we want to consider to move forward with?
Also note the parent issue I added ...
Comment #3
avpadernoI am closing this issue, as Drupal 6 is no longer supported.