|
Error bars on a multi-bar chart? |
Posted by Steve on May-14-2021 23:53 |
|
I want to add error bars to the chart generated by this code. I understand that this is done with XYChart.addBoxWhiskerLayer but I'm not sure out how to make it work with a multi-bar chart. (This would probably be a good worked example to include in the docs because it's something people often want to do.)
#!/usr/bin/perl
# In the sample code, the ChartDirector for Perl module is assumed to be in "../lib"
use File::Basename;
use lib qw(c:/perlmodules/chartdir);
use perlchartdir;
# The data for the bar chart
my $data0 = [5.71,6.45,5.18,4.17];
my $data0upper = [5.95,6.62,5.47,4.51];
my $data0lower = [5.48,6.28,4.9,3.83];
my $data1 = [5.53,6.42,5.19,3.31];
my $data1upper = [6.1,6.85,5.78,4.01];
my $data1lower = [4.95,5.99,4.61,2.61];
my $data2 = [4.95,5.79,4.45,3.17];
my $data2upper = [5.38,6.19,4.98,3.77];
my $data2lower = [4.52,5.38,3.93,2.56];
my $labels = ["Scale 1","Scale 2","Scale 3","Scale 4"];
# Create a XYChart object of size 400 x 240 pixels
my $c = new XYChart(500, 500);
# Set the plot area at (50, 25) and of size 320 x 180. Use two alternative background colors
# (0xffffc0 and 0xffffe0)
$c->setPlotArea(150, 50, 320, 380, 0xececec, 0xdcdcdc);
# Add a legend box at (55, 18) using horizontal layout. Use 8 pt Arial font, with transparent
# background
$c->addLegend(175, 18, 0, "", 8)->setBackground($perlchartdir::Transparent);
# Add a title to the y-axis
$c->yAxis()->setTitle("Mean");
# Reserve 20 pixels at the top of the y-axis for the legend box
$c->yAxis()->setTopMargin(20);
$c->yAxis()->setLinearScale(1,7);
# Set the x axis labels
$c->xAxis()->setLabels($labels);
# Add a multi-bar layer with 3 data sets and 3 pixels 3D depth
my $layer = $c->addBarLayer2($perlchartdir::Side, 0);
$layer->addDataSet($data0, 0x6666ff, "Group 1");
$layer->addDataSet($data1, 0xff6600, "Group 2");
$layer->addDataSet($data2, 0x777777, "Group 3");
# Output the chart
$c->setOutputOptions("bmpscale=3");
$c->makeChart("multibar.png") |
Re: Error bars on a multi-bar chart? |
Posted by Peter Kwan on May-15-2021 02:47 |
|
Hi Steve,
The key API is Layer.alignLayer. This aligns the box-whisker symbols with the bars in a multi-bar chart. The complete code is:
#!/usr/bin/perl
# In the sample code, the ChartDirector for Perl module is assumed to be in "../lib"
use File::Basename;
use lib qw(c:/perlmodules/chartdir);
use perlchartdir;
# The data for the bar chart
my $data0 = [5.71,6.45,5.18,4.17];
my $data0upper = [5.95,6.62,5.47,4.51];
my $data0lower = [5.48,6.28,4.9,3.83];
my $data1 = [5.53,6.42,5.19,3.31];
my $data1upper = [6.1,6.85,5.78,4.01];
my $data1lower = [4.95,5.99,4.61,2.61];
my $data2 = [4.95,5.79,4.45,3.17];
my $data2upper = [5.38,6.19,4.98,3.77];
my $data2lower = [4.52,5.38,3.93,2.56];
my $labels = ["Scale 1","Scale 2","Scale 3","Scale 4"];
# Create a XYChart object of size 400 x 240 pixels
my $c = new XYChart(500, 500);
# Set the plot area at (50, 25) and of size 320 x 180. Use two alternative background colors
# (0xffffc0 and 0xffffe0)
$c->setPlotArea(150, 50, 320, 380, 0xececec, 0xdcdcdc);
# Add a legend box at (55, 18) using horizontal layout. Use 8 pt Arial font, with transparent
# background
$c->addLegend(175, 18, 0, "", 8)->setBackground($perlchartdir::Transparent);
# Add a title to the y-axis
$c->yAxis()->setTitle("Mean");
# Reserve 20 pixels at the top of the y-axis for the legend box
$c->yAxis()->setTopMargin(20);
$c->yAxis()->setLinearScale(1,7);
# Set the x axis labels
$c->xAxis()->setLabels($labels);
# Add a multi-bar layer with 3 data sets and 3 pixels 3D depth
my $layer = $c->addBarLayer2($perlchartdir::Side, 0);
$layer->addDataSet($data0, 0x6666ff, "Group 1");
$layer->addDataSet($data1, 0xff6600, "Group 2");
$layer->addDataSet($data2, 0x777777, "Group 3");
#
# Add the error bars
#
my $allUpper = [$data0upper, $data1upper, $data2upper];
my $allLower = [$data0lower, $data1lower, $data2lower];
for(my $i = 0; $i < scalar(@$allUpper); ++$i) {
my $errLayer = $c->addBoxWhiskerLayer(undef, undef, $allUpper->[$i], $allLower->[$i], undef, $perlchartdir::Transparent, 0xbb6633);
$errLayer->setLineWidth(2);
$errLayer->setDataGap(0.85);
$errLayer->alignLayer($layer, $i);
$errLayer->moveFront();
}
# Output the chart
$c->setOutputOptions("bmpscale=3");
$c->makeChart("multibar.png")
Regards
Peter Kwan |
|