ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Show two pie charts in the same page
Posted by seafree on Jul-30-2013 22:06
Hi Friends,  I draw 1-piechart3d and 1-simplepiechart in the same webpage,  the two charts have diferents data, you can see here:

while($reg_dcev = $sth->fetchrow_hashref()) {
                $descrip_comp=$reg_dcev->{'descrip_comp'};
                push(@$labels, "$descrip_comp");
                $tir=$reg_dcev->{'tir'};
                push(@$data, $tir);
        }
        #--------------------------------------------------
        $sth= $dbh->prepare($sql_eventos2) or die $DBI::errstr;
        $sth->execute();
        while($reg_dcev = $sth->fetchrow_hashref()) {
                $descrip_comp2=$reg_dcev->{'descrip_comp'};
                push(@$labels2, "$descrip_comp2");
                $total=$reg_dcev->{'total'};
                push(@$data2, 5);
        }

I use perl languaje, but when I show the charts in a webpage the two charts have the same data, the code for show the 2 charts is:

##-----------------------------------------------------------------------###

my $c = new PieChart(660, 300);
$c->setLabelLayout(c.SideLayout);
$c->setPieSize(330, 140, 100);
$c->set3D();
$c->setData($data, $labels);
$c->setExplode(1);
my $chart1URL = $c->makeTmpFile("/tmp/tmpcharts");
my $imageMap = $c ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

##-----------------------------------------------------------------------###

my $c2 = new PieChart(300, 300);

if ($query->param("img") eq "0") {
    $c2->setPieSize(150, 150, 120);
    $c2->setLabelPos(10);
} else {
    $c2->setPieSize(150, 150, 80);
    $c2->setLabelPos(-30, $perlchartdir::LineColor);
}
$c2->setLabelFormat("{label}\\nUS\\${value}K\\n({percent}%)");
$c2->setData($data2, $labels2);
$c2->setExplode(0);
my $chartURL_otra = $c2->makeTmpFile("/tmp/tmpcharts");
my $imageMap_otra = $c2->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|1}'");

What it's happen, can you help me please. I uso Perl in Linux RHE.

  Re: Show two pie charts in the same page
Posted by seafree on Jul-31-2013 04:47
I use the url code:
http://www.advsofteng.com/doc/cdperldoc/multipie.htm

For test, and see one chart, what it's happen?

  Re: Show two pie charts in the same page
Posted by Peter Kwan on Jul-31-2013 17:34
Attachments:
Hi seafree,

I assume you are already familiar with how to write HTML. In HTML, each image must have a separate URL, and each URL can only deliver one image. It is the HTML standard. For example, if you have two normal images (say a company logo and a picture), you must use two URLs to insert them into a web page. You cannot use one URL to insert two separate images in a web page.

In the "Multi-Pie Chart" sample code, the three pie charts are created as three images, and three different URLs are used displayed the pie charts. The URLs are:

<img src="multipie.pl?img=0">
<img src="multipie.pl?img=1">
<img src="multipie.pl?img=2">

If you are seeing only one chart, are you using only one <img> tag with one URL? It is normal to see one chart if one <img> tag is used. To see three charts, please use 3 <img> tags.

For your other issue, you mentioned you can see two pie charts but they are using the same data. In your message, you only show part of your code (there is no HTML?), and it is using data from a database, so it is hard to determine what is your real data. I have created an example using your exact charting code, and replace the database code with hard coded data (so we can be sure the data are different), and I added the HTML code, and it displays two different charts. The code is as attached.

If you see two identical charts, is it possible the issue is in the HTML code? For example, are you displaying two different charts in your HTML code (using two <img> tags with two different URLs)? If you need further help, is it possible to provide a complete working script using hard coded data, so I can try it myself?

Regards
Peter Kwan
twopies.pl
#!/usr/bin/perl

# Include current script directory in the module path (needed on Microsoft IIS).
# This allows this script to work by copying ChartDirector to the same directory
# as the script (as an alternative to installation in Perl module directory)
use File::Basename;
use lib dirname($0) =~ /(.*)/;

use perlchartdir;
use CGI;

my $query = new CGI;

