4

I'm using a Radar Chart similar to this example: https://pchart.net/doc.draw.radar.html

My data ranges from score 1 to 4, so I configured some options:

$options = array(
    'SegmentHeight' => 1,
    'Segments' => 3,
    'FixedMax' => 4
);

One problem remains: Even if my lowest score is 1, the Radar chart always has the value 0 in the center.

How can I change the minimum value in the chart to 1?

lorenz
  • 4,538
  • 1
  • 27
  • 45
  • What if you drop your lowest value? Then that would default to the center, correct? – JSuar Dec 23 '13 at 14:16
  • I also thought that, but it doesn't work. The lowest value is 1 and still the radar shows the 0 value. – lorenz Dec 27 '13 at 09:38
  • What if you subtract one from all your values, then your range would be 0-3. That would hopefully place your values in the center. Then you could change the labels to show 1-4. – JSuar Dec 28 '13 at 15:40

1 Answers1

4

You can create a "minimum" value, but only if you're willing to fork the library and make some changes.

Game plan

The easiest and most straightforward way to do so is to create a new option that I'll call FixedMin. If, and only if, this new option is provided (and the other criteria for non-auto-segmentation are met) will you achieve the effect you're seeking.

If you want a minimum to be generated without providing SegmentHeight, Segments, and FixedMax, you'll also need to modify pImage::computeScale which generates these configuration values when they're not provided.

We need to make three changes:

  1. Create a new configuration option called FixedMin
  2. Adjust the values' position
  3. Adjust the labels' text

Let's code

The drawRadar method is held in class/pRadar.class.php. Open it up. Let's walk through our game plan.

First, let's add the configuration option. I'll add it with the others (line ~38) like so:

 $FixedMin      = isset($Format["FixedMin"]) ? $Format["FixedMin"] : 0;

Let's have FixedMin default to 0 because that is the default pChart behavior.

Second, we need to somehow trick pChart into repositioning these larger values as if they were smaller to accomodate the offset FixedMin creates.

We can do that where the function computes the plots position (line ~319). Find the loop foreach($DataS["Data"] as $Key => $Value) (line ~328). Here, we'll modify the $Value by adding this line at the top of the loop:

$Value -= $FixedMin; // Let's offset the perceived value by our new minimum

Third, we need to change the axis labels' text values. Inside the conditional that $DrawAxisValues encompasses, you'll find this line of code (line ~255):

$Label  = ($j * $SegmentHeight)

This works great; for each segment, it generates a label that is the segment times the height (e.g., 4 segments of 20 units should generate 4 labels with: 20, 40, 60, 80). But it won't work with our FixedMin. Let's reuse the offset. In this case, we're incrementing by the offset, to generate the illusion of a minimum. Replace that line with this one:

$Label  = ($j * $SegmentHeight) + $FixedMin;

Recap

We created a new configuration variable for a minimum segment; and for each value, we subtracted that minimum value (or offset); and rejiggered the axis labels by adding that minimum value (or offset).

Caveats

We only modified radar charts; no other chart type will be affected. Do not attempt to use data with values that are below the configured minimum. The hazards of doing so may be why the author didn't include this option.

Show and tell

I don't have access to your data set, so I used the pChart example and bumped the "application review" sample so that all the scores were between 20 and 40.

// FixedMin set to 0, "default" behavior
$Options = array(
 'SegmentHeight' => 20,
 'Segments' => 2,
 'FixedMax' => 40,
 'FixedMin' => 0, // And so on...

Default behavior

As expected, now let's check out our new code...

// FixedMin set to 20, smaller SegmentHeight
$Options = array(
 'SegmentHeight' => 10,
 'Segments' => 2,
 'FixedMax' => 40,
 'FixedMin' => 20, // And so on...

With a minimum of 20

Ta-da.

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
  • I'm awarding you with the bounty for the research and proof of concept (before time runs out) and will accept the answer as soon as I implemented it. Thanks a lot, Jacob! – lorenz Dec 30 '13 at 18:53