Permalink
Browse files

[pie] linkify pie arcs

  • Loading branch information...
moritz committed Sep 6, 2009
1 parent 5f85d7c commit 3a66db4d724c9cc2d520778851222793378ecef6
Showing with 48 additions and 42 deletions.
  1. +3 −3 lib/SVG/Plot.pm
  2. +42 −36 lib/SVG/Plot/Pie.pm
  3. +3 −3 plot.pl
View
@@ -284,15 +284,15 @@ multi method apply-coordinate-transform(*@things) {
];
}
-method linkify($key, $thing) {
+method linkify($key, *@things) {
my $link = @.links[$key];
defined($link)
?? ('a' => [
'xlink:href' => $link,
:target<_top>,
- $thing
+ @things
])
- !! $thing;
+ !! @things;
}
method wrap-in-svg-header-if-necessary(*@things, :$wrap) {
View
@@ -11,55 +11,61 @@ multi method plot(:$full = True, :$pie!) {
}
my @d := @( @.values[0]);
my $step = 2.0 * pi / [+] @d;
+ if 0 > [min] @d {
+ die "ERROR: can't plot pie chart with negative values";
+ }
my $prev-angle = 0;
my $cr = 0.35 * ($.plot-width min $.plot-height);
my $cx = 0.5 * $.plot-width;
my $cy = 0.5 * $.plot-height;
my @svg = gather for @d.kv -> $i, $v {
my $angle = $prev-angle + $v * $step;
- take $.arc(
- :start($prev-angle),
- :end($angle),
- :r($cr),
- :stroke<black>,
- :color(@.colors[$i % *]),
- );
- my $legend-angle = 0.5 * ($prev-angle + $angle);
+ my @items = gather {
+ take $.arc(
+ :start($prev-angle),
+ :end($angle),
+ :r($cr),
+ :stroke<black>,
+ :color(@.colors[$i % *]),
+ );
+ my $legend-angle = 0.5 * ($prev-angle + $angle);
- my $incr = $i % 2 ?? 0.3 !! 0;
+ my $incr = $i % 2 ?? 0.3 !! 0;
- take 'line' => [
- :style('stroke: grey; stroke-width: 1.2'),
- :x1($cx + 1.1 * $cr * cos($legend-angle)),
- :y1($cy + 1.1 * $cr * sin($legend-angle)),
- :x2($cx + (1.35 + $incr) * $cr * cos($legend-angle)),
- :y2($cy + (1.35 + $incr) * $cr * sin($legend-angle)),
- ];
+ take 'line' => [
+ :style('stroke: grey; stroke-width: 1.2'),
+ :x1($cx + 1.1 * $cr * cos($legend-angle)),
+ :y1($cy + 1.1 * $cr * sin($legend-angle)),
+ :x2($cx + (1.35 + $incr) * $cr * cos($legend-angle)),
+ :y2($cy + (1.35 + $incr) * $cr * sin($legend-angle)),
+ ];
- my ($text-anchor, $base-alignment);
- given cos($legend-angle) {
- if .abs < 0.4 {
- $text-anchor = 'middle';
- } else {
- $text-anchor = $_ > 0 ?? 'start' !! 'end';
+ my ($text-anchor, $base-alignment);
+ given cos($legend-angle) {
+ if .abs < 0.4 {
+ $text-anchor = 'middle';
+ } else {
+ $text-anchor = $_ > 0 ?? 'start' !! 'end';
+ }
}
- }
- given sin($legend-angle) {
- if .abs < 0.4 {
- $base-alignment = 'cenral';
- } else {
- $base-alignment = $_ < 0 ?? 'top' !! 'bottom';
+ given sin($legend-angle) {
+ if .abs < 0.4 {
+ $base-alignment = 'cenral';
+ } else {
+ $base-alignment = $_ < 0 ?? 'top' !! 'bottom';
+ }
}
- }
- take 'text' => [
- :x($cx + (1.5 + $incr) * $cr * cos($legend-angle)),
- :y($cy + (1.5 + $incr) * $cr * sin($legend-angle)),
- :text-anchor($text-anchor),
- :dominant-baseline($base-alignment),
- @.labels[$i],
- ] if defined @.labels[$i];
+ take 'text' => [
+ :x($cx + (1.5 + $incr) * $cr * cos($legend-angle)),
+ :y($cy + (1.5 + $incr) * $cr * sin($legend-angle)),
+ :text-anchor($text-anchor),
+ :dominant-baseline($base-alignment),
+ @.labels[$i],
+ ] if defined @.labels[$i];
+ }
+ take $.linkify($i, @items);
$prev-angle = $angle;
}
View
@@ -6,17 +6,17 @@
use SVG::Plot;
use SVG::Plot::Pie;
-my @data1 = 5, 6, 4, -3, -7, 12, 1, 1, 3, 7;
+my @data1 = map *.abs, 5, 6, 4, -3, -7, 12, 1, 1, 3, 7;
my @data2 = 2, 8, 0, 5, 6, 7, 8, -1, -1, -3;
my @labels = <the quick brown fox jumps over the lazy red dog>;
-my $svg = SVG::Plot.new(
+my $svg = SVG::Plot::Pie.new(
width => 400,
height => 350,
values => ([@data1], [@data2]),
title => 'Some data',
:@labels,
links => <http://en.wikipedia.org/wiki/The_quick_brown_fox_jumps_over_the_lazy_dog>,
- ).plot(:lines);
+ ).plot(:pie);
say SVG.serialize($svg);

0 comments on commit 3a66db4

Please sign in to comment.