1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#
# Chart::Pie #
# #
# written by Chart Group #
# #
# maintained by the Chart Group #
# Chart@wettzell.ifag.de #
# #
# #
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
package Chart::Pie;
use Chart::Base '2.4.1';
use GD;
use Carp;
use strict;
@Chart::Pie::ISA = qw(Chart::Base);
$Chart::Pie::VERSION = '2.4.1';
#>>>>>>>>>>>>>>>>>>>>>>>>>>#
# public methods go here #
#<<<<<<<<<<<<<<<<<<<<<<<<<<#
#>>>>>>>>>>>>>>>>>>>>>>>>>>>#
# private methods go here #
#<<<<<<<<<<<<<<<<<<<<<<<<<<<#
#Overwrite the legend methods to get the right legend
sub _draw_right_legend {
my $self = shift;
my $data = $self->{'dataref'};
my @labels = @{$data->[0]};
my ($x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush);
my $font = $self->{'legend_font'};
my $l1 = 0;
my $l2 =0;
my ($i, $j, $label, $dataset_sum);
my $max_label_len = 1;
# make sure we're using a real font
unless ((ref ($font)) eq 'GD::Font') {
croak "The subtitle font you specified isn\'t a GD Font object";
}
# get the size of the font
($h, $w) = ($font->height, $font->width);
# get the miscellaneous color
$misccolor = $self->_color_role_to_index('misc');
#find out what the sum of all datapoits is, needed for the Labels with percent
$dataset_sum = 0;
for my $j (0..$self->{'num_datapoints'})
{
if(defined $data->[1][$j])
{
$dataset_sum += $data->[1][$j];
}
}
# find out how who wide the largest label text is
foreach (@labels)
{
if ( length($_) > $l1)
{
$l1 = length($_);
}
}
for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++)
{
if ( length($data->[1][$i]) > $l2 )
{
$l2 = length($data->[1][$i]);
}
}
if ($self->{'legend_label_values'} =~ /^value$/i )
{
$max_label_len = $l1 + $l2 +1;
}
elsif ($self->{'legend_label_values'} =~ /^percent$/i )
{
$max_label_len = $l1 +7;
}
elsif ($self->{'legend_label_values'} =~ /^both$/i )
{
$max_label_len = $l1 + $l2 +9;
}
else
{
$max_label_len = $l1;
}
# find out how wide the largest label is
$width = (2 * $self->{'text_space'})
#+ ($self->{'max_legend_label'} * $w)
+ $max_label_len *$w
+ $self->{'legend_example_size'}
+ (2 * $self->{'legend_space'});
# get some starting x-y values
$x1 = $self->{'curr_x_max'} - $width;
$x2 = $self->{'curr_x_max'};
$y1 = $self->{'curr_y_min'} + $self->{'graph_border'} ;
$y2 = $self->{'curr_y_min'} + $self->{'graph_border'} + $self->{'text_space'}
+ ($self->{'num_datapoints'} * ($h + $self->{'text_space'}))
+ (2 * $self->{'legend_space'});
# box the legend off
$self->{'gd_obj'}->rectangle ($x1, $y1, $x2, $y2, $misccolor);
# leave that nice space inside the legend box
$x1 += $self->{'legend_space'};
$y1 += $self->{'legend_space'} + $self->{'text_space'};
# now draw the actual legend
for (0..$#labels)
{
# get the color
$color = $self->_color_role_to_index('dataset'.$_);
# find the x-y coords
$x2 = $x1;
$x3 = $x2 + $self->{'legend_example_size'};
$y2 = $y1 + ($_ * ($self->{'text_space'} + $h)) + $h/2;
# do the line first
$self->{'gd_obj'}->line ($x2, $y2, $x3, $y2, $color);
# reset the brush for points
$brush = $self->_prepare_brush($color, 'point',
$self->{'pointStyle' . $_});
$self->{'gd_obj'}->setBrush($brush);
# draw the point
$self->{'gd_obj'}->line(int(($x3+$x2)/2), $y2,
int(($x3+$x2)/2), $y2, gdBrushed);
# now the label
$x2 = $x3 + (2 * $self->{'text_space'});
$y2 -= $h/2;
if (defined $data->[1][$_])
{
if ( $self->{'legend_label_values'} =~ /^value$/i )
{
$self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_].' '.$data->[1][$_], $color);
}
elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
{
$label = sprintf("%s %4.2f%%",$labels[$_], $data->[1][$_] / ($dataset_sum || 1)* 100);
$self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
}
elsif ( $self->{'legend_label_values'} =~ /^both$/i )
{
if ( $data->[1][$_] =~ /\./ )
{
$label = sprintf("%s %4.2f%% %.2f",$labels[$_], $data->[1][$_] / ($dataset_sum || 1) * 100, $data->[1][$_]);
}
else
{
$label = sprintf("%s %4.2f%% %d",$labels[$_], $data->[1][$_] / ($dataset_sum || 1)* 100, $data->[1][$_]);
}
$self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
}
else
{
$self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_], $color);
}
}
}
# mark off the used space
$self->{'curr_x_max'} -= $width;
# and return
return 1;
}
## put the legend on the left of the chart
sub _draw_left_legend {
my $self = shift;
my $data = $self->{'dataref'};
my @labels = @{$data->[0]};
my ($x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush);
my $font = $self->{'legend_font'};
my $max_label_len= 1;;
my $l1 = 0;
my $l2 = 0;
my ($dataset_sum, $label);
# make sure we're using a real font
unless ((ref ($font)) eq 'GD::Font') {
croak "The subtitle font you specified isn\'t a GD Font object";
}
# get the size of the font
($h, $w) = ($font->height, $font->width);
# get the miscellaneous color
$misccolor = $self->_color_role_to_index('misc');
#find out what the sum of all datapoits is, needed for the Labels with percent
$dataset_sum = 0;
for my $j (0..$self->{'num_datapoints'}) {
if(defined $data->[1][$j]) {
$dataset_sum += $data->[1][$j];
}
}
# find out how who wide the largest label text is
foreach (@labels) {
if ( length($_) > $l1) {
$l1 = length($_);
}
}
for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++) {
if ( length($data->[1][$i]) > $l2 ) {
$l2 = length($data->[1][$i]);
}
}
if ($self->{'legend_label_values'} =~ /^value$/i ) {
$max_label_len = $l1 + $l2 +1;
}
elsif ($self->{'legend_label_values'} =~ /^percent$/i ) {
$max_label_len = $l1 +7;
}
elsif ($self->{'legend_label_values'} =~ /^both$/i ) {
$max_label_len = $l1 + $l2 +9;
}
else {
$max_label_len = $l1;
}
# find out how wide the largest label is
$width = (2 * $self->{'text_space'})
+ ($max_label_len * $w)
+ $self->{'legend_example_size'}
+ (2 * $self->{'legend_space'});
# get some base x-y coordinates
$x1 = $self->{'curr_x_min'};
$x2 = $self->{'curr_x_min'} + $width;
$y1 = $self->{'curr_y_min'} + $self->{'graph_border'} ;
$y2 = $self->{'curr_y_min'} + $self->{'graph_border'} + $self->{'text_space'}
+ ($self->{'num_datapoints'} * ($h + $self->{'text_space'}))
+ (2 * $self->{'legend_space'});
# box the legend off
$self->{'gd_obj'}->rectangle ($x1, $y1, $x2, $y2, $misccolor);
# leave that nice space inside the legend box
$x1 += $self->{'legend_space'};
$y1 += $self->{'legend_space'} + $self->{'text_space'};
# now draw the actual legend
for (0..$#labels) {
# get the color
$color = $self->_color_role_to_index('dataset'.$_);
# find the x-y coords
$x2 = $x1;
$x3 = $x2 + $self->{'legend_example_size'};
$y2 = $y1 + ($_ * ($self->{'text_space'} + $h)) + $h/2;
# do the line first
$self->{'gd_obj'}->line ($x2, $y2, $x3, $y2, $color);
# reset the brush for points
$brush = $self->_prepare_brush($color, 'point',
$self->{'pointStyle' . $_});
$self->{'gd_obj'}->setBrush($brush);
# draw the point
$self->{'gd_obj'}->line(int(($x3+$x2)/2), $y2,
int(($x3+$x2)/2), $y2, gdBrushed);
# now the label
$x2 = $x3 + (2 * $self->{'text_space'});
$y2 -= $h/2;
if ( $self->{'legend_label_values'} =~ /^value$/i ) {
$self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_].' '.$data->[1][$_], $color);
}
elsif ( $self->{'legend_label_values'} =~ /^percent$/i ) {
$label = sprintf("%s %4.2f%%",$labels[$_], $data->[1][$_] / ($dataset_sum || 1) * 100);
$self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
}
elsif ( $self->{'legend_label_values'} =~ /^both$/i ) {
if ($data->[1][$_] =~ /\./) {
$label = sprintf("%s %4.2f%% %.2f",$labels[$_], $data->[1][$_] / ($dataset_sum || 1) * 100, $data->[1][$_]);
}
else {
$label = sprintf("%s %4.2f%% %d",$labels[$_], $data->[1][$_] / ($dataset_sum || 1)* 100, $data->[1][$_]);
}
$self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
}
else {
$self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_], $color);
}
}
# mark off the used space
$self->{'curr_x_min'} += $width;
# and return
return 1;
}
## put the legend on the bottom of the chart
sub _draw_bottom_legend {
my $self = shift;
my $data = $self->{'dataref'};
my @labels =@{$data->[0]};
my ($x1, $y1, $x2, $x3, $y2, $empty_width, $max_label_width, $cols, $rows, $color, $brush);
my ($col_width, $row_height, $r, $c, $index, $x, $y, $w, $h);
my $font = $self->{'legend_font'};
my $max_label_len;
my $l1 = 0;
my $l2 = 0;
my ($dataset_sum, $j);
my $label;
# make sure we're using a real font
unless ((ref ($font)) eq 'GD::Font') {
croak "The subtitle font you specified isn\'t a GD Font object";
}
# get the size of the font
($h, $w) = ($font->height, $font->width);
# find the base x values
$x1 = $self->{'curr_x_min'} + $self->{'graph_border'} ;
# + ($self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width)
# + $self->{'tick_len'} + (3 * $self->{'text_space'});
$x2 = $self->{'curr_x_max'} - $self->{'graph_border'};
if ($self->{'y_label'}) {
$x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
}
if ($self->{'y_label2'}) {
$x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
}
#find out what the sum of all datapoits is, needed for the Labels with percent
$dataset_sum = 0;
for $j (0..$self->{'num_datapoints'})
{
if(defined $data->[1][$j])
{
$dataset_sum += $data->[1][$j];
}
}
# find out how who wide the largest label text is, especially look what kind of
# label is needed
foreach (@labels)
{
if ( length($_) > $l1)
{
$l1 = length($_);
}
}
for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++)
{
if ( length($data->[1][$i]) > $l2 )
{
$l2 = length($data->[1][$i]);
}
}
if ($self->{'legend_label_values'} =~ /^value$/i )
{
$max_label_len = $l1 + $l2 +1;
}
elsif ($self->{'legend_label_values'} =~ /^percent$/i )
{
$max_label_len = $l1 +7;
}
elsif ($self->{'legend_label_values'} =~ /^both$/i )
{
$max_label_len = $l1 + $l2 +9;
}
else
{
$max_label_len = $l1;
}
# figure out how wide the columns need to be, and how many we
# can fit in the space available
$empty_width = ($x2 - $x1) - (2 * $self->{'legend_space'});
$max_label_width = $max_label_len * $w
#$self->{'max_legend_label'} * $w
+ (4 * $self->{'text_space'}) + $self->{'legend_example_size'};
$cols = int ($empty_width / $max_label_width);
unless ($cols) {
$cols = 1;
}
$col_width = $empty_width / $cols;
# figure out how many rows we need, remember how tall they are
$rows = int ($self->{'num_datapoints'} / $cols);
unless (($self->{'num_datapoints'} % $cols) == 0) {
$rows++;
}
unless ($rows) {
$rows = 1;
}
$row_height = $h + $self->{'text_space'};
# box the legend off
$y1 = $self->{'curr_y_max'} - $self->{'text_space'}
- ($rows * $row_height) - (2 * $self->{'legend_space'});
$y2 = $self->{'curr_y_max'};
$self->{'gd_obj'}->rectangle($x1, $y1, $x2, $y2,
$self->_color_role_to_index('misc'));
$x1 += $self->{'legend_space'} + $self->{'text_space'};
$x2 -= $self->{'legend_space'};
$y1 += $self->{'legend_space'} + $self->{'text_space'};
$y2 -= $self->{'legend_space'} + $self->{'text_space'};
# draw in the actual legend
for $r (0..$rows-1) {
for $c (0..$cols-1) {
$index = ($r * $cols) + $c; # find the index in the label array
if ($labels[$index]) {
# get the color
$color = $self->_color_role_to_index('dataset'.$index);
# get the x-y coordinate for the start of the example line
$x = $x1 + ($col_width * $c);
$y = $y1 + ($row_height * $r) + $h/2;
# now draw the example line
$self->{'gd_obj'}->line($x, $y,
$x + $self->{'legend_example_size'}, $y,
$color);
# reset the brush for points
$brush = $self->_prepare_brush($color, 'point',
$self->{'pointStyle' . $index});
$self->{'gd_obj'}->setBrush($brush);
# draw the point
$x3 = int($x + $self->{'legend_example_size'}/2);
$self->{'gd_obj'}->line($x3, $y, $x3, $y, gdBrushed);
# adjust the x-y coordinates for the start of the label
$x += $self->{'legend_example_size'} + (2 * $self->{'text_space'});
$y = $y1 + ($row_height * $r);
# now draw the label
if ( $self->{'legend_label_values'} =~ /^value$/i ) {
$self->{'gd_obj'}->string($font, $x, $y, $labels[$index].' '.$data->[1][$index], $color);
#$self->{'gd_obj'}->stringTTF($color, FONT, 10, 0, $x, $y+10, $labels[$index].' '.$data->[1][$index]); ############
}
elsif ( $self->{'legend_label_values'} =~ /^percent$/i ) {
$label = sprintf("%s %4.2f%%",$labels[$index], $data->[1][$index] / ($dataset_sum || 1)* 100);
$self->{'gd_obj'}->string($font, $x, $y, $label, $color);
}
elsif ( $self->{'legend_label_values'} =~ /^both$/i ) {
if ($data->[1][$index] =~ /\./) {
$label = sprintf("%s %4.2f%% %.2f",$labels[$index], $data->[1][$index] / ($dataset_sum || 1) * 100, $data->[1][$index]);
}
else {
$label = sprintf("%s %4.2f%% %d",$labels[$index], $data->[1][$index] / ($dataset_sum || 1) * 100, $data->[1][$index]);
}
$self->{'gd_obj'}->string($font, $x, $y, $label, $color); ###
# $self->{'gd_obj'}->stringTTF($color, FONT, 10, 0, $x, $y, $label);
}
else {
$self->{'gd_obj'}->string($font, $x, $y, $labels[$index], $color);
}
}
}
}
# mark off the space used
$self->{'curr_y_max'} -= ($rows * $row_height) + $self->{'text_space'}
+ (2 * $self->{'legend_space'});
# now return
return 1;
}
## put the legend on top of the chart
sub _draw_top_legend {
my $self = shift;
my $data = $self->{'dataref'};
my ($max_label_len);
my $l1 = 0;
my $l2 = 0;
my @labels = @{$data->[0]};
my ($x1, $y1, $x2, $x3, $y2, $empty_width, $max_label_width, $cols, $rows, $color, $brush);
my ($col_width, $row_height, $r, $c, $index, $x, $y, $w, $h, $dataset_sum, $label);
my $font = $self->{'legend_font'};
# make sure we're using a real font
unless ((ref ($font)) eq 'GD::Font') {
croak "The subtitle font you specified isn\'t a GD Font object";
}
# get the size of the font
($h, $w) = ($font->height, $font->width);
#find out what the sum of all datapoits is, needed for the Labels with percent
$dataset_sum = 0;
for my $j (0..$self->{'num_datapoints'})
{
if(defined $data->[1][$j])
{
$dataset_sum += $data->[1][$j];
}
}
# get some base x coordinates
$x1 = $self->{'curr_x_min'}
+ $self->{'graph_border'};
# + $self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width
# + $self->{'tick_len'} + (3 * $self->{'text_space'});
$x2 = $self->{'curr_x_max'} - $self->{'graph_border'};
if ($self->{'y_label'})
{
$x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
}
if ($self->{'y_label2'})
{
$x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
}
# find out how who wide the largest label text is
foreach (@labels)
{
if ( length($_) > $l1)
{
$l1 = length($_);
}
}
for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++)
{
if ( length($data->[1][$i]) > $l2 )
{
$l2 = length($data->[1][$i]);
}
}
if ($self->{'legend_label_values'} =~ /^value$/i )
{
$max_label_len = $l1 + $l2 +1;
}
elsif ($self->{'legend_label_values'} =~ /^percent$/i )
{
$max_label_len = $l1 +7;
}
elsif ($self->{'legend_label_values'} =~ /^both$/i )
{
$max_label_len = $l1 + $l2 +9;
}
else
{
$max_label_len = $l1;
}
# figure out how wide the columns can be, and how many will fit
$empty_width = ($x2 - $x1) - (2 * $self->{'legend_space'});
$max_label_width = (4 * $self->{'text_space'})
# + ($self->{'max_legend_label'} * $w)
+ $max_label_len * $w
+ $self->{'legend_example_size'};
$cols = int ($empty_width / $max_label_width);
unless ($cols)
{
$cols = 1;
}
$col_width = $empty_width / $cols;
# figure out how many rows we need and remember how tall they are
$rows = int ($self->{'num_datapoints'} / $cols);
unless (($self->{'num_datapoints'} % $cols) == 0)
{
$rows++;
}
unless ($rows)
{
$rows = 1;
}
$row_height = $h + $self->{'text_space'};
# box the legend off
$y1 = $self->{'curr_y_min'};
$y2 = $self->{'curr_y_min'} + $self->{'text_space'}
+ ($rows * $row_height) + (2 * $self->{'legend_space'});
$self->{'gd_obj'}->rectangle($x1, $y1, $x2, $y2,
$self->_color_role_to_index('misc'));
# leave some space inside the legend
$x1 += $self->{'legend_space'} + $self->{'text_space'};
$x2 -= $self->{'legend_space'};
$y1 += $self->{'legend_space'} + $self->{'text_space'};
$y2 -= $self->{'legend_space'} + $self->{'text_space'};
# draw in the actual legend
for $r (0..$rows-1)
{
for $c (0..$cols-1)
{
$index = ($r * $cols) + $c; # find the index in the label array
if ($labels[$index])
{
# get the color
$color = $self->_color_role_to_index('dataset'.$index);
# find the x-y coords
$x = $x1 + ($col_width * $c);
$y = $y1 + ($row_height * $r) + $h/2;
# draw the line first
$self->{'gd_obj'}->line($x, $y,
$x + $self->{'legend_example_size'}, $y,
$color);
# reset the brush for points
$brush = $self->_prepare_brush($color, 'point',
$self->{'pointStyle' . $index});
$self->{'gd_obj'}->setBrush($brush);
# draw the point
$x3 = int($x + $self->{'legend_example_size'}/2);
$self->{'gd_obj'}->line($x3, $y, $x3, $y, gdBrushed);
# now the label
$x += $self->{'legend_example_size'} + (2 * $self->{'text_space'});
$y -= $h/2;
if ( $self->{'legend_label_values'} =~ /^value$/i )
{
$self->{'gd_obj'}->string($font, $x, $y, $labels[$index].' '.$data->[1][$index], $color);
}
elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
{
$label = sprintf("%s %4.2f%%",$labels[$index], $data->[1][$index] / ($dataset_sum || 1) * 100);
$self->{'gd_obj'}->string($font, $x, $y, $label, $color);
}
elsif ( $self->{'legend_label_values'} =~ /^both$/i )
{
if ( $data->[1][$index] =~ /\./)
{
$label = sprintf("%s %4.2f%% %.2f",$labels[$index], $data->[1][$index] / ($dataset_sum || 1) * 100, $data->[1][$index]);
}
else
{
$label = sprintf("%s %4.2f%% %d",$labels[$index], $data->[1][$index] / ($dataset_sum || 1) * 100, $data->[1][$index]);
}
$self->{'gd_obj'}->string($font, $x, $y, $label, $color);
}
else
{
$self->{'gd_obj'}->string($font, $x, $y, $labels[$index], $color);
}
}
}
}
# mark off the space used
$self->{'curr_y_min'} += ($rows * $row_height) + $self->{'text_space'}
+ 2 * $self->{'legend_space'};
# now return
return 1;
}
# Override the ticks methods for the pie charts
# as they do not always make sense.
sub _draw_x_ticks {
my $self = shift;
return;
}
sub _draw_y_ticks {
my $self = shift;
return;
}
sub _find_y_scale {
my $self = shift;
return;
}
## finally get around to plotting the data
sub _draw_data {
my $self = shift;
my $data = $self->{'dataref'};
my $misccolor = $self->_color_role_to_index('misc');
my $textcolor = $self->_color_role_to_index('text');
my $background = $self->_color_role_to_index('background');
my ($width, $height, $centerX, $centerY, $diameter, $text_diameter);
my $dataset_sum;
my ($start_degrees, $end_degrees, $label_degrees, $labelY_repeat_count);
my $max_label_len;
my ($pi, $rd2dg, $dg2rd);
my ($font, $fontW, $fontH, $labelX, $labelY);
my $label;
my ($i, $j, $color);
my $label_length;
my $degrees=0;
my $insidecolor;
my $forbidden_degrees = 0; # last occupied degree
my %labelinfo;
# set up initial constant values
$pi = 3.14159265;
$dg2rd = $pi/180; # Degree to Radians
$rd2dg = 180/$pi; # Radian to Degree
$start_degrees=0;
$end_degrees=0;
$font = $self->{'legend_font'};
$fontW = $self->{'legend_font'}->width;
$fontH = $self->{'legend_font'}->height;
$label_degrees = $labelY_repeat_count = 0;
# init the imagemap data field if they wanted it
if ($self->{'imagemap'} =~ /^true$/i)
{
$self->{'imagemap_data'} = [];
}
# find width and height
$width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
$height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
# okay, add up all the numbers of all the datasets, to get the
# sum total. This will be used to determine the percentage
# of each dataset. Obviously, negative numbers might be bad :)
$dataset_sum = 0;
for $j (0..$self->{'num_datapoints'})
{
if(defined $data->[1][$j])
{
#add to sum
$dataset_sum += $data->[1][$j];
#don't allow negativ values
if ($data->[1][$j] < 0)
{
croak "We need positiv data for a pie chart (which is not true for data[$j])!";
}
}
}
# find the longest label
# first we need the length of the values
$max_label_len = 1;
for $j (0..($self->{'num_datapoints'}-1))
{
# don't try to draw anything if there's no data
$labelinfo{$j}{data} = 'undefined';
if (defined ($data->[1][$j]))
{
$labelinfo{$j}{data} = $data->[1][$j];
$label = $data->[0][$j];
$labelinfo{$j}{labeldata} = $label;
if( defined $self->{'label_values'})
{
if($self->{'label_values'} =~ /^percent$/i)
{
$label = sprintf("%s %4.2f%%",$label, $data->[1][$j] / ($dataset_sum || 1) * 100);
}
elsif($self->{'label_values'} =~ /^value$/i)
{
if ($data->[1][$j] =~ /\./)
{
$label = sprintf("%s %.2f",$label, $data->[1][$j]);
}
else
{
$label = sprintf("%s %d",$label,$data->[1][$j]);
}
}
elsif ($self->{'label_values'} =~ /^both$/i)
{
if ($data->[1][$j] =~ /\./)
{
$label = sprintf("%s %4.2f%% %.2f",$label,
$data->[1][$j] / ($dataset_sum || 1)* 100,
$data->[1][$j]);
}
else
{
$label = sprintf("%s %4.2f%% %d",$label,
$data->[1][$j] / ($dataset_sum || 1) * 100,
$data->[1][$j]);
}
}
elsif($self->{'label_values'} =~ /^none$/i)
{
$label = sprintf("%s",$label);
}
}
$label_length = length($label);
$labelinfo{$j}{labelstring} = $label,
$labelinfo{$j}{labellength} = $label_length;
}
$max_label_len = $label_length if ( $max_label_len < $label_length );
}
$max_label_len *= $fontW;
# find center point, from which the pie will be drawn around
$centerX = int($width/2) + $self->{'curr_x_min'};
$centerY = int($height/2) + $self->{'curr_y_min'};
# always draw a circle, which means the diameter will be the smaller
# of the width and height. let enougth space for the labels
my $labeldistance = 2*$self->maximum($fontW,$fontH);
$diameter = $self->minimum($width,$height) - 2*$max_label_len - $labeldistance;
# make sure, that we have a positive diameter
if ($diameter < 0)
{
croak "I have calculated a negative diameter for the pie chart, maybe your labels are too long or the picture is too small.";
}
$text_diameter = $diameter + $labeldistance;
$self->{'gd_obj'}->arc($centerX, $centerY, $diameter, $diameter,
0, 360, $misccolor);
# for DEBUG!!
#$self->{'gd_obj'}->arc($centerX, $centerY, $text_diameter, $text_diameter,
# 0, 360, $misccolor);
for $j (0..($self->{'num_datapoints'}-1))
{
#next if $labelinfo{$j}{data} eq 'undefined';
# get the color for this datapoint, take the color of the datasets
$color = $self->_color_role_to_index('dataset'.$j);
$label = $labelinfo{$j}{labelstring};
$label_length = $labelinfo{$j}{labellength};
# The first value starts at 0 degrees, each additional dataset
# stops where the previous left off, and since I've already
# calculated the sum_total for the whole graph, I know that
# the final pie slice will end at 360 degrees.
# So, get the degree offset for this dataset
$end_degrees = $start_degrees + ($data->[1][$j] / ($dataset_sum || 1) * 360);
$degrees = $start_degrees+($end_degrees-$start_degrees)/2;
# stick the label in the middle of the slice
$label_degrees = ($start_degrees + $end_degrees) / 2;
# The following drawings are in a very specific ordering, and are not
# intuitive as to why they are being done this way, but it is basically
# because the GD module doesn't provide a filledArc() method. So, I
# developed my own, below.
# First, draw an arc, in black, from the starting offset, all the
# way to 360 degrees.
$self->{'gd_obj'}->arc($centerX,$centerY,
$diameter, $diameter,
$start_degrees, 360,
$misccolor);
# This is tricky, but draw a short line in the desired color, along the
# path that will be the end of this pie slice of data. But, make sure not
# to extend this line to intersect with the boundary of the arc. This
# is crucial.
if ( $start_degrees != $end_degrees )
{
$self->{'gd_obj'}->line($centerX, $centerY,
$centerX + .4*$diameter*cos($end_degrees*$dg2rd),
$centerY + .4*$diameter*sin($end_degrees*$dg2rd),
$color);
# Draw the radius of the beginning side of the pie slice, in black
$self->{'gd_obj'}->line($centerX,$centerY,
$centerX + .5*$diameter*cos($start_degrees*$dg2rd),
$centerY + .5*$diameter*sin($start_degrees*$dg2rd),
$misccolor);
# Now, execute fillToBorder, starting from a point on the end line, in the
# desired pie slice color, and fill until a black pixel if encountered.
# What this means, is that a series of pie slices is drawn, each starting
# at the correct location, but each ending at 360 degrees.
$self->{'gd_obj'}->fillToBorder(
$centerX + .4*$diameter*cos($end_degrees*$dg2rd),
$centerY + .4*$diameter*sin($end_degrees*$dg2rd),
$misccolor, $color
);
}
# Figure out where to place the label
if ( $j == 0 )
{
$forbidden_degrees = $rd2dg*atan2($fontH,0.5*$text_diameter);
}
else
{
$label_degrees = $self->maximum($label_degrees,$forbidden_degrees);
my $winkel = cos($label_degrees*$dg2rd);
my $h;
$winkel = abs($winkel);
if ( abs($winkel) < 0.01 )
{
$h = 0;
}
else
{
$h = $fontH/$winkel;
}
my $atan = atan2($h,0.5*$text_diameter); # -pi ... +pi
#print $j.",". $atan*$rd2dg.",".cos($label_degrees*$dg2rd).",".$h.",".$label_degrees."\n";
$forbidden_degrees = $label_degrees + $rd2dg*$atan;
}
$labelX = $centerX+$text_diameter*0.5*cos($label_degrees*$dg2rd);
$labelY = $centerY+$text_diameter*0.5*sin($label_degrees*$dg2rd);
# # For debugging
# # Draw Point
# # reset the brush for points
# my $brush = $self->_prepare_brush($color, 'point',
# $self->{'pointStyle' . '0'});
# $self->{'gd_obj'}->setBrush($brush);
#
# # draw the point
# $self->{'gd_obj'}->line($labelX, $labelY, $labelX, $labelY, gdBrushed);
# # end for debugging
# Okay, if a bunch of very small datasets are close together, they can
# overwrite each other. The following if statement is to help keep
# labels of neighbor datasets from being overlapped. It ain't perfect,
# but it does a pretty good job.
if ( ($label_degrees >= 270 && $label_degrees <= 360) ||
($label_degrees >= 0 && $label_degrees <= 90 ) )
{
# right side of the circle
# as 0 degrees means east
$self->{'gd_obj'}->string($font, $labelX, $labelY-$fontH*0.5,
$label, $textcolor); # $textcolor would mark everything black
}
else
{
$self->{'gd_obj'}->string($font, $labelX-length($label)*$fontW, $labelY-$fontH*0.5,
$label, $textcolor); # $textcolor would mark everything black
}
if ($self->{'legend_lines'} =~ /^true$/i)
{
$self->{'gd_obj'}->line(
$centerX+0.5*$diameter*cos($degrees*$dg2rd),
$centerY+0.5*$diameter*sin($degrees*$dg2rd),
$labelX, $labelY, $color
);
}
# reset starting point for next dataset and continue.
$start_degrees = $end_degrees;
}
# print "Center $centerX, $centerY\n";
# print "Durchmesser $diameter\n";
# print "Hintergrund $background\n";
if ( defined($self->{'ring'}) && abs($self->{'ring'}) < 1 )
{
# print "bground $bground\n";
my $hole = (1- abs($self->{'ring'}));
if ($self->{'grey_background'} =~ /^true$/i)
{
$insidecolor = $self->_color_role_to_index('grey_background');
}
else
{
$insidecolor = $background;
}
$self->{'gd_obj'}->filledArc($centerX, $centerY,
$hole*$diameter,
$hole*$diameter,
0, 360, $insidecolor);
$self->{'gd_obj'}->arc($centerX, $centerY,
$hole*$diameter,
$hole*$diameter,
0, 360, $misccolor);
}
# and finaly box it off
$self->{'gd_obj'}->rectangle ($self->{'curr_x_min'},
$self->{'curr_y_min'},
$self->{'curr_x_max'},
$self->{'curr_y_max'},
$misccolor);
return;
}
## be a good module and return 1
1;
|