my $data = [85, 156, 179.5, 211, 123];
my $labels = ["Mon", "Tue", "Wed", "Thu", "Fri"];

my $data2 = [25, 18, 15, 12, 8, 30, 35];
my $labels2 = ["Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities",
    "Production"];


##-----------------------------------------------------------------------###

my $c = new PieChart(660, 300);
$c->setLabelLayout(c.SideLayout);
$c->setPieSize(330, 140, 100);
$c->set3D();
$c->setData($data, $labels);
$c->setExplode(1);
my $chart1URL = $c->makeTmpFile("/tmp/tmpcharts");
my $imageMap = $c->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

##-----------------------------------------------------------------------###

my $c2 = new PieChart(300, 300);

if ($query->param("img") eq "0") {
    $c2->setPieSize(150, 150, 120);
    $c2->setLabelPos(10);
} else {
    $c2->setPieSize(150, 150, 80);
    $c2->setLabelPos(-30, $perlchartdir::LineColor);
}
$c2->setLabelFormat("{label}\\nUS\\${value}K\\n({percent}%)");
$c2->setData($data2, $labels2);
$c2->setExplode(0);
my $chartURL_otra = $c2->makeTmpFile("/tmp/tmpcharts");
my $imageMap_otra = $c2->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|1}'");


print "Content-type: text/html\\n\\n";
print <<EndOfHTML
<html>
<body style="margin:5px 0px 0px 5px">
<img src="getchart.pl?img=/tmp/tmpcharts/$chart1URL" border="0" usemap="#map1">
<map name="map1">
$imageMap
</map>
<img src="getchart.pl?img=/tmp/tmpcharts/$chartURL_otra" border="0" usemap="#map1">
<map name="map1">
$imageMap_otra
</map>
</body>
</html>
EndOfHTML
;

  Re: Show two pie charts in the same page
Posted by seafree on Jul-31-2013 21:34
Hi Peter, good morning. Thanks a lot. The data were correct, but you have reason I showed the same chart, I were confuse with chart director code in html but with you clarify all is correct.

Peter really thanks a lot.

  Re: Show two pie charts in the same page
Posted by seafree on Aug-03-2013 04:28
The charts are correct now, but I must apply the same color for sector because are the same in the two charts, one represent porcent disponibility and the other represente events occurred. I fill the sector colors in the second while:


1) while($reg_dcev = $sth->fetchrow_hashref()) {
                $descrip_comp=$reg_dcev->{'descrip_comp'};
                push(@$labels, "$descrip_comp");
                $tir=$reg_dcev->{'tir'};
                push(@$data, $tir);
}


2) while($reg_dcev_event2 = $sth_event2->fetchrow_hashref()) {
                $id_comp2=$reg_dcev_event2->{'id_componente'};
                $descrip_comp2=$reg_dcev_event2->{'descrip_comp'};
                push(@$labels2, "$descrip_comp2");
                $total=$reg_dcev_event2->{'total'};
                push(@$data2, $total);
                push @sector_colors, $colors_componentes{$id_comp2};
}

###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###

my $c = new PieChart(660, 300);
$c->setColors2($perlchartdir::DataColor, $sector_colors);
$c->setLabelLayout(c.SideLayout);
$c->setPieSize(330, 140, 100);
$c->set3D();
$c->setData($data, $labels);
$c->setExplode(1);
my $chart1URL = $c->makeTmpFile("/tmp/tmpcharts");
my $imageMap = $c ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###
my $c_otra = new PieChart(660, 300);
$c_otra->setColors2($perlchartdir::DataColor, $sector_colors);
$c_otra->setLabelLayout(c.SideLayout);
$c_otra->setPieSize(330, 140, 100);
$c_otra->set3D();
$c_otra->setData($data2, $labels2);
$c_otra->setExplode(1);
my $chartURL_otra = $c_otra->makeTmpFile("/tmp/tmpcharts");
my $imageMap_otra = $c_otra ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

Can you help me please, because now I can't see the charts.

  Re: Show two pie charts in the same page
Posted by Peter Kwan on Aug-05-2013 23:35
Hi seafree,

If you cannot see the charts, would you mind to inform me what do you see? Are there any error messages?

