Data is scaled from 0 to 100 by default. if adjust_resolution is TRUE, the data values are altered to fit in with a 0- 100 data range. This does not acheive the results I need, e.g. where the theoretical maximum value is 10 but the highest value is 7, I can either have a very tiny lines at the bottom, or lines reaching the top, neither of which portray the real situation: what I want in this case is that the hightest line should reach 70% of the way up.

I have added a few lines of code (shown below) to my local version of chart.module to allow the data scaling parameters of the google API to be accessed. I've not commited my version back to CVS as I'm not sure how helpful it will be for other people. If it is useful, please feel free to make the change or assign the issue to me and I'll commit it. Hope this helps

Line 220, insert:

*     #data_scale

Line 277, insert:

  _chart_append('chds', $chart['#data_scale'],              $data); 

Line 554, insert:

    // Bar chart data scaling
    case 'chds':
      $data[$attr] .= implode(',', array($value['#min'], $value['#max']));
      break;

Line 1042, insert:

/**
* Bar chart data scaling
* 
* @param int $min
*   (optional) minimum value for data scale.
* 
* @param int $max
*   (optional) maximum value for data scale.
* 
* @return array
*/
function chart_data_scale($min = 1, $max = 100) {
  return array(       
      '#min'    => $min,  
      '#max' => $max,  
    );
}
CommentFileSizeAuthor
#14 536472-14-add_datascaling_chds.patch2.17 KBAnonymous (not verified)
#7 536472-add_datascaling_chds-reroll.patch2.29 KBxurizaemon
#4 chart.module.patch1.84 KBAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SirClickalot’s picture

This sounds like exactly what I need and what many users would also need - I'm puzzled as to why it wasn't in place anyway surely any user will need what you describe before long?

Anyway, I have made all the additions to my local version of the chart module as per your instructions but can't get it working. Here's the details.

What I want is for my line chart (comprising two data sets) to scale its actual data values (which range from between roughly 80 and 100) so that they 'fill' the whole of the vertical available space.

In order to achieve this, am I correct in thinking that the following will do the job...

I set up an array which I called data_scalers to hold the min and max values for the two series.

/* Data scaling */
$data_scalers = array();
$data_scalers[] = 80;
$data_scalers[] = 100;
$data_scalers[] = 80;
$data_scalers[] = 100;

So that the data_scalers array is filled {80,100,80,100} to accomodate both data sets.

I then followed this with ...

'#data_scale' => $data_scalers

... as the last line of the '$chart = array(.....) bit inside the brackets of course.

The only reference 'chds' that I can see in the output URI is as follows...

...&chxt=y%2Cy%2Cx&chds=%2Cchd=t%3A87.912777500%2C...

Any guidance?

Thanks

Nick

Anonymous’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
Category: support » feature

Just what I needed. Ineeded scaling.
The above code works for me when used in the following manner

<?php
$chart = array(
      '#chart_id' => 'test_chart',
      '#title' => t('My test'),
      '#type' => CHART_TYPE_GMETER,
    );
 $chart['#data_scale'] = array(
	'#min'=>-54,
        '#max'=>54);
 $chart['#data']['test'] = 0;
 $chart['#labels'][] = t('Test Label');

  echo chart_render($chart);
?>
Anonymous’s picture

Status: Active » Needs review

This should be looked at and properly patched.

Anonymous’s picture

Category: feature » task
FileSize
1.84 KB

Since the code above works fine for me when I use the google-o-meter (gom) chart type (only one tested), I decided to try my hand at a patch using the code submitted by jwinstanley above.

So, please find attached a patch. Hope this is what a patch should be.

Anonymous’s picture

Title: Data scaling » PATCH: Data scaling

Just changed the title.

ppcc’s picture

@SirClickaLot, and anyone else who wants to use this with multiple series...

you'll need to add extra array elements in the functions.

eg, for xy charts, change the code snippets like this:

<?php
   case 'chds':
      $data[$attr] .= implode(',', array($value['#minx'], $value['#maxx'], $value['#miny'], $value['#maxy']));
      break;
?>
<?php
function chart_data_scale($miny = 1, $maxy = 100, $minx = 1, $maxx = 100) {
  return array(       
      '#miny'    => $miny,  
      '#maxy' => $maxy, 
      '#minx'    => $minx,  
      '#maxx' => $maxx, 	  
    );
}
?>

And pass the values in like this:

<?php
$chart = array(
      '#chart_id' => 'test_chart',
      '#title' => t('My test'),
      '#type' => CHART_TYPE_GMETER,
    );
$chart['#data_scale'] = array(
    '#minx'=> 354,
    '#maxx'=> 872,
    '#miny'=> 0,
    '#maxy'=> 10000);

$chart['#data']['test'] = 0;
$chart['#labels'][] = t('Test Label');

  echo chart_render($chart);
?>
xurizaemon’s picture

Handles multiple ranges as per @ppcc's comment above, which I haven't tested extensively.

Rerolled against current HEAD, previous patch was no longer applying.

xurizaemon’s picture

Title: PATCH: Data scaling » Add support for data scaling (chds) parameter.
texas-bronius’s picture

Title: Add support for data scaling (chds) parameter. » Add support for data scaling (chds) parameter. (patch attached)
Status: Needs review » Reviewed & tested by the community

Thanks for the patch, folks. Works for me, too.

But the module status is "Minimally maintained" and "Maintenance fixes only". Rather than piecemeal patches of this sort, if anyone has any thoughts on #1109472: Hook chart_post_build to append custom GIC parms (to be commented on in that thread, not this one), maybe we could encourage another more encompassing, future-proof release?

j_ten_man’s picture

I think the following would be a better solution since you can have multiple series (more than just 2 like the previous patch handled):

    // Bar chart data scaling
    case 'chds':
      if (count($value) && isset($value['#max'])) {
        $data[$attr] = implode(',', $value);
      }
      elseif (count($value)) {
        $scales = array();
        foreach($value AS $i => $scale){
          $scales[] = implode(',', $scale);
        }
        $data[$attr] = implode(',', $scales);
      }
      break;
function chart_data_scale($min = 1, $max = 100) {
  return array(
      '#min' => $min,
      '#max' => $max,
    );
}

Usage:

$chart['#data_scale'][] = chart_data_scale(0, 10);
Pedro Lozano’s picture

With the patch I posted at #1109472: Hook chart_post_build to append custom GIC parms you can use a new hook to implement more API parameters.

Tilo’s picture

Many thanks to grobot
This patch works great! However, it is somehow disappointing because it is still not released.
Regards, Tilo

13rac1’s picture

I'm stuck on a large project, I'll be working on this issue queue again as soon as I am done.

Anonymous’s picture

Same patch as #7 for the latest dev version. Don't see #6 changes included though, so they aren't included in mine either.

Thanks to all involved. This solved my problems (bar-chart bars getting cut off and negative values not working). Tested with bar-chart and pie-chart.

Pierre.Vriens’s picture

Trying to get this issue moving again, refer to the parent link I added for details.

Pierre.Vriens’s picture

Title: Add support for data scaling (chds) parameter. (patch attached) » Support data scaling (chds) parameter
Category: Task » Feature request
Status: Reviewed & tested by the community » Patch (to be ported)
apaderno’s picture

Status: Patch (to be ported) » Closed (outdated)

I am closing this issue, as Drupal 6 is no longer supported.