|
Is it possible to have multi-bar box whisker chart? |
Posted by John Doe on Aug-02-2012 08:02 |
|
Hello,
Is it possible to have a multi-bar box whisker chart? If it is, can you please show me some
code (in PHP) to point me in the right direction?
Thanks! |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by Peter Kwan on Aug-03-2012 01:03 |
|
Hi John,
There are several methods. I have attached an example based on the "Box-Whisker Chart" sample code. I modified it by adding 3 box-whisker layers, and offset the layers by a small amount (0.3 x-axis units). This results in the attached chart.
Hope this can help.
Regards
Peter Kwan
|
boxwhisker.php |
---|
<?php
require_once("../lib/phpchartdir.php");
# Sample data for the Box-Whisker chart. Represents the minimum, 1st quartile,
# medium, 3rd quartile and maximum values of some quantities
$Q0Data = array(40, 45, 40, 30, 20, 50, 25, 44);
$Q1Data = array(55, 60, 50, 40, 38, 60, 51, 60);
$Q2Data = array(62, 70, 60, 50, 48, 70, 62, 70);
$Q3Data = array(70, 80, 65, 60, 53, 78, 69, 76);
$Q4Data = array(80, 90, 75, 70, 60, 85, 80, 84);
# The labels for the chart
$labels = array("Group A", "Group B", "Group C", "Group D", "Group E", "Group F",
"Group G", "Group H");
# Create a XYChart object of size 550 x 250 pixels
$c = new XYChart(650, 250);
# Set the plotarea at (50, 25) and of size 450 x 200 pixels. Enable both horizontal
# and vertical grids by setting their colors to grey (0xc0c0c0)
$plotAreaObj = $c->setPlotArea(50, 25, 450, 200);
$plotAreaObj->setGridColor(0xc0c0c0, 0xc0c0c0);
# Add a title to the chart
$c->addTitle("Computer Vision Test Scores");
# Set the labels on the x axis and the font to Arial Bold
$c->xAxis->setLinearScale(0, count($labels) - 1, $labels);
$c->xAxis->setLabelStyle("arialbd.ttf");
# Set the font for the y axis labels to Arial Bold
$c->yAxis->setLabelStyle("arialbd.ttf");
# Add a Box Whisker layer using light blue 0x9999ff as the fill color and blue (0xcc)
# as the line color. Set the line width to 2 pixels
$boxWhiskerLayerObj = $c->addBoxWhiskerLayer($Q3Data, $Q1Data, $Q4Data, $Q0Data,
$Q2Data, 0x9999ff, 0x0000cc);
$boxWhiskerLayerObj->setLineWidth(2);
$boxWhiskerLayerObj->setXData(-0.3, count($Q0Data) - 1 - 0.3);
$boxWhiskerLayerObj->setDataGap(0.8);
# Add a Box Whisker layer using light blue 0x9999ff as the fill color and blue (0xcc)
# as the line color. Set the line width to 2 pixels
$boxWhiskerLayerObj = $c->addBoxWhiskerLayer($Q3Data, $Q1Data, $Q4Data, $Q0Data,
$Q2Data, 0xff9999, 0xcc0000);
$boxWhiskerLayerObj->setLineWidth(2);
$boxWhiskerLayerObj->setDataGap(0.8);
# Add a Box Whisker layer using light blue 0x9999ff as the fill color and blue (0xcc)
# as the line color. Set the line width to 2 pixels
$boxWhiskerLayerObj = $c->addBoxWhiskerLayer($Q3Data, $Q1Data, $Q4Data, $Q0Data,
$Q2Data, 0x99ff99, 0x00cc00);
$boxWhiskerLayerObj->setLineWidth(2);
$boxWhiskerLayerObj->setXData(+0.3, count($Q0Data) - 1 + 0.3);
$boxWhiskerLayerObj->setDataGap(0.8);
$legendBox = $c->addLegend(520, 25);
$legendBox->addKey("AAAAA", 0x9999ff);
$legendBox->addKey("BBBBB", 0xff9999);
$legendBox->addKey("CCCCC", 0x99ff99);
# Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?>
|
| |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by John Doe on Aug-03-2012 06:44 |
|
That helps! Thanks! |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by at on Nov-14-2012 16:00 |
|
Hi Peter,
Pls help on how to adjust this such that it will not be limited to 3 boxwhiskers only. So
basically need an idea on how to get the offset such that the width of the box will adjust
with respect to the number of boxwhiskers per x-axis label.
Hope you can reply immediately as I need this chart to work asap. Thank you very
much. |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by Peter Kwan on Nov-15-2012 01:57 |
|
Hi at,
You can compute the box width using simple geometry.
The entire width for an x-label position is 1. Assuming the box width is w, and the gap between the boxes in the same x-label position is 0.2w (20% of the box-width), and that the gap between the boxes in the adjacent x-label positions is 0.6w, we can easily solve w and get:
w = 1 / (1.2 * noOfBoxes + 0.4)
For a box-whisker layer, you can set the box width to w by using setDataGap to set the gap to (1 - w).
Similarly, it is easy to show that the x-offset for box N is:
offset = w * (0.8 + 1.2 * N) - 0.5
In PHP, the code would be like:
#The box series at position N (N = 0, 1, 2, 3 ...)
$w = 1 / (1.2 * $noOfBoxes + 0.4);
$layer->setDataGap(1 - $w);
$xOffset = $w * (0.8 + 1.2 * $N) - 0.5
$boxWhiskerLayerObj->setXData($xOffset, count($Q0Data) - 1 + $xOffset);
Hope this can help.
Regards
Peter Kwan |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by at on Nov-16-2012 15:08 |
|
Hi Peter, thanks for the formula. |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by at on Feb-03-2016 18:08 |
|
Hi Peter, would it be possible to add lines that will intersect with each boxwhiskers? |
Re: Is it possible to have multi-bar box whisker chart? |
Posted by Peter Kwan on Feb-04-2016 03:38 |
|
Hi at,
If we use the sample chart in this thread as an example, do you mean you would like to
have a line layer that draws a line with 24 data points, which are located at the x positions
of the 24 boxes?
In this example, the 24 boxes are located at:
-0.3, 0, 0.3, 0.7, 1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 4.3, 4.7, 5, 5.3,
5.7, 6, 6.3, 6.7, 7, 7.3
So to achieve the line with 24 points, you just need to use those 24 x-coordinates for the
points.
$dataX = array(-0.3, 0, 0.3, 0.7, 1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 4.3, 4.7,
5, 5.3, 5.7, 6, 6.3, 6.7, 7, 7.3);
$dataY = array(.... 24 data values .....);
$layer = $c->addLineLayer($dataY, ......);
$layer->setXData($dataX);
If the above is not what you need, is it possible to attach an example chart to help me
understand what you need?
Regards
Peter Kwan |
|