As I do not have your data, it is hard for me to trouble-shoot your issue. For trouble-shooting, I suggest you to use hard coded data, like:

my $sector_colors = [0x888888, 0xcccccc, 0xeeeeee];

If hard coded data works normally, but with your database code the charts are not shown, then the issue may be related to the database code, or to your actual data. For example, is $colors_componentes{$id_comp2}; actually a color? (In ChartDirector in Perl, a color is an integer in Perl syntax.)

Also, just by reading your code, I can already find one error. "@sector_colors" should be "@$sector_colors".

Regards
Peter Kwan

  Re: Show two pie charts in the same page
Posted by seafree on Aug-06-2013 04:38
Hi Peter,

Your question1: If you cannot see the charts, would you mind to inform me what do you see? Are there any error messages?

I cannot see the charts and I don't see anything.

Your question2: As I do not have your data, it is hard for me to trouble-shoot your issue. For trouble-shooting, I suggest you to use hard coded data, like:

%colores_componentes = ("1","0xfe9d04", "2", "0x1c1cf0", "3", "0xffcc00", "4","0xff0000", "5","0xffd700", "6","0xff8100", "7","oxie90ff", "8","0x734a12", "9","0x0000cc", "10","0x507d2a", "11","0x00bfff","12","0x34a7b7", "13","0x1e90ff", "14","0xffff00");

I execute two querys:

while($reg_dcev = $sth->fetchrow_hashref()) {
                $descrip_comp=$reg_dcev->{'descrip_comp'};
                # The labels for the pie chart
                push(@$labels, "$descrip_comp");
                $tir=$reg_dcev->{'tir'};
                # The data for the pie chart
                push(@$data, $tir);
        } #END WHILE ($reg_dcev = $sth->fetchrow_hashref)

I buil another psql handle:

$sth_event2= $dbh->prepare($sql_eventos2) or die $DBI::errstr;
$sth_event2->execute();

while($reg_dcev_event2 = $sth_event2->fetchrow_hashref()) {
                $id_comp2=$reg_dcev_event2->{'id_componente'};
                $descrip_comp2=$reg_dcev_event2->{'descrip_comp'};
                # The labels for the pie chart
                push(@$labels2, "$descrip_comp2");
                $total=$reg_dcev_event2->{'total'};
                # The data for the pie chart
                push(@$data2, $total);
                #datacolor
                push(@$colors, $colores_componentes{$id_comp2})
        } #END WHILE ($reg_dcev = $sth->fetchrow_hashref)


Your question3: If hard coded data works normally, but with your database code the charts are not shown, then the issue may be related to the database code, or to your actual data. For example, is $colors_componentes{$id_comp2}; actually a color? (In ChartDirector in Perl, a color is an integer in Perl syntax.)

The ARRAYS values are:
LABEL: SERVICES1
LABEL: SERVICES2
DATA: 1
DATA: 5
COLOR: 0x1c1cf0
COLOR: 0xffcc00

Your Note: Also, just by reading your code, I can already find one error. "@sector_colors" should be "@$sector_colors".

###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###

my $c = new PieChart(660, 300);
$c->setLabelLayout(c.SideLayout);
$c->setPieSize(330, 140, 100);
$c->set3D();
$c->setData($data, $labels);
$c->setColors2($perlchartdir::DataColor, @$colors);
$c->setExplode(1);
my $chart1URL = $c->makeTmpFile("/tmp/tmpcharts");
my $imageMap = $c ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###
my $c_otra = new PieChart(660, 300);
$c_otra->setLabelLayout(c.SideLayout);
$c_otra->setPieSize(330, 140, 100);
$c_otra->set3D();
$c_otra->setColors2($perlchartdir::DataColor, @$colors);
$c_otra->setData($data2, $labels2);
$c_otra->setExplode(1);
my $chartURL_otra = $c_otra->makeTmpFile("/tmp/tmpcharts");
my $imageMap_otra = $c_otra ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

I can't see nothing, only I show my messages. I hope can you help me. Thanks Peter.

Regards
seafree

  Re: Show two pie charts in the same page
Posted by seafree on Aug-06-2013 22:21
Hi Peter, have any suggestion?

