|
image mapping strange when using setLabels() |
Posted by Keith on Apr-10-2012 02:37 |
|
I noticed a problem with image mapping when using setLabels() to change X-axis labels from numbers to letters. Can you help me determine whether I'm doing something wrong or there is a bug in ChartDirector?
Here is some perl code that makes 2 charts. The first one has a correct image map. The second chart has one extra line that uses setLabels(), and the image map turns out incorrectly. Code is also attached. Thanks.
#!/usr/bin/perl
#
# make plots to show image mapping not working properly with setLabels
#
use strict;
use warnings;
use CGI qw(:all);
use perlchartdir;
print header, start_html({
-title=>"Testing tooltip"}
) ;
my $g = new XYChart(300, 300);
$g->setPlotArea(20, 20, 270, 230);
my @Ydata = (5,8,5,8,7,5,6,9,1,4,25,12,22,12,15);
my @Xdata = (10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
my @Xdata_string2 = ("A","F","K","P"); # these align with the automatic X ticks at 10, 15, 20, 25
my $layer1 = $g->addLineLayer(\\@Ydata);
$layer1->setXData(\\@Xdata);
$g->layout();
$g->makeChart("../images/tooltip.png");
my $imagemap = $layer1->getHTMLImageMap(" ", " ", "title='{value|0}'");
print "<img src='/images/tooltip.png' usemap=\\"#datamap\\">";
print "<map name=\\"datamap\\">";
print $imagemap;
print "</map>";
# done with the first plot. Now make the second one that has incorrect image mapping
my $g2 = new XYChart(300, 300);
$g2->setPlotArea(20, 20, 270, 230);
my $layer2 = $g2->addLineLayer(\\@Ydata);
$layer2->setXData(\\@Xdata);
$g2->layout();
# the next line sets labels to be letters, and messes up the image mapping
$g2->xAxis()->setLabels(\\@Xdata_string2);
$g2->makeChart("../images/tooltip2.png");
my $imagemap2 = $g2->getHTMLImageMap(" ", " ", "title='{value|0}'");
print "<img src='/images/tooltip2.png' usemap=\\"#datamap2\\">";
print "<map name=\\"datamap2\\">";
print $imagemap2;
print "</map>";
print end_html;
exit 0;
|
Re: image mapping strange when using setLabels() |
Posted by Peter Kwan on Apr-11-2012 00:17 |
|
Hi Keith,
The Axis.setLabels will define the axis scale to be label based, so the axis scale will not match your data. Also, your code modifies the axis scale (with Axis.setLabels) after layout. It means the chart is created using one set of axis scale (auto-scaling during layout), but the axis then changes to a different scale. The result is undefined.
If you would like the set four axis labels to be at (10, 15, 20, 25), it is not suggested you let ChartDirector auto-scale the axis, and modify the labels afterwards. It is because there is no guarantee that ChartDirector will choose the ticks to be at (10, 15, 20, 25). It depends on your data, chart size, label rotation angle and many other factors. It is suggested you just set the axis scale directly.
$g2->xAxis()->setLinearScale(10, 25, \\@Xdata_string2);
There should not be any need to use BaseChart.layout or using Axis.setLabels afterwards.
Hope this can hlep.
Regards
Peter Kwan |
Re: image mapping strange when using setLabels() |
Posted by Keith on Apr-11-2012 03:21 |
|
Thanks Peter!
That's a great use of setLinearScale() that I would not have thought to use based on the Axis.setLinearScale manual page. Now I see that setLinearScale() is used in the Compact Line Chart example to set the tick labels as you described.
It would be useful to add the behavior when the majorTickInc field of setLinearScale() is an array to the Axis.setLinearScale manual page. |
Re: image mapping strange when using setLabels() |
Posted by Peter Kwan on Apr-12-2012 00:20 |
|
Hi Keith,
It is actually already documented in Axis.setLinearScale2.
I made a mistake of using Axis.setLinearScale instead of Axis.setLinearScale2. It still works because ChartDirector for Perl will automatically redirect setLinearScale to setLinearScale2 if it detects that third argument is an array of labels.
To avoid confusion, you may consider to change the method to setLinearScale2, so that it matches with the documentation.
Regards
Peter Kwan |
|