|
setData dislikes both an array of numbers and a string list, baffled |
Posted by LoydB on Jan-18-2011 04:06 |
|
I'm working on my first ChartDirectory Perl CGI script, using the example code to build a
Circular Label Layout pie chart.
I have two string variables, one holding the labels, one holding the data
$label = "One, Two, Three";
$data = "10, 20, 30";
When I execute this:
$c->setData($data, $label);
I get the "Error converting argument 1 to type 11DoubleArray".
Some forum searching revealed that a string of numbers won't work, it needs an array.
I've tried building the array using both
@datarray = split(/,/, $data);
and as the calculation loop runs
push (@datarray, $each-individual-value);
In both cases, when I try
When I execute this:
$c->setData(@datarray, $label);
I now get the error "Too many arguments, expecting 2 but received XX" (where XX varies
based on the number of elements in the @datarray.
I'm kind of at a loss here, what exactly does it want?
Thanks!
Loyd |
Re: setData dislikes both an array of numbers and a string list, baffled |
Posted by LoydB on Jan-18-2011 06:27 |
|
I'm getting closer. The code below doesn't crash, but also doesn't generate a chart.
If I change $strdata to:
my $strdata = [11341, 153, 35, 26, 6, 5, 5, 3, 3, 3, 2, 2, 1, 1];
and use it in the setData call, a chart displays correctly:
$c->setData($strdata, $trimlabel);
-- why is this broken below?
#!/usr/bin/perl
use File::Basename;
use lib dirname($0) =~ /(.*)/;
use CGI;
use perlchartdir;
#vars
my $strdata = "11341, 153, 35, 26, 6, 5, 5, 3, 3, 3, 2, 2, 1, 1";
my $labels = "United States, France, Slovakia, China, Belgium, United Kingdom, Argentina,
Nigeria, Netherlands, Japan, Morocco, Canada, Iran, Pakistan";
my $colors=[0x9900CC, 0x666600, 0x0000FF, 0x990000, 0xFF0000, 0xFF6600, 0xFF66FF,
0x009933, 0xFFCC99, 0x33CCFF, 0x99CCFF, 0xCCFF00, 0xFFFF00, 0x00FF33, 0x66CC33,
0xCC9933, 0x9999FF, 0xCCFFCC, 0x66FFFF, 0xCCFFFF];
# Get HTTP query parameters
my $query = new CGI;
my @datarray = split(/,/, $strdata);
my $c = new PieChart(300, 300);
$c->setBackground($c->linearGradientColor(0, 0, 0, $c->getHeight() / 2, 0xaaccff,
0xffffff), 0x888888);
$c->setRoundedFrame();
$c->setDropShadow();
if ($query->param("img") eq "0") {
$c->setPieSize(150, 150, 120);
$c->setLabelPos(-40);
} else {
$c->setPieSize(150, 150, 80);
$c->setLabelPos(20, $perlchartdir::LineColor);
}
$c->setData($datarray, $trimlabel);
$c->setColors2($perlchartdir::DataColor, $colors);
$c->setSectorStyle($perlchartdir::LocalGradientShading, 0xbb000000, 1);
# Output the chart
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG); |
Re: setData dislikes both an array of numbers and a string list, baffled |
Posted by Peter Kwan on Jan-18-2011 18:06 |
|
Hi LoydB,
Your code produces an empty chart because there is no data in the chart.
The data passed to ChartDirector in your code is:
$c->setData($datarray, $trimlabel);
However, both $datarray and $trimlabel are undefined. So there is no data.
Note that in Perl, @datarray and $datarray are completely unrelated.
Also, according to Perl syntax, you cannot really pass an array to any function. Perl will automatically flatten the array into scalar variables. You can only pass array reference to functions. In Perl syntax, the reference operator is "\\". For example:
$c->setData(\\@datarray);
In all of our sample code, array references are always used.
For more information on what is an array, what is a reference, you may refer to the Perl documentation.
Hope this can help.
Regards
Peter Kwan |
|