Created
May 30, 2013 06:50
-
-
Save CLCL/5676122 to your computer and use it in GitHub Desktop.
ストレージの利用率とかに使える単純円グラフのCSS Spliteを生成します。
出来上がりはごちら http://labo.dtpwiki.jp/pi/pi.png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# ストレージの利用率とかに使える単純円グラフのCSS Spliteを生成します。 | |
# 出来上がりはごちら http://labo.dtpwiki.jp/pi/pi.png | |
use strict; | |
use warnings; | |
use File::Spec; | |
use FindBin; | |
use Image::Magick; | |
my $color = '#ebbb38'; # デフォルトカラー | |
my %colors = ( # 指定パーセント以上のときの色 | |
'50' => '#2683c9', | |
'80' => '#ed6267', | |
); | |
my $size = 72; # 一コマのサイズ | |
my $step = 5; # パーセント刻み | |
my $image_file = File::Spec->catfile( $FindBin::RealBin, 'pi.png'); | |
# 横長にCSS Spliteファイルを作ります | |
my $im = Image::Magick->new(); | |
$im->Set( size => $size * ( 1 +int( 100 / $step) ) . 'x' . $size ); | |
$im->Read('XC:none'); # PNG用背景透明 | |
# Main | |
my $x = 0; | |
my $percent = 0; | |
do { | |
circle ({ | |
size => $size, | |
percent => $percent, | |
x => $x, | |
y => 0, | |
}); | |
$percent += $step; | |
$x += $size; | |
} while ( $percent <= 100); | |
$im->Write("png:$image_file"); | |
undef $im; | |
exit; | |
sub circle { | |
my $p = shift; | |
my $size = $p->{size}; | |
my $percent = $p->{percent}; | |
my $cx = $size / 2 + $p->{x}; | |
my $cy = $size / 2 + $p->{y}; | |
my $linewidth = 6; | |
my $rx = ( $size - $linewidth ) / 2 -1; | |
my $ry = ( $size - $linewidth ) / 2 -1; | |
my $deg_start = degree( 0 ); # わっかの開始角度 | |
my $deg_end = degree( $percent ); # わっかの終了角度 | |
$deg_end = 269 if $deg_end == 270; # 100% のときは1週より少し短く | |
# 色変えルーチン | |
foreach my $level ( reverse sort keys %colors ) { | |
if ( $percent >= $level ) { | |
$color = $colors{$level}; | |
last; | |
} | |
} | |
# わっかを作る | |
if ( $percent != 0 ) { | |
$im->Draw( | |
primitive => 'ellipse', | |
points => "$cx,$cy, $rx,$ry $deg_start,$deg_end", | |
linewidth => $linewidth, | |
stroke => $color, | |
fill => 'none', | |
); | |
} | |
# 中心の円を作る | |
$im->Draw( | |
primitive => 'Circle', | |
points => "$cx,$cy " . ( ( $cx + $size / 2 ) - $linewidth ) . ",$cy", | |
linewidth => 0, | |
fill => '#ccc', # お好みで | |
); | |
} | |
# 角度計算 ellipseの指定ではてっぺんが270度なので変換する | |
sub degree { | |
my $percent = shift; | |
return ( ( $percent / 100 ) * 360 + 270 ) % 360; | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment