| 1 | # ABSTRACT: simple use chart director | ||
| 2 | package SimpleCall::ChartDirector; | ||
| 3 | |||
| 4 | require Exporter; | ||
| 5 | |||
| 6 | @ISA = qw(Exporter); | ||
| 7 | @EXPORT = qw( | ||
| 8 | chart_bar | ||
| 9 | chart_pyramid chart_pie | ||
| 10 | chart_spline chart_line | ||
| 11 | chart_percentage_bar chart_stacked_bar chart_multi_bar | ||
| 12 | chart_percentage_area chart_stacked_area | ||
| 13 | chart_scatter | ||
| 14 | ); | ||
| 15 | |||
| 16 | use Encode; | ||
| 17 | use POSIX qw/strtod/; | ||
| 18 | |||
| 19 | our $VERSION=0.06; | ||
| 20 | |||
| 21 | #需要微软雅黑字体,放到chart_director的fonts目录下 | ||
| 22 | our $CHART_FONT = 'msyh.ttf'; | ||
| 23 | our $CHART_BOLD_FONT = 'msyhbd.ttf'; | ||
| 24 | |||
| 25 | #颜色表,支持指定<DATA>中的颜色名,或者16进制值 | ||
| 26 | my @COLOR_HEXCODE = <DATA>; | ||
| 27 | our %COLOR_HEXCODE = map { chomp; split; } @COLOR_HEXCODE; | ||
| 28 | our @DEFAULT_COLORLIST = qw/LightBlue1 Green Yellow Red1 Purple | ||
| 29 | LightGoldenrod Pink4 LemonChiffon2 LightCoral Salmon3 | ||
| 30 | IndianRed4 Khaki3 Chartreuse2 SeaGreen3 LightCyan3 | ||
| 31 | PaleVioletRed3 | ||
| 32 | /; | ||
| 33 | our @DEFAULT_DATA_SYMBOL = ( | ||
| 34 | $perlchartdir::DiamondSymbol, $perlchartdir::TriangleSymbol, | ||
| 35 | $perlchartdir::CircleSymbol, $perlchartdir::SquareSymbol, | ||
| 36 | $perlchartdir::LeftTriangleSymbol, $perlchartdir::InvertedTriangleSymbol, | ||
| 37 | $perlchartdir::RightTriangleSymbol, $perlchartdir::StarSymbol, | ||
| 38 | $perlchartdir::PolygonSymbol, $perlchartdir::Polygon2Symbol, | ||
| 39 | $perlchartdir::CrossSymbol, $perlchartdir::Cross2Symbol, | ||
| 40 | $perlchartdir::GlassSphereSymbol, $perlchartdir::GlassSphere2Symbol, | ||
| 41 | $perlchartdir::SolidSphereSymbol, | ||
| 42 | ); | ||
| 43 | our @DEFAULT_BAR_SHAPE = ( | ||
| 44 | $perlchartdir::SquareShape, | ||
| 45 | $perlchartdir::DiamondShape, | ||
| 46 | $perlchartdir::TriangleShape, | ||
| 47 | $perlchartdir::RightTriangleShape, | ||
| 48 | $perlchartdir::LeftTriangleShape, | ||
| 49 | $perlchartdir::InvertedTriangleShape, | ||
| 50 | $perlchartdir::CircleShape, | ||
| 51 | $perlchartdir::GlassSphereShape, | ||
| 52 | $perlchartdir::GlassSphere2Shape, | ||
| 53 | $perlchartdir::SolidSphereShape, | ||
| 54 | perlchartdir::StarShape(6), # 3 .. 10, | ||
| 55 | perlchartdir::PolygonShape(6), #5 .. 6, | ||
| 56 | perlchartdir::Polygon2Shape(6), # 5..6 | ||
| 57 | perlchartdir::CrossShape(0.3), # 0.1 .. 0.7 | ||
| 58 | perlchartdir::Cross2Shape(0.3), # 0.1 .. 0.7 | ||
| 59 | ); | ||
| 60 | |||
| 61 | use perlchartdir; | ||
| 62 | |||
| 63 | sub set_data_label { | ||
| 64 | my ( $layer, $opt ) = @_; | ||
| 65 | |||
| 66 | if($opt->{with_data_label}){ | ||
| 67 | ##描点的旁边加上具体数据 | ||
| 68 | $layer->setDataLabelFormat( $opt->{data_label_format} ); | ||
| 69 | |||
| 70 | #画图区域内数据标识的字体 | ||
| 71 | $layer->setDataLabelStyle( $opt->{data_label_font}, | ||
| 72 | $opt->{data_label_font_size} ); | ||
| 73 | } | ||
| 74 | |||
| 75 | #总数 | ||
| 76 | $layer->setAggregateLabelFormat($opt->{data_label_format}) | ||
| 77 | if($opt->{with_aggregate_data_label}); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub set_legend { | ||
| 81 | my ( $c, $opt ) = @_; | ||
| 82 | |||
| 83 | return unless ( $opt->{with_legend} ); | ||
| 84 | |||
| 85 | my $c_legend = | ||
| 86 | $c->addLegend( $opt->{legend_pos_x}, $opt->{legend_pos_y}, | ||
| 87 | $opt->{legend_is_vertical} , | ||
| 88 | $opt->{legend_font}, $opt->{legend_font_size}, | ||
| 89 | ); | ||
| 90 | |||
| 91 | #图例背景 | ||
| 92 | $c_legend->setBackground($perlchartdir::Transparent); | ||
| 93 | |||
| 94 | $c_legend->setText( $opt->{legend_text} ) if ( exists $opt->{legend_text} ); | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | sub set_color { | ||
| 99 | my ($opt) = @_; | ||
| 100 | |||
| 101 | my $c = | ||
| 102 | ( exists $opt->{color} ) ? $opt->{color} | ||
| 103 | : ( exists $opt->{label_to_color} ) | ||
| 104 | ? [ map { $opt->{label_to_color}{$_} } @{ $opt->{label} } ] | ||
| 105 | : ( exists $opt->{legend_to_color} ) | ||
| 106 | ? [ map { $opt->{legend_to_color}{$_} } @{ $opt->{legend} } ] | ||
| 107 | : \@DEFAULT_COLORLIST; | ||
| 108 | |||
| 109 | my @color = map { map_color_to_hexcode($_) } @$c; | ||
| 110 | return \@color; | ||
| 111 | } ## end sub specify_label_color | ||
| 112 | |||
| 113 | sub set_default_option { | ||
| 114 | my ($opt) = @_; | ||
| 115 | $opt->{width} ||= 800; | ||
| 116 | $opt->{height} ||= 330; | ||
| 117 | $opt->{plot_area} ||= [ 75, 70, 700, 200 ]; | ||
| 118 | $opt->{title_font_size} ||= 12; | ||
| 119 | $opt->{title_font} ||= $CHART_BOLD_FONT; | ||
| 120 | $opt->{default_font} ||= $CHART_FONT; | ||
| 121 | $opt->{default_font_bold} ||= $CHART_BOLD_FONT; | ||
| 122 | $opt->{label_font_size} ||= 10; | ||
| 123 | $opt->{label_font} ||= $CHART_FONT; | ||
| 124 | $opt->{legend} ||= $opt->{label}; | ||
| 125 | $opt->{legend_font_size} ||= 10; | ||
| 126 | $opt->{legend_font} ||= $CHART_FONT; | ||
| 127 | $opt->{line_width} ||= 1; | ||
| 128 | |||
| 129 | $opt->{data_label_font_size} ||= 10; | ||
| 130 | $opt->{data_label_font} ||= $CHART_FONT; | ||
| 131 | $opt->{data_label_format} ||= '{value|0}', | ||
| 132 | |||
| 133 | $opt->{y_axis_font_size} ||= 10; | ||
| 134 | $opt->{y_axis_font} ||= $CHART_FONT; | ||
| 135 | |||
| 136 | $opt->{x_axis_font_size} ||= 10; | ||
| 137 | $opt->{x_axis_font} ||= $CHART_FONT; | ||
| 138 | $opt->{x_axis_font_color} ||= $perlchartdir::TextColor, | ||
| 139 | $opt->{x_axis_font_angle} ||= 0, | ||
| 140 | |||
| 141 | $opt->{data_symbol} ||= \@DEFAULT_DATA_SYMBOL; | ||
| 142 | $opt->{data_symbol_size} ||= 9; | ||
| 143 | |||
| 144 | $opt->{bar_shape} ||= \@DEFAULT_BAR_SHAPE, | ||
| 145 | |||
| 146 | #层内标签 | ||
| 147 | $opt->{center_label_format} ||= "{percent}%"; | ||
| 148 | |||
| 149 | #层右拉一条线出来,写字 | ||
| 150 | $opt->{right_label_format} ||= "{label}, {value}"; | ||
| 151 | |||
| 152 | #各层间隔高度 | ||
| 153 | $opt->{layer_gap} ||= 0.01; | ||
| 154 | |||
| 155 | #横坐标,纵坐标,半径 | ||
| 156 | $opt->{pie_size} ||= [ 450, 290, 180 ]; | ||
| 157 | |||
| 158 | #图层内 3d 形状深度 | ||
| 159 | $opt->{layer_3d_depth} ||= undef, | ||
| 160 | } | ||
| 161 | |||
| 162 | sub set_axis_mark { | ||
| 163 | |||
| 164 | #X/Y轴划线 | ||
| 165 | my ( $axis, $mark_list ) = @_; | ||
| 166 | for my $r (@$mark_list) { | ||
| 167 | my $c = $r->{color}; | ||
| 168 | my $m = $axis->addMark( $r->{offset}, $c, $r->{info} ); | ||
| 169 | $m->setLineWidth( $r->{line_width} ); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | sub chart_bar { | ||
| 174 | my ( $data, %opt ) = @_; | ||
| 175 | set_default_option( \%opt ); | ||
| 176 | |||
| 177 | my $c = new XYChart( $opt{width}, $opt{height} ); | ||
| 178 | $c->setDefaultFonts( $opt{default_font}, $opt{default_font_bold} ); | ||
| 179 | $c->addTitle( $opt{title}, $opt{title_font}, $opt{title_font_size} ); | ||
| 180 | $c->setPlotArea( @{ $opt{plot_area} } ); | ||
| 181 | |||
| 182 | set_axis_option($c, %opt); | ||
| 183 | |||
| 184 | $c->swapXY() if ( $opt{is_horizontal} ); | ||
| 185 | |||
| 186 | my $color = set_color( \%opt ); | ||
| 187 | my $layer = $c->addBarLayer3( $data, $color ); | ||
| 188 | $layer->setAggregateLabelFormat($opt{data_label_format}) if($opt{with_data_label}); | ||
| 189 | $layer->setBorderColor( -1, 1 ); | ||
| 190 | |||
| 191 | $c->makeChart( $opt{file} ); | ||
| 192 | return $opt{file}; | ||
| 193 | } ## end sub draw_pie | ||
| 194 | |||
| 195 | sub chart_pyramid { | ||
| 196 | my ( $data, %opt ) = @_; | ||
| 197 | set_default_option( \%opt ); | ||
| 198 | |||
| 199 | my $c = new PyramidChart( $opt{width}, $opt{height} ); | ||
| 200 | |||
| 201 | $c->setPyramidSize( @{ $opt{plot_area} } ); | ||
| 202 | $c->setDefaultFonts( $opt{default_font}, $opt{default_font_bold} ); | ||
| 203 | $c->addTitle( $opt{title}, $opt{title_font}, $opt{title_font_size} ); | ||
| 204 | $c->setData( $data, $opt{label} ); | ||
| 205 | |||
| 206 | my $color = set_color( \%opt ); | ||
| 207 | $c->setColors2( $perlchartdir::DataColor, $color ); | ||
| 208 | |||
| 209 | $c->setCenterLabel( $opt{center_label_format} ); | ||
| 210 | $c->setRightLabel( $opt{right_label_format} ); | ||
| 211 | $c->setLayerGap( $opt{layer_gap} ); | ||
| 212 | |||
| 213 | $c->makeChart( $opt{file} ); | ||
| 214 | return $opt{file}; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | sub map_color_to_hexcode { | ||
| 219 | my ($color) = @_; | ||
| 220 | |||
| 221 | $! = 0; | ||
| 222 | my ( $num, $unparsed ) = strtod($color); | ||
| 223 | my $is_hex = ( ( $unparsed != 0 ) || $! ) ? 0 : 1; | ||
| 224 | |||
| 225 | return $color if ($is_hex); | ||
| 226 | |||
| 227 | return hex( $COLOR_HEXCODE{$color} ); | ||
| 228 | } ## end sub map_color_to_hexcode | ||
| 229 | |||
| 230 | sub chart_pie { #饼图 | ||
| 231 | my ( $data, %opt ) = @_; | ||
| 232 | set_default_option( \%opt ); | ||
| 233 | |||
| 234 | my $c = new PieChart( $opt{width}, $opt{height} ); | ||
| 235 | $c->setPieSize( @{ $opt{pie_size} } ); | ||
| 236 | $c->setDefaultFonts( $opt{default_font}, $opt{default_font_bold} ); | ||
| 237 | |||
| 238 | $c->setLabelFormat( $opt{label_format} ); | ||
| 239 | $c->setLabelPos( $opt{label_pos}, $perlchartdir::LineColor ); | ||
| 240 | $c->setLabelStyle( $opt{label_font}, $opt{label_font_size} ); | ||
| 241 | |||
| 242 | $c->setLabelLayout($perlchartdir::SideLayout) | ||
| 243 | if ( $opt{label_side_layout} ); | ||
| 244 | |||
| 245 | $c->setData( $data, $opt{label} ); | ||
| 246 | my $color = set_color( \%opt ); | ||
| 247 | $c->setColors2( $perlchartdir::DataColor, $color ); | ||
| 248 | |||
| 249 | $c->addTitle( $opt{title}, $opt{title_font}, $opt{title_font_size} ); | ||
| 250 | $c->setStartAngle( $opt{start_angle} ) if ( exists $opt{start_angle} ); | ||
| 251 | |||
| 252 | $c->setRoundedFrame(); | ||
| 253 | $c->setDropShadow(); | ||
| 254 | $c->setSectorStyle( $perlchartdir::LocalGradientShading, 0xbb000000, 1 ); | ||
| 255 | |||
| 256 | $c->makeChart( $opt{file} ); | ||
| 257 | return $opt{file}; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub chart_spline { | ||
| 261 | my ( $data, %opt ) = @_; | ||
| 262 | $opt{xy_chart_layer_sub} = sub { | ||
| 263 | my ($c) = @_; | ||
| 264 | my $layer = $c->addSplineLayer(); | ||
| 265 | $layer->setMonotonicity($perlchartdir::MonotonicY); | ||
| 266 | return $layer; | ||
| 267 | }; | ||
| 268 | chart_xy( $data, %opt ); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub chart_line { | ||
| 272 | my ( $data, %opt ) = @_; | ||
| 273 | $opt{xy_chart_layer_sub} = sub { | ||
| 274 | my ($c) = @_; | ||
| 275 | my $layer = $c->addLineLayer(); | ||
| 276 | return $layer; | ||
| 277 | }; | ||
| 278 | chart_xy( $data, %opt ); | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub chart_percentage_bar { | ||
| 283 | my ( $data, %opt ) = @_; | ||
| 284 | chart_xy_layer($data, 'bar', $perlchartdir::Percentage, %opt); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub chart_stacked_bar { | ||
| 288 | my ( $data, %opt ) = @_; | ||
| 289 | chart_xy_layer($data, 'bar', $perlchartdir::Stack, %opt); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub chart_xy_layer { | ||
| 293 | my ( $data, $type, $c_layer, %opt ) = @_; | ||
| 294 | $opt{xy_chart_layer_sub} = sub { | ||
| 295 | my ($c) = @_; | ||
| 296 | my $layer; | ||
| 297 | if($type eq 'bar'){ | ||
| 298 | $layer = $c->addBarLayer2( $c_layer, $opt{layer_3d_depth} ); | ||
| 299 | }elsif($type eq 'area'){ | ||
| 300 | $layer = $c->addAreaLayer2( $c_layer, $opt{layer_3d_depth} ); | ||
| 301 | } | ||
| 302 | |||
| 303 | if($opt{with_data_label}){ | ||
| 304 | $layer->setAggregateLabelStyle(); | ||
| 305 | $layer->setDataLabelStyle(); | ||
| 306 | } | ||
| 307 | |||
| 308 | return $layer; | ||
| 309 | }; | ||
| 310 | chart_xy( $data, %opt ); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub chart_percentage_area { | ||
| 314 | my ( $data, %opt ) = @_; | ||
| 315 | #$opt{xy_chart_layer_sub} = sub { | ||
| 316 | #my ($c) = @_; | ||
| 317 | #my $layer = $c->addAreaLayer2($perlchartdir::Stack, $opt{layer_3d_depth}); | ||
| 318 | #return $layer; | ||
| 319 | #}; | ||
| 320 | #chart_xy( $data, %opt ); | ||
| 321 | chart_xy_layer($data, 'area', $perlchartdir::Percentage, %opt); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub chart_stacked_area { | ||
| 325 | my ( $data, %opt ) = @_; | ||
| 326 | #$opt{xy_chart_layer_sub} = sub { | ||
| 327 | #my ($c) = @_; | ||
| 328 | #my $layer = $c->addAreaLayer2($perlchartdir::Stack, $opt{layer_3d_depth}); | ||
| 329 | #return $layer; | ||
| 330 | #}; | ||
| 331 | #chart_xy( $data, %opt ); | ||
| 332 | chart_xy_layer($data, 'area', $perlchartdir::Stack, %opt); | ||
| 333 | } | ||
| 334 | |||
| 335 | sub chart_multi_bar { | ||
| 336 | my ( $data, %opt ) = @_; | ||
| 337 | $opt{xy_chart_layer_sub} = sub { | ||
| 338 | my ($c) = @_; | ||
| 339 | my $layer = $c->addBarLayer2( $perlchartdir::Side, $opt{layer_3d_depth}); | ||
| 340 | return $layer; | ||
| 341 | }; | ||
| 342 | chart_xy( $data, %opt ); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub chart_scatter { | ||
| 346 | my ($data, %opt) = @_; | ||
| 347 | $opt{xy_chart_layer_sub} = sub { | ||
| 348 | my ($c) = @_; | ||
| 349 | my $layer_sub = sub { | ||
| 350 | my ($r) = @_; | ||
| 351 | $c->addScatterLayer($r->{data}[0], $r->{data}[1], | ||
| 352 | $r->{legend}, | ||
| 353 | $r->{data_symbol}, $r->{data_symbol_size}, | ||
| 354 | $r->{color}); | ||
| 355 | }; | ||
| 356 | return $layer_sub; | ||
| 357 | }; | ||
| 358 | chart_xy( $data, %opt ); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub set_axis_option { | ||
| 362 | my ($c, %opt) = @_; | ||
| 363 | |||
| 364 | #x | ||
| 365 | set_axis_mark( $c->xAxis(), $opt{x_axis_mark} ) | ||
| 366 | if ( exists $opt{x_axis_mark} ); | ||
| 367 | $c->xAxis()->setLabels( $opt{label} ); | ||
| 368 | $c->xAxis()->setLabelStyle( | ||
| 369 | $opt{x_axis_font}, $opt{x_axis_font_size}, | ||
| 370 | $opt{x_axis_font_color}, $opt{x_axis_font_angle} | ||
| 371 | ); | ||
| 372 | |||
| 373 | #y | ||
| 374 | set_axis_mark( $c->yAxis(), $opt{y_axis_mark} ) | ||
| 375 | if ( exists $opt{y_axis_mark} ); | ||
| 376 | $c->yAxis()->setLabelFormat( $opt{y_label_format} ) | ||
| 377 | if ( exists $opt{y_label_format} ); | ||
| 378 | $c->yAxis()->setLabelStyle( $opt{y_axis_font}, $opt{y_axis_font_size} ); | ||
| 379 | $c->yAxis()->setTickDensity( $opt{y_tick_density} ) | ||
| 380 | if ( exists $opt{y_tick_density} ); | ||
| 381 | $c->yAxis() | ||
| 382 | ->setDateScale( $opt{y_axis_lower_limit}, $opt{y_axis_upper_limit} ) | ||
| 383 | if ( exists $opt{y_axis_lower_limit} | ||
| 384 | and exists $opt{y_axis_upper_limit} ); | ||
| 385 | |||
| 386 | } | ||
| 387 | |||
| 388 | sub chart_xy { # XY型chart 基础函数 | ||
| 389 | my ( $data, %opt ) = @_; | ||
| 390 | set_default_option( \%opt ); | ||
| 391 | |||
| 392 | my $c = new XYChart( $opt{width}, $opt{height} ); | ||
| 393 | $c->setPlotArea( @{ $opt{plot_area} } ); | ||
| 394 | $c->addTitle( $opt{title}, $opt{title_font}, $opt{title_font_size} ); | ||
| 395 | |||
| 396 | |||
| 397 | #x/y 轴 | ||
| 398 | set_axis_option($c, %opt); | ||
| 399 | |||
| 400 | $c->swapXY() if ( $opt{is_horizontal} ); | ||
| 401 | |||
| 402 | #画什么样的图 | ||
| 403 | my $color = set_color( \%opt ); | ||
| 404 | |||
| 405 | |||
| 406 | my $layer = $opt{xy_chart_layer_sub}->($c); | ||
| 407 | if(ref($layer) eq 'CODE'){ | ||
| 408 | for ( my $i = 0 ; $i <= $#$data ; $i++ ) { | ||
| 409 | my $d = $data->[$i]; | ||
| 410 | $layer->({ | ||
| 411 | data=> $d, | ||
| 412 | color => $color->[$i], | ||
| 413 | legend => $opt{legend}[$i], | ||
| 414 | data_symbol => $opt{data_symbol}[$i], | ||
| 415 | data_symbol_size => $opt{data_symbol_size}, | ||
| 416 | with_data_symbol => $opt{with_data_symbol}, | ||
| 417 | }); | ||
| 418 | } | ||
| 419 | #$c->addScatterLayer($dataX0, $dataY0, "Genetically Engineered", $perlchartdir::DiamondSymbol, 13, 0xff9933); | ||
| 420 | }else{ | ||
| 421 | $layer->setLineWidth( $opt{line_width} ); | ||
| 422 | set_data_label( $layer, \%opt ); | ||
| 423 | for ( my $i = 0 ; $i <= $#$data ; $i++ ) { | ||
| 424 | my $d = $data->[$i]; | ||
| 425 | $_ ||= 0 for @$d; | ||
| 426 | my $temp = $layer->addDataSet( $d, $color->[$i], $opt{legend}[$i] ); | ||
| 427 | $temp->setDataSymbol( $opt{data_symbol}[$i], $opt{data_symbol_size} ) if($opt{with_data_symbol}); | ||
| 428 | $layer->setBarShape($opt{bar_shape}[$i], $i) if($opt{with_bar_shape}); | ||
| 429 | } ## end for ( my $i = 0; $i <= ...) | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | set_legend( $c, \%opt ); | ||
| 434 | $c->makeChart( $opt{file} ); | ||
| 435 | return $opt{file}; | ||
| 436 | } ## end sub draw_xy_chart | ||
| 437 | |||
| 438 | 1; | ||
| 439 | __DATA__ |