| 1 | package Chart::OFC2; | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Chart::OFC2 - Generate html and data files for use with Open Flash Chart version 2 | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | OFC2 html: | ||
| 12 | |||
| 13 | use Chart::OFC2; | ||
| 14 | |||
| 15 | my $chart = Chart::OFC2->new( | ||
| 16 | 'title' => 'Bar chart test', | ||
| 17 | ); | ||
| 18 | print $chart->render_swf(600, 400, 'chart-data.json', 'test-chart'); | ||
| 19 | |||
| 20 | OFC2 bar chart data: | ||
| 21 | |||
| 22 | use Chart::OFC2; | ||
| 23 | use Chart::OFC2::Axis; | ||
| 24 | use Chart::OFC2::Bar; | ||
| 25 | |||
| 26 | my $chart = Chart::OFC2->new( | ||
| 27 | 'title' => 'Bar chart test', | ||
| 28 | x_axis => { | ||
| 29 | labels => { | ||
| 30 | labels => [ 'Jan', 'Feb', 'Mar', 'Apr', 'May' ], | ||
| 31 | } | ||
| 32 | }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $bar = Chart::OFC2::Bar->new(); | ||
| 36 | $bar->values([ 1..5 ]); | ||
| 37 | $chart->add_element($bar); | ||
| 38 | |||
| 39 | print $chart->render_chart_data(); | ||
| 40 | |||
| 41 | =head1 WARNING | ||
| 42 | |||
| 43 | Current version implements just subset of functionality that Open Flash | ||
| 44 | Chart 2 is offering. But it should help you to starting creating OFC2 | ||
| 45 | graphs quite fast. The JSON format is quite intuitive and can be created | ||
| 46 | from any hash. This module is more like guideline. | ||
| 47 | |||
| 48 | This is early version B<PROTOTYPE> so the API B<WILL> change, be careful when upgrading | ||
| 49 | versions. | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | OFC2 is a flash script for creating graphs. To have a graph we need an | ||
| 54 | F<open-flash-chart.swf> and a JSON data file describing graph data. | ||
| 55 | Complete examples you can find after successful run of this module | ||
| 56 | tests in F<t/output/> folder - F<t/output/bar.html>, F<t/output/pie.html>, | ||
| 57 | F<t/output/hbar.html> are html graphs and F<t/output/bad-data.json>, | ||
| 58 | F<t/output/pie-data.json>, F<t/output/hbar-data.json> are the data files. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | |||
| 63 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 64 | use Moose::Util::TypeConstraints; | ||
| 65 | use MooseX::StrictConstructor; | ||
| 66 | use MooseX::Aliases; | ||
| 67 | |||
| 68 | our $VERSION = '0.07'; | ||
| 69 | |||
| 70 | use Carp::Clan 'croak'; | ||
| 71 | use JSON::XS qw(); | ||
| 72 | |||
| 73 | use Chart::OFC2::Axis; | ||
| 74 | use Chart::OFC2::Bar; | ||
| 75 | use Chart::OFC2::Title; | ||
| 76 | use Chart::OFC2::Extremes; | ||
| 77 | use Chart::OFC2::ToolTip; | ||
| 78 | use List::Util 'min', 'max'; | ||
| 79 | use List::MoreUtils 'any'; | ||
| 80 | |||
| 81 | =head1 PROPERTIES | ||
| 82 | |||
| 83 | has 'data_load_type' => (is => 'rw', isa => 'Str', default => 'inline_js'); | ||
| 84 | has 'bootstrap' => (is => 'rw', isa => 'Bool', default => '1'); | ||
| 85 | has 'title' => (is => 'rw', isa => 'Chart::OFC2::Title', default => sub { Chart::OFC2::Title->new() }, lazy => 1, coerce => 1); | ||
| 86 | has 'x_axis' => (is => 'rw', isa => 'Chart::OFC2::XAxis', default => sub { Chart::OFC2::XAxis->new() }, lazy => 1,); | ||
| 87 | has 'y_axis' => (is => 'rw', isa => 'Chart::OFC2::YAxis', default => sub { Chart::OFC2::YAxis->new() }, lazy => 1, ); | ||
| 88 | has 'elements' => (is => 'rw', isa => 'ArrayRef', default => sub{[]}, lazy => 1); | ||
| 89 | has 'extremes' => (is => 'rw', isa => 'Chart::OFC2::Extremes', default => sub { Chart::OFC2::Extremes->new() }, lazy => 1); | ||
| 90 | has 'tooltip' => (is => 'rw', isa => 'Chart::OFC2::ToolTip',); | ||
| 91 | has 'bg_colour' => (is => 'rw', isa => 'Str', default => 'f8f8d8', alias => 'bg_color' ); | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | has 'data_load_type' => (is => 'rw', isa => 'Str', default => 'inline_js'); | ||
| 96 | has 'bootstrap' => (is => 'rw', isa => 'Bool', default => '1'); | ||
| 97 | has 'title' => (is => 'rw', isa => 'Chart::OFC2::Title', default => sub { Chart::OFC2::Title->new() }, lazy => 1, coerce => 1); | ||
| 98 | has 'x_axis' => (is => 'rw', isa => 'Chart::OFC2::XAxis', default => sub { Chart::OFC2::XAxis->new() }, lazy => 1, coerce => 1); | ||
| 99 | has 'y_axis' => (is => 'rw', isa => 'Chart::OFC2::YAxis', default => sub { Chart::OFC2::YAxis->new() }, lazy => 1, coerce => 1); | ||
| 100 | has 'elements' => (is => 'rw', isa => 'ArrayRef', default => sub{[]}, lazy => 1); | ||
| 101 | has 'extremes' => (is => 'rw', isa => 'Chart::OFC2::Extremes', default => sub { Chart::OFC2::Extremes->new() }, lazy => 1); | ||
| 102 | has '_json' => (is => 'rw', isa => 'Object', default => sub { JSON::XS->new->pretty(1)->convert_blessed(1) }, lazy => 1); | ||
| 103 | has 'tooltip' => (is => 'rw', isa => 'Chart::OFC2::ToolTip', coerce => 1); | ||
| 104 | has 'bg_colour' => (is => 'rw', isa => 'Str', default => 'f8f8d8', alias => 'bg_color' ); | ||
| 105 | |||
| 106 | =head1 METHODS | ||
| 107 | |||
| 108 | =head2 new() | ||
| 109 | |||
| 110 | Object constructor. | ||
| 111 | |||
| 112 | =head2 get_element($type) | ||
| 113 | |||
| 114 | Returns new chart object of selected type. Currently only C<bar> and C<pie> | ||
| 115 | is available. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | # elements are the data series items, usually containing values to plot | ||
| 120 | sub get_element { | ||
| 121 | my ($self, $element_name) = @_; | ||
| 122 | |||
| 123 | my $element_module = ( | ||
| 124 | $element_name eq 'bar' ? 'Chart::OFC2::Bar' | ||
| 125 | : $element_name eq 'pie' ? 'Chart::OFC2::Pie' | ||
| 126 | : undef | ||
| 127 | ); | ||
| 128 | croak 'unsupported element - ', $element_name | ||
| 129 | if not defined $element_name; | ||
| 130 | |||
| 131 | return $element_module->new(); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | =head2 add_element($element) | ||
| 136 | |||
| 137 | Adds passed element to the graph. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub add_element { | ||
| 142 | my ($self, $element) = @_; | ||
| 143 | |||
| 144 | if ($element->use_extremes) { | ||
| 145 | $self->y_axis->max('a'); | ||
| 146 | $self->y_axis->min('a'); | ||
| 147 | $self->x_axis->max('a'); | ||
| 148 | $self->x_axis->min('a'); | ||
| 149 | } | ||
| 150 | |||
| 151 | push(@{ $self->elements }, $element); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | =head2 render_chart_data | ||
| 156 | |||
| 157 | Returns stringified JSON encoded graph data. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub render_chart_data { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | $self->auto_extremes(); | ||
| 165 | |||
| 166 | return $self->_json->encode({ | ||
| 167 | 'title' => $self->title, | ||
| 168 | 'x_axis' => $self->x_axis, | ||
| 169 | 'y_axis' => $self->y_axis, | ||
| 170 | 'tooltip' => $self->tooltip, | ||
| 171 | 'elements' => $self->elements, | ||
| 172 | 'bg_colour' => $self->bg_colour, | ||
| 173 | }); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | =head2 auto_extremes | ||
| 178 | |||
| 179 | Recalculate graph auto extremes. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub auto_extremes { | ||
| 184 | my $self = shift; | ||
| 185 | |||
| 186 | foreach my $axis_name ('x_axis', 'y_axis') { | ||
| 187 | my $axis = $self->$axis_name; | ||
| 188 | next if not defined $axis; | ||
| 189 | |||
| 190 | foreach my $axis_type ('min', 'max') { | ||
| 191 | my $axis_value = $axis->$axis_type; | ||
| 192 | if ((defined $axis_value) and ($axis_value eq 'a')) { | ||
| 193 | $axis->$axis_type($self->smooth($axis_name, $axis_type)); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | return; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | =head2 render_swf($width, $height, $data_url, $div_id) | ||
| 203 | |||
| 204 | Returns html snippet that will represent one graph in a html document. | ||
| 205 | |||
| 206 | WARN: the arguments format will change to C<key => value> in the next | ||
| 207 | releases. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub render_swf { | ||
| 212 | my ($self, $width, $height, $data_url, $div_id) = @_; | ||
| 213 | |||
| 214 | $div_id ||= 'my_chart'; | ||
| 215 | |||
| 216 | my $html = ''; | ||
| 217 | |||
| 218 | if ($self->data_load_type eq 'inline_js') { | ||
| 219 | if ($self->bootstrap) { | ||
| 220 | $html .= '<script type="text/javascript" src="swfobject.js"></script>'; | ||
| 221 | $self->{'skip_bootstrap'} = 1; | ||
| 222 | } | ||
| 223 | $html .= qq^ | ||
| 224 | <div id="$div_id"></div> | ||
| 225 | <script type="text/javascript"> | ||
| 226 | swfobject.embedSWF( | ||
| 227 | "open-flash-chart.swf", "$div_id", "$width", "$height", | ||
| 228 | "9.0.0", "expressInstall.swf", | ||
| 229 | {"data-file":"$data_url"} | ||
| 230 | ); | ||
| 231 | </script> | ||
| 232 | ^; | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | $html .= qq^ | ||
| 236 | <object | ||
| 237 | classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" | ||
| 238 | codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" | ||
| 239 | width="$width" | ||
| 240 | height="$height" | ||
| 241 | id="graph_2" | ||
| 242 | align="middle"> | ||
| 243 | <param name="allowScriptAccess" value="sameDomain" /> | ||
| 244 | <param name="movie" value="open-flash-chart.swf?width=$width&height=$height&data=$data_url"/> | ||
| 245 | <param name="quality" value="high" /> | ||
| 246 | <param name="bgcolor" value="#FFFFFF" /> | ||
| 247 | <embed | ||
| 248 | src="open-flash-chart.swf?width=$width&height=$height&data=$data_url" | ||
| 249 | quality="high" | ||
| 250 | bgcolor="#FFFFFF" | ||
| 251 | width="$width" | ||
| 252 | height="$height" | ||
| 253 | name="open-flash-chart" | ||
| 254 | align="middle" | ||
| 255 | allowScriptAccess="sameDomain" | ||
| 256 | type="application/x-shockwave-flash" | ||
| 257 | pluginspage="http://www.macromedia.com/go/getflashplayer" | ||
| 258 | /> | ||
| 259 | </object> | ||
| 260 | ^; | ||
| 261 | } | ||
| 262 | |||
| 263 | return $html; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | =head1 FUNCTIONS | ||
| 268 | |||
| 269 | =head2 smoother_number | ||
| 270 | |||
| 271 | round the number up a bit to a nice round number also changes number to an int | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub smoother_number { | ||
| 276 | my $number = shift; | ||
| 277 | my $min_max = shift; | ||
| 278 | my $n = $number; | ||
| 279 | |||
| 280 | return | ||
| 281 | if not defined $number; | ||
| 282 | |||
| 283 | if ($min_max eq 'max') { | ||
| 284 | $n += 1; | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | $n -= 1; | ||
| 288 | } | ||
| 289 | if ($n <= 10) { $n = int($n) } | ||
| 290 | elsif ($n < 30) { $n = $n + (-$n % 5) } | ||
| 291 | elsif ($n < 100) { $n = $n + (-$n % 10) } | ||
| 292 | elsif ($n < 500) { $n = $n + (-$n % 50) } | ||
| 293 | elsif ($n < 1000) { $n = $n + (-$n % 100) } | ||
| 294 | elsif ($n < 10000) { $n = $n + (-$n % 200) } | ||
| 295 | else { $n = $n + (-$n % 500) } | ||
| 296 | return int($n); | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | =head2 smooth($axis_name, $axis_type) | ||
| 301 | |||
| 302 | Smooth axis min/max. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub smooth { | ||
| 307 | my $self = shift; | ||
| 308 | my $axis_name = shift; | ||
| 309 | my $axis_type = shift; | ||
| 310 | |||
| 311 | my $extremes_name = $axis_name.'_'.$axis_type; | ||
| 312 | my $cmp_function = ($axis_type eq 'min' ? \&min : \&max); | ||
| 313 | |||
| 314 | my $number; | ||
| 315 | foreach my $element (@{$self->elements}) { | ||
| 316 | my $element_number = $element->extremes->$extremes_name; | ||
| 317 | |||
| 318 | next | ||
| 319 | if not defined $element_number; | ||
| 320 | $number = $element_number | ||
| 321 | if not defined $number; | ||
| 322 | $number = $cmp_function->($number, $element_number); | ||
| 323 | } | ||
| 324 | |||
| 325 | return smoother_number($number, $axis_type); | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 bg_color() | ||
| 329 | |||
| 330 | Same as bg_colour(). | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub bg_color { | ||
| 335 | &bg_colour; | ||
| 336 | } | ||
| 337 | |||
| 338 | __PACKAGE__->meta->make_immutable; | ||
| 339 | |||
| 340 | 1; | ||
| 341 | |||
| 342 | |||
| 343 | __END__ | ||