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?
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
, andFixedMax
, you'll also need to modifypImage::computeScale
which generates these configuration values when they're not provided.We need to make three changes:
FixedMin
Let's code
The
drawRadar
method is held inclass/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:
Let's have
FixedMin
default to0
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: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):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: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.
As expected, now let's check out our new code...
Ta-da.