Regards
Xochitl

  Re: Show two pie charts in the same page
Posted by Peter Kwan on Aug-07-2013 00:03
Attachments:
Hi seafree,

If you see a blank page, it is likely your Perl script has some error, but your web server is configured not to return the error message to the browser. (Due to security reasons, or to avoid confusing the end user, some web servers may be configured not to return error messages to the browser.)

To help you to trouble-shoot the problem, I suggest you to configure the web server to display the error message to the browser, or you may read the error log file to see the error message. Another method is just to run the code from the command line (not through the web server) using the command line Perl interpreter. In this way, the error message will be displayed in the terminal window.

For your case, just be reading the code, I can see several errors:

(a) In my last message, I mentioned that @sector_colors is incorrect. However, I did not mention that $sector_colors is incorrect. In fact, the $sector_colors is correct. So in your latest charting code, the:

$c->setColors2($perlchartdir::DataColor, @$colors);

should be:

$c->setColors2($perlchartdir::DataColor, $colors);

(Please make sure you understand what is @$colors and what is $colors, and when to use them.)


(b) There are some mistakes in "%colores_componentes".

As mentioned in my last message, a color should be an integer. However, the contents in %colores_componentes are not integers or numbers. Please note that in Perl syntax "0xfe9d04" (with quotes) is not a number or integer, but 0xfe9d04 is a number and an integer. The "0xfe9d04" (with quotes) is called a text string in Perl syntax, not a number. In ChartDirector, a color must be a number according to Perl (or whatever programming language you are using).

I also see something like "oxie90ff" in %colores_componentes. This is not a number even without a quote.


I have attached an example using your exact charting code (with the modification in (a) to fix the error), and with hard coded data, and it works normally.

Hope this can help.

Regards
Peter Kwan
twopie2.pl
#!/usr/bin/perl

# Include current script directory in the module path (needed on Microsoft IIS).
# This allows this script to work by copying ChartDirector to the same directory
# as the script (as an alternative to installation in Perl module directory)
use File::Basename;
use lib dirname($0) =~ /(.*)/;

use perlchartdir;
use CGI;

my $query = new CGI;

my $data = [85, 156, 179.5, 211, 123];
my $labels = ["Mon", "Tue", "Wed", "Thu", "Fri"];

my $data2 = [25, 18, 15, 12, 8, 30, 35];
my $labels2 = ["Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities",
    "Production"];

my $colors = [0x888888, 0xcccccc, 0xeeeeee];


###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###

my $c = new PieChart(660, 300);
$c->setLabelLayout($perlchartdir::SideLayout);
$c->setPieSize(330, 140, 100);
$c->set3D();
$c->setData($data, $labels);
$c->setColors2($perlchartdir::DataColor, $colors);
$c->setExplode(1);
my $chart1URL = $c->makeTmpFile("/tmp/tmpcharts");
my $imageMap = $c ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");

###-----------------------------------------------------------------------###
###-----------------------------------------------------------------------###
my $c_otra = new PieChart(660, 300);
$c_otra->setLabelLayout($perlchartdir::SideLayout);
$c_otra->setPieSize(330, 140, 100);
$c_otra->set3D();
$c_otra->setColors2($perlchartdir::DataColor, $colors);
$c_otra->setData($data2, $labels2);
$c_otra->setExplode(1);
my $chartURL_otra = $c_otra->makeTmpFile("/tmp/tmpcharts");
my $imageMap_otra = $c_otra ->getHTMLImageMap("","","title='{dataSetName}@{xLabel}:{value|0}'");


print "Content-type: text/html\\n\\n";
print <<EndOfHTML
<html>
<body style="margin:5px 0px 0px 5px">
<img src="getchart.pl?img=/tmp/tmpcharts/$chart1URL" border="0" usemap="#map1">
<map name="map1">
$imageMap
</map>
<img src="getchart.pl?img=/tmp/tmpcharts/$chartURL_otra" border="0" usemap="#map1">
<map name="map1">
$imageMap_otra
</map>
</body>
</html>
EndOfHTML
;

  Re: Show two pie charts in the same page
Posted by seafree on Aug-07-2013 02:48
Thank a lot Peter, the chart is ok.