| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: Plot.pm | ||
| 3 | # | ||
| 4 | # Description: Plot tag values in SVG format | ||
| 5 | # | ||
| 6 | # Revisions: 2025-02-14 - P. Harvey Created | ||
| 7 | #------------------------------------------------------------------------------ | ||
| 8 | |||
| 9 | package Image::ExifTool::Plot; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | |||
| 14 | $VERSION = '1.05'; | ||
| 15 | |||
| 16 | # default plot settings (lower-case settings may be overridden by the user) | ||
| 17 | my %defaults = ( | ||
| 18 | size => [ 800, 600 ], # width,height of output image | ||
| 19 | margin => [ 60, 15, 15, 30 ], # left,top,right,bottom margins around plot area | ||
| 20 | legend => [ 0, 0 ], # top,right offset for legend | ||
| 21 | txtpad => [ 10, 10 ], # padding between text and x,y scale | ||
| 22 | linespacing => 20, # text line spacing | ||
| 23 | # colours for plot lines | ||
| 24 | cols => [ qw(red green blue black orange gray fuchsia brown turquoise gold | ||
| 25 | lime violet maroon aqua navy pink olive indigo silver teal) ], | ||
| 26 | marks => [ qw(circle square triangle diamond star plus pentagon left down right) ], | ||
| 27 | stroke => 1, # stroke width and marker scaling | ||
| 28 | grid => 'darkgray', # grid colour | ||
| 29 | text => 'black', # text and plot frame colour | ||
| 30 | type => 'line', # plot type, 'line' or 'scatter' | ||
| 31 | style => '', # 'line', 'marker' or 'line+marker' | ||
| 32 | xlabel => '', # x axis label | ||
| 33 | ylabel => '', # y axis label | ||
| 34 | title => '', # plot title | ||
| 35 | nbins => 20, # number of histogram bins | ||
| 36 | # xmin, xmax # x axis minimum,maximum | ||
| 37 | # ymin, ymax # y axis minimum,maximum | ||
| 38 | # split # split list of numbers into separate plot lines | ||
| 39 | # bkg # background colour | ||
| 40 | # multi # flag to make one plot per dataset | ||
| 41 | # | ||
| 42 | # members containing capital letters are used internally | ||
| 43 | # | ||
| 44 | Data => { }, # data arrays for each variable | ||
| 45 | Name => [ ], # variable names | ||
| 46 | # XMin, XMax # min/max data index | ||
| 47 | # YMin, YMax # min/max data value | ||
| 48 | # SaveName, Save # saved variables between plots | ||
| 49 | ); | ||
| 50 | |||
| 51 | my %markerData = ( | ||
| 52 | circle => '<circle cx="4" cy="4" r="2.667"', | ||
| 53 | square => '<path d="M1.667 1.667 l4.667 0 0 4.667 -4.667 0 z"', | ||
| 54 | triangle => '<path d="M4 0.8 l2.667 5.333 -5.333 0 z"', | ||
| 55 | diamond => '<path d="M4 1 l3 3 -3 3 -3 -3 z"', | ||
| 56 | star => '<path d="M4 0.8 L5 2.625 7.043 3.011 5.617 4.525 5.881 6.589 4 5.7 2.119 6.589 2.383 4.525 0.957 3.011 3 2.625 z"', | ||
| 57 | plus => '<path d="M2.75 1 l2.5 0 0 1.75 1.75 0 0 2.5 -1.75 0 0 1.75 -2.5 0 0 -1.75 -1.75 0 0 -2.5 1.75 0 z"', | ||
| 58 | pentagon => '<path d="M4 1 L6.853 3.073 5.763 6.427 2.237 6.427 1.147 3.073 z"', | ||
| 59 | left => '<path d="M0.8 4 l5.333 2.667 0 -5.333 z"', | ||
| 60 | down => '<path d="M4 7.2 l2.667 -5.333 -5.333 0 z"', | ||
| 61 | right => '<path d="M7.2 4 l-5.333 2.667 0 -5.333 z"', | ||
| 62 | ); | ||
| 63 | |||
| 64 | my @ng = (20, 15); # optimal number grid lines in X and Y for a 800x600 plot | ||
| 65 | my $wch = 8; # nominal width of a character (measured at 7.92) | ||
| 66 | |||
| 67 | #------------------------------------------------------------------------------ | ||
| 68 | # Create new plot object | ||
| 69 | sub new | ||
| 70 | { | ||
| 71 | my $that = shift; | ||
| 72 | my $class = ref($that) || $that || 'Image::ExifTool::Plot'; | ||
| 73 | my $self = bless { }, $class; | ||
| 74 | foreach (keys %defaults) { | ||
| 75 | ref $defaults{$_} eq 'HASH' and $$self{$_} = { %{$defaults{$_}} }, next; | ||
| 76 | ref $defaults{$_} eq 'ARRAY' and $$self{$_} = [ @{$defaults{$_}} ], next; | ||
| 77 | $$self{$_} = $defaults{$_}; | ||
| 78 | } | ||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | #------------------------------------------------------------------------------ | ||
| 83 | # Set plot settings | ||
| 84 | # Inputs: 0) Plot ref, 1) comma-separated options | ||
| 85 | sub Settings($$) | ||
| 86 | { | ||
| 87 | my ($self, $set) = @_; | ||
| 88 | return unless $set; | ||
| 89 | foreach (split /,\s*/, $set) { | ||
| 90 | next unless /^([a-z].*?)(=(.*))?$/i; | ||
| 91 | my ($name, $val) = (lc $1, $3); | ||
| 92 | if (ref $$self{$name} eq 'ARRAY') { | ||
| 93 | next unless defined $val; | ||
| 94 | my $isNum = $$self{$name}[0] =~ /^\d+$/; | ||
| 95 | # also allow numbers to also be separated by 'x' | ||
| 96 | my @vals = $isNum ? split(/\s*[x\s\/+]\s*/, $val) : split(/\s*[\s\/+]\s*/, $val); | ||
| 97 | my $i; | ||
| 98 | for ($i=0; @vals; ++$i) { | ||
| 99 | my $val = lc shift @vals; | ||
| 100 | next unless length $val; | ||
| 101 | if ($name eq 'marks') { | ||
| 102 | my @v = split /-/, $val; | ||
| 103 | if ($v[0]) { | ||
| 104 | if ($v[0] =~ /^n/) { | ||
| 105 | $v[0] = 'none'; | ||
| 106 | } else { | ||
| 107 | ($v[0]) = grep /^$v[0]/, @{$defaults{marks}}; | ||
| 108 | $v[0] or $$self{Warn} = 'Invalid marker name', next; | ||
| 109 | } | ||
| 110 | } else { | ||
| 111 | # cycle through default markers if none specified | ||
| 112 | $v[0] = $defaults{marks}[$i % @{$defaults{marks}}]; | ||
| 113 | } | ||
| 114 | $val = join '-', @v; | ||
| 115 | } | ||
| 116 | $$self{$name}[$i] = $val; | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | $val = 1 unless defined $val; # default to 1 if no "=" | ||
| 120 | my %charName = ('&'=>'amp', '<'=>'lt', '>'=>'gt'); | ||
| 121 | # escape necessary XML characters, but allow numerical entities | ||
| 122 | $val =~ s/([&><])/&$charName{$1};/sg and $val =~ s/&(#(\d+|x[0-9a-fA-F]+);)/&$1/; | ||
| 123 | undef $val unless length $val; | ||
| 124 | $$self{$name} = $val; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | #------------------------------------------------------------------------------ | ||
| 130 | # Add points to SVG plot | ||
| 131 | # Inputs: 0) Plot object ref, 1) tag value hash ref, 2) tag ID list ref | ||
| 132 | sub AddPoints($$$) | ||
| 133 | { | ||
| 134 | my ($self, $info, $tags) = @_; | ||
| 135 | my ($tag, $name, %num, $index, $mod, $val, @vals); | ||
| 136 | my ($ee, $docNum, $data, $xmin, $xmax) = @$self{qw(EE DocNum Data XMin XMax)}; | ||
| 137 | $$self{type} or $$self{type} = 'line'; | ||
| 138 | my $scat = $$self{type} =~ /^s/ ? 1 : 0; | ||
| 139 | my $xname = $$self{Name}[0]; # (x-axis name if using scatter plot) | ||
| 140 | my $maxLines = ($$self{type} =~ /^h/ and not $$self{multi}) ? 1 : 20; | ||
| 141 | for (;;) { | ||
| 142 | if (@vals) { | ||
| 143 | $val = shift @vals; | ||
| 144 | next unless $val =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?$/; | ||
| 145 | } else { | ||
| 146 | $tag = shift @$tags or last; | ||
| 147 | # ignore non-floating-point values | ||
| 148 | $val = $$info{$tag}; | ||
| 149 | ($name) = $tag =~ /^(\S+)/g; # remove index number | ||
| 150 | if (ref $val) { | ||
| 151 | if (ref $val eq 'ARRAY') { | ||
| 152 | $index = defined $index ? $index + 1 : 0; | ||
| 153 | $val = $$val[$index]; | ||
| 154 | defined $val or undef($index), undef($mod), next; | ||
| 155 | $name .= $mod ? '['.($index % $mod).']' : "[$index]"; | ||
| 156 | unshift @$tags, $tag; # will continue with this tag later | ||
| 157 | } elsif (ref $val eq 'SCALAR') { | ||
| 158 | $val = $$val; # handle binary values | ||
| 159 | } else { | ||
| 160 | next; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | next unless defined $val and $val =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?([ ,;\t\n\r]+|$)/i; | ||
| 165 | if ($1) { | ||
| 166 | # split a string of numbers into separate plot points (eg. histogram tags) | ||
| 167 | if ($$self{'split'}) { | ||
| 168 | # make an array | ||
| 169 | $$info{$tag} = [ split /[ ,;\t\n\r][\n\r]? */, $val ]; | ||
| 170 | unshift @$tags, $tag; | ||
| 171 | # split into lists of 'split' elements if split > 1 | ||
| 172 | $mod = $$self{'split'} if $$self{'split'} > 1; | ||
| 173 | next; | ||
| 174 | } else { | ||
| 175 | @vals = split /[ ,;\t\n\r][\n\r]? */, $val; | ||
| 176 | $val = shift @vals; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | my $docNum = $docNum ? $$docNum{$tag} || 0 : 0; | ||
| 180 | next if $docNum and not $ee; | ||
| 181 | unless ($$data{$name}) { | ||
| 182 | if (@{$$self{Name}} >= $maxLines + $scat) { | ||
| 183 | unless ($$self{MaxTags}) { | ||
| 184 | if ($$self{type} =~ /^h/ and not $$self{multi}) { | ||
| 185 | $$self{Warn} = 'Use the Multi setting to make a separate histogram for each dataset'; | ||
| 186 | } else { | ||
| 187 | $$self{Warn} = 'Too many variables to plot all of them'; | ||
| 188 | } | ||
| 189 | $$self{MaxTags} = 1; | ||
| 190 | } | ||
| 191 | next; | ||
| 192 | } | ||
| 193 | push @{$$self{Name}}, $name; | ||
| 194 | $xname or $xname = $name; # x-axis data for scatter plot | ||
| 195 | unless ($scat and $name eq $xname) { | ||
| 196 | $$self{Max} = $val if not defined $$self{Max} or $val > $$self{Max}; | ||
| 197 | $$self{Min} = $val if not defined $$self{Min} or $val < $$self{Min}; | ||
| 198 | } | ||
| 199 | $xmin = $xmax = $docNum unless defined $xmin; | ||
| 200 | $num{$name} = $xmax; | ||
| 201 | $$data{$name}[$xmax - $xmin] = $val if $xmax >= $xmin; | ||
| 202 | next; | ||
| 203 | } | ||
| 204 | if ($docNum and defined $num{$name} and $num{$name} < $docNum) { | ||
| 205 | $num{$name} = $docNum; # keep documents synchronized if some tags are missing | ||
| 206 | } else { | ||
| 207 | $num{$name} = $xmax unless defined $num{$name}; | ||
| 208 | ++$num{$name}; | ||
| 209 | } | ||
| 210 | $$data{$name}[$num{$name} - $xmin] = $val if $num{$name} >= $xmin; | ||
| 211 | unless ($scat and $name eq $xname) { | ||
| 212 | $$self{Max} = $val if $val > $$self{Max}; | ||
| 213 | $$self{Min} = $val if $val < $$self{Min}; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | # start next file at x value so far | ||
| 217 | $xmax < $num{$_} and $xmax = $num{$_} foreach keys %num; | ||
| 218 | $$self{XMin} = $xmin; | ||
| 219 | $$self{XMax} = $xmax; | ||
| 220 | } | ||
| 221 | |||
| 222 | #------------------------------------------------------------------------------ | ||
| 223 | # Calculate a nice round number for grid spacing | ||
| 224 | # Inputs: 0) nominal spacing (must be positive), 1) flag to increment to next number | ||
| 225 | # Returns: spacing rounded to an even number | ||
| 226 | sub GetGridSpacing($;$) | ||
| 227 | { | ||
| 228 | my ($nom, $inc) = @_; | ||
| 229 | my ($rounded, $spc); | ||
| 230 | my $div = sprintf('%.3e', $nom); | ||
| 231 | my $num = substr($div, 0, 1); | ||
| 232 | my $exp = $div =~ s/.*e// ? $div : 0; | ||
| 233 | if ($inc) { | ||
| 234 | # increment to next highest even number | ||
| 235 | $num = $num < 2 ? 2 : ($num < 5 ? 5 : (++$exp, 1)); | ||
| 236 | } else { | ||
| 237 | # look for nearest factor to 1, 2 or 5 * 10^x | ||
| 238 | $num = $num < 8 ? 5 : (++$exp, 1) if $num > 2; | ||
| 239 | } | ||
| 240 | return $exp >= 0 ? $num . ('0' x $exp) : '.' . ('0' x (-$exp - 1)) . $num; | ||
| 241 | } | ||
| 242 | |||
| 243 | #------------------------------------------------------------------------------ | ||
| 244 | # Get plot range | ||
| 245 | # Inputs: 0) minimum, 1) maximum | ||
| 246 | # Returns: difference | ||
| 247 | # Notes: Adjusts min/max if necessary to make difference positive | ||
| 248 | sub GetRange($$) | ||
| 249 | { | ||
| 250 | if ($_[0] >= $_[1]) { | ||
| 251 | $_[0] = ($_[0] + $_[1]) / 2; | ||
| 252 | $_[0] -= 0.5 if $_[0]; | ||
| 253 | $_[1] = $_[0] + 1; | ||
| 254 | } | ||
| 255 | return $_[1] - $_[0]; | ||
| 256 | } | ||
| 257 | |||
| 258 | #------------------------------------------------------------------------------ | ||
| 259 | # Draw SVG plot | ||
| 260 | # Inputs: 0) Plot ref, 1) Output file reference | ||
| 261 | sub Draw($$) | ||
| 262 | { | ||
| 263 | my ($self, $fp) = @_; | ||
| 264 | my ($min, $max, $xmin, $xmax, $name, $style) = @$self{qw(Min Max XMin XMax Name style)}; | ||
| 265 | my ($plotNum, $multiMulti); | ||
| 266 | |||
| 267 | if (not defined $min or not defined $xmin) { | ||
| 268 | $$self{Error} = 'Nothing to plot'; | ||
| 269 | return; | ||
| 270 | } | ||
| 271 | my $scat = $$self{type} =~ /^s/ ? 1 : 0; | ||
| 272 | my $hist = $$self{type} =~ /^h/ ? [ ] : 0; | ||
| 273 | my $multi = $$self{multi} || 0; | ||
| 274 | my @multi = $multi =~ /\d+/g; | ||
| 275 | my @names = @$name; | ||
| 276 | shift @names if $scat; | ||
| 277 | $multi = shift @multi; | ||
| 278 | $multi = 0 unless $multi > 0; | ||
| 279 | $style or $style = $hist ? 'line+fill' : 'line'; | ||
| 280 | unless ($style =~ /\b[mpl]/ or ($hist and $style =~ /\bf/)) { | ||
| 281 | $$self{Error} = 'Invalid plot Style setting'; | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | my $numPlots = 0; | ||
| 285 | if ($multi) { | ||
| 286 | my $n; | ||
| 287 | for ($n=0; $n<scalar(@$name)-$scat; ++$numPlots) { | ||
| 288 | $n += ($multi[$numPlots] || 1); | ||
| 289 | $multiMulti = 1 if $multi[$numPlots] and $multi[$numPlots] > 1; | ||
| 290 | } | ||
| 291 | } else { | ||
| 292 | $numPlots = 1; | ||
| 293 | } | ||
| 294 | my @size = @{$$self{size}}; | ||
| 295 | my $sy = $size[1]; | ||
| 296 | if ($multi) { | ||
| 297 | $sy *= int(($numPlots + $multi - 1) / $multi) / $multi; | ||
| 298 | $_ /= $multi foreach @size; | ||
| 299 | } | ||
| 300 | my $tmp = $$self{title} || "Plot by ExifTool $Image::ExifTool::VERSION"; | ||
| 301 | print $fp qq{<?xml version="1.0" standalone="no"?> | ||
| 302 | <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> | ||
| 303 | <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="$$self{size}[0]" height="$sy" | ||
| 304 | preserveAspectRatio="xMidYMid meet" viewBox="0 0 $$self{size}[0] $sy"> | ||
| 305 | <title>$tmp</title>}; | ||
| 306 | # loop through all plots | ||
| 307 | for ($plotNum=0; $plotNum<$numPlots; ++$plotNum) { | ||
| 308 | my ($i, $n, %col, %class, $dx, $dy, $dx2, $xAxis, $x, $y, $px, $py, @og); | ||
| 309 | my ($noLegend, $xname, $xdat, $xdiff, $diff, %markID); | ||
| 310 | if ($numPlots > 1) { | ||
| 311 | print $fp "\n<g transform='translate(", ($plotNum % $multi) * $size[0], | ||
| 312 | ',', int($plotNum/$multi) * $size[1], ")'>"; | ||
| 313 | if ($plotNum) { | ||
| 314 | @$self{qw(XMin XMax title xlabel ylabel)} = @{$$self{Save}}; | ||
| 315 | } else { | ||
| 316 | $$self{Save} = [ @$self{qw(XMin XMax title xlabel ylabel)} ]; | ||
| 317 | $$self{SaveName} = [ @{$$self{Name}} ]; | ||
| 318 | } | ||
| 319 | $name = $$self{Name} = [ ]; | ||
| 320 | push @{$$self{Name}}, $$self{SaveName}[0] if $scat; | ||
| 321 | foreach (0 .. (($multi[$plotNum] || 1) - 1)) { | ||
| 322 | push @{$$self{Name}}, shift(@names); | ||
| 323 | } | ||
| 324 | undef $min; undef $max; | ||
| 325 | foreach ($scat .. (@{$$self{Name}} - 1)) { | ||
| 326 | my $dat = $$self{Data}{$$self{Name}[$_]}; | ||
| 327 | foreach (@$dat) { | ||
| 328 | defined or next; | ||
| 329 | defined $min or $min = $max = $_, next; | ||
| 330 | $min > $_ and $min = $_; | ||
| 331 | $max < $_ and $max = $_; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | my ($data, $title, $xlabel, $ylabel, $cols, $marks, $tpad, $wid) = | ||
| 336 | @$self{qw(Data title xlabel ylabel cols marks txtpad stroke)}; | ||
| 337 | my @name = @$name; | ||
| 338 | my @margin = ( @{$$self{margin}} ); | ||
| 339 | |||
| 340 | # set reasonable default titles and labels | ||
| 341 | $xname = shift @name if $scat; | ||
| 342 | $title = "$name[0] vs $xname" if $scat and defined $title and not $title and @name == 1 and not $multi; | ||
| 343 | if ($scat || $hist and defined $xlabel and not $xlabel) { | ||
| 344 | $xlabel = $$name[0]; | ||
| 345 | $noLegend = 1 if $hist; | ||
| 346 | } | ||
| 347 | if (defined $ylabel and not $ylabel and @name == 1 and not $multiMulti) { | ||
| 348 | $ylabel = $hist ? 'Count' : $name[0]; | ||
| 349 | $noLegend = 1 unless $hist; | ||
| 350 | } | ||
| 351 | |||
| 352 | # make room for title/labels | ||
| 353 | $margin[1] += $$self{linespacing} * 1.5 if $title; | ||
| 354 | $margin[3] += $$self{linespacing} if $xlabel; | ||
| 355 | $margin[0] += $$self{linespacing} if $ylabel; | ||
| 356 | |||
| 357 | # calculate optimal number of X/Y grid lines | ||
| 358 | for ($i=0; $i<2; ++$i) { | ||
| 359 | $og[$i] = $ng[$i] * ($size[$i] - $margin[$i] - $margin[$i+2]) / | ||
| 360 | ($defaults{size}[$i] - $defaults{margin}[$i] - $defaults{margin}[$i+2]); | ||
| 361 | $og[$i] <= 0 and $$self{Error} = 'Invalid plot size', return; | ||
| 362 | } | ||
| 363 | if ($scat) { | ||
| 364 | $xdat = $$self{Data}{$xname}; | ||
| 365 | unless (defined $$self{xmin} and defined $$self{xmax}) { | ||
| 366 | my $set; | ||
| 367 | foreach (@$xdat) { | ||
| 368 | next unless defined; | ||
| 369 | $set or $xmin = $xmax = $_, $set = 1, next; | ||
| 370 | $xmin = $_ if $xmin > $_; | ||
| 371 | $xmax = $_ if $xmax < $_; | ||
| 372 | } | ||
| 373 | my $dnx2 = ($xmax - $xmin) / ($og[0] * 2); | ||
| 374 | # leave a bit of a left/right margin, but don't pass 0 | ||
| 375 | $xmin = ($xmin >= 0 and $xmin < $dnx2) ? 0 : $xmin - $dnx2; | ||
| 376 | $xmax = ($xmax <= 0 and -$xmax < $dnx2) ? 0 : $xmax + $dnx2; | ||
| 377 | } | ||
| 378 | $xmin = $$self{xmin} if defined $$self{xmin}; | ||
| 379 | $xmax = $$self{xmax} if defined $$self{xmax}; | ||
| 380 | } else { | ||
| 381 | # shift x range to correspond with index in data list | ||
| 382 | $xmax -= $xmin; | ||
| 383 | $xmin = 0; | ||
| 384 | } | ||
| 385 | if ($hist) { | ||
| 386 | $$self{nbins} > 0 or $$self{Error} = 'Invalid number of histogram bins', return; | ||
| 387 | $noLegend = 1; | ||
| 388 | # y axis becomes histogram x axis after binning | ||
| 389 | $min = $$self{xmin} if defined $$self{xmin}; | ||
| 390 | $max = $$self{xmax} if defined $$self{xmax}; | ||
| 391 | } else { | ||
| 392 | # leave a bit of a margin above/below data when autoscaling but don't pass 0 | ||
| 393 | my $dny2 = ($max - $min) / ($og[1] * 2); | ||
| 394 | $min = ($min >= 0 and $min < $dny2) ? 0 : $min - $dny2; | ||
| 395 | $max = ($max <= 0 and -$max < $dny2) ? 0 : $max + $dny2; | ||
| 396 | # adjust to user-defined range if specified | ||
| 397 | $min = $$self{ymin} if defined $$self{ymin}; | ||
| 398 | $max = $$self{ymax} if defined $$self{ymax}; | ||
| 399 | } | ||
| 400 | # generate random colors if we need more | ||
| 401 | while (@$cols < @$name) { | ||
| 402 | $$self{seeded} or srand(141), $$self{seeded} = 1; | ||
| 403 | push @$cols, sprintf("#%.2x%.2x%.2x",int(rand(220)),int(rand(220)),int(rand(220))); | ||
| 404 | } | ||
| 405 | $diff = GetRange($min, $max); | ||
| 406 | $xdiff = GetRange($xmin, $xmax); | ||
| 407 | |||
| 408 | # determine y grid spacing (nice even numbers) | ||
| 409 | $dy = GetGridSpacing($diff / ($hist ? $$self{nbins} : $og[1])); | ||
| 410 | # expand plot min/max to the nearest even multiple of our grid spacing | ||
| 411 | $min = ($min > 0 ? int($min/$dy) : int($min/$dy-0.9999)) * $dy; | ||
| 412 | $max = ($max > 0 ? int($max/$dy+0.9999) : int($max/$dy)) * $dy; | ||
| 413 | |||
| 414 | # bin histogram | ||
| 415 | if ($hist) { | ||
| 416 | my $dat = $$data{$name[0]}; | ||
| 417 | my $nmax = int(($max - $min) / $dy + 0.5); | ||
| 418 | @$hist = (0) x $nmax; | ||
| 419 | foreach (@$dat) { | ||
| 420 | next unless defined; | ||
| 421 | $n = ($_ - $min) / $dy; | ||
| 422 | next if $n < 0 or $n > $nmax + 0.00001; | ||
| 423 | $n = int($n); | ||
| 424 | ++$$hist[$n < $nmax ? $n : $nmax - 1]; | ||
| 425 | } | ||
| 426 | ($xmin, $xmax, $min, $max) = ($min, $max, 0, 0); | ||
| 427 | if ($$self{ymax}) { | ||
| 428 | $max = $$self{ymax}; | ||
| 429 | } else { | ||
| 430 | $max < $_ and $max = $_ foreach @$hist; # find max count | ||
| 431 | } | ||
| 432 | $diff = GetRange($min, $max); | ||
| 433 | $dx = $dy; | ||
| 434 | $dy = GetGridSpacing($diff / $og[1]); | ||
| 435 | $max = ($max > 0 ? int($max/$dy+0.9999) : int($max/$dy)) * $dy; | ||
| 436 | $$data{$name[0]} = $hist; | ||
| 437 | } else { | ||
| 438 | $dx = GetGridSpacing($xdiff / $og[0]); | ||
| 439 | } | ||
| 440 | if ($scat) { | ||
| 441 | $xmin = ($xmin > 0 ? int($xmin/$dx) : int($xmin/$dx-0.9999)) * $dx; | ||
| 442 | $xmax = ($xmax > 0 ? int($xmax/$dx+0.9999) : int($xmax/$dx)) * $dx; | ||
| 443 | } | ||
| 444 | $diff = GetRange($min, $max); | ||
| 445 | $xdiff = GetRange($xmin, $xmax); | ||
| 446 | # width/height of plot area | ||
| 447 | my $width = $size[0] - $margin[0] - $margin[2]; | ||
| 448 | my $height = $size[1] - $margin[1] - $margin[3]; | ||
| 449 | my $yscl = $height / $diff; | ||
| 450 | my $xscl = $width / $xdiff; | ||
| 451 | my $px0 = $margin[0] - $xmin * $xscl; | ||
| 452 | my $py0 = $margin[1] + $height + $min * $yscl; | ||
| 453 | my @clip = ($margin[0]-6*$wid, $margin[1]-6*$wid, $width+12*$wid, $height+12*$wid); | ||
| 454 | print $fp "\n<!-- Definitions -->\n<defs>\n<clipPath id='plot-area'>"; | ||
| 455 | print $fp "<rect x='$clip[0]' y='$clip[1]' width='$clip[2]' height='$clip[3]'/></clipPath>"; | ||
| 456 | if ($style =~ /\b[mp]/) { # 'm' for 'marker' or 'p' for 'point' (undocumented) | ||
| 457 | for ($i=0; $i<@name; ++$i) { | ||
| 458 | my @m = split /-/, ($$marks[$i] || $defaults{marks}[$i % @{$defaults{marks}}]); | ||
| 459 | my ($fill, $mark); | ||
| 460 | $fill = $m[2] || $$cols[$i] if $m[1] ? $m[1] =~ /^f/ : $style =~ /\bf/; | ||
| 461 | $mark = $markerData{$m[0]}; | ||
| 462 | $mark or $markID{$mark} = '', next; # skip 'none' or unrecognized marker name | ||
| 463 | if ($fill and $fill ne 'none') { | ||
| 464 | my $op = $m[3] || ($$cols[$i] eq 'none' ? 50 : 20); | ||
| 465 | $mark .= qq( fill="$fill" style="fill-opacity: $op%"); | ||
| 466 | } else { | ||
| 467 | $mark .= ' fill="none"'; | ||
| 468 | } | ||
| 469 | # (was using 'context-stroke', but Chrome didn't show this properly) | ||
| 470 | $mark .= " stroke='$$cols[$i]'/>"; | ||
| 471 | # don't re-define mark if it is the same as a previous one | ||
| 472 | $markID{$mark} and $markID{$i} = $markID{$mark}, next; | ||
| 473 | $markID{$mark} = $markID{$i} = "mark$i"; | ||
| 474 | print $fp "\n<marker id='$markID{$i}' markerWidth='8' markerHeight='8' refX='4'", | ||
| 475 | " refY='4'>\n$mark\n</marker>"; | ||
| 476 | } | ||
| 477 | print $fp "\n</defs>\n<style>"; | ||
| 478 | for ($i=0; $i<@name; ++$i) { | ||
| 479 | next unless $markID{$i} eq "mark$i"; | ||
| 480 | print $fp "\n path.mark$i { marker: url(#mark$i) }"; | ||
| 481 | } | ||
| 482 | } else { | ||
| 483 | print $fp "\n</defs>\n<style>"; | ||
| 484 | } | ||
| 485 | print $fp "\n text { fill: $$self{text} }\n</style>"; | ||
| 486 | print $fp "\n<rect x='0' y='0' width='$size[0]' height='$size[1]' fill='$$self{bkg}'/>" if $$self{bkg}; | ||
| 487 | print $fp "\n<!-- X axis -->"; | ||
| 488 | print $fp "\n<g dominant-baseline='hanging' text-anchor='middle'>"; | ||
| 489 | $py = int(($margin[1] + $height + $$tpad[1]) * 10 + 0.5) / 10; | ||
| 490 | $px = int(($margin[0] + $width / 2) * 10 + 0.5) / 10; | ||
| 491 | if ($title) { | ||
| 492 | print $fp "\n<text x='${px}' y='14' font-size='150%'>$title</text>"; | ||
| 493 | } | ||
| 494 | if ($xlabel) { | ||
| 495 | $y = $py + $$self{linespacing}; | ||
| 496 | print $fp "\n<text x='${px}' y='${y}'>$xlabel</text>"; | ||
| 497 | } | ||
| 498 | if ($ylabel) { | ||
| 499 | $y = $margin[1] + $height / 2; | ||
| 500 | print $fp "\n<text x='10' y='${y}' transform='rotate(-90,10,$y)'>$ylabel</text>"; | ||
| 501 | } | ||
| 502 | # make sure the X labels will fit | ||
| 503 | my $spc = $dx; | ||
| 504 | for (;;) { | ||
| 505 | # find longest label at current spacing | ||
| 506 | my $len = 0; | ||
| 507 | my $x0 = int($xmax / $spc + 0.5) * $spc; # get value of last x label | ||
| 508 | for ($i=0, $x=$x0; $i<3; ++$i, $x-=$spc) { | ||
| 509 | $n = length sprintf('%g', $x); | ||
| 510 | $len = $n if $len < $n; | ||
| 511 | } | ||
| 512 | last if $spc >= ($len + 1) * $wch * $xdiff / $width; | ||
| 513 | # increase label spacing by one increment and try again | ||
| 514 | $spc = $dx2 = GetGridSpacing($spc, 1); | ||
| 515 | } | ||
| 516 | my ($grid, $lastLen) = ('', 0); | ||
| 517 | for ($x=int($xmin/$dx-1)*$dx; ; $x+=$dx) { | ||
| 518 | $px = int(($margin[0] + ($x - $xmin) * $width / $xdiff) * 10 + 0.5) / 10; | ||
| 519 | next if $px < $margin[0] - 0.5; | ||
| 520 | last if $px > $margin[0] + $width + 0.5; | ||
| 521 | my $h = $height; | ||
| 522 | if (not $dx2 or abs($x/$dx2 - int($x/$dx2+($x>0 ? 0.5 : -0.5))) < 0.01) { | ||
| 523 | printf $fp "\n<text x='${px}' y='${py}'>%g</text>", $x; | ||
| 524 | $h += $$tpad[1]/2; | ||
| 525 | } | ||
| 526 | length($grid) - $lastLen > 80 and $grid .= "\n", $lastLen = length($grid); | ||
| 527 | $grid .= sprintf("M$px $margin[1] v$h "); | ||
| 528 | } | ||
| 529 | print $fp "\n<path stroke='$$self{grid}' stroke-width='0.5' d='\n${grid}'/>"; | ||
| 530 | print $fp "\n</g>\n<!-- Y axis -->\n<g dominant-baseline='middle' text-anchor='end'>"; | ||
| 531 | $px = int(($margin[0] - $$tpad[0]) * 10 + 0.5) / 10; | ||
| 532 | ($grid, $lastLen) = ('', 0); | ||
| 533 | my ($gx, $gw) = ($margin[0]-$$tpad[0]/2, $width + $$tpad[0]/2); | ||
| 534 | for ($y=$min; ; $y+=$dy) { | ||
| 535 | $py = int(($margin[1] + $height - ($y - $min) * $yscl) * 10 + 0.5) / 10; | ||
| 536 | last if $py < $margin[1] - 0.5; | ||
| 537 | $y = 0 if $y < $dy/2 and $y > -$dy/2; # (avoid round-off errors) | ||
| 538 | printf $fp "\n<text x='${px}' y='${py}'>%g</text>", $y; | ||
| 539 | $y < $dy/2 and $y > -$dy/2 and $xAxis = 1; # redraw x axis later | ||
| 540 | length($grid) - $lastLen > 80 and $grid .= "\n", $lastLen = length($grid); | ||
| 541 | $grid .= "M$gx $py h$gw "; | ||
| 542 | } | ||
| 543 | if ($xAxis and $min!=0) { | ||
| 544 | $py = $margin[1] + $height + $min * $yscl; | ||
| 545 | print $fp "\n<path stroke='$$self{text}' d='M$margin[0] $py h$width'/>"; | ||
| 546 | } | ||
| 547 | print $fp "\n<path stroke='$$self{grid}' stroke-width='0.5' d='\n${grid}'/>"; | ||
| 548 | print $fp "\n</g>\n<!-- Plot box and legend -->\n<g dominant-baseline='middle' text-anchor='start'>"; | ||
| 549 | print $fp "\n<path stroke='$$self{text}' fill='none' d='M$margin[0] $margin[1] l0 $height $width 0 0 -$height z'/>"; | ||
| 550 | for ($i=0; $i<@name and not $noLegend; ++$i) { | ||
| 551 | $x = $size[0] - $margin[2] - 175 + $$self{legend}[0]; | ||
| 552 | $y = $margin[1] + $$self{legend}[1] + 15 + $$self{linespacing} * ($i + 0.5); | ||
| 553 | my $col = $$cols[$i]; | ||
| 554 | my $mark = $markID{$i} ? " marker-end='url(#$markID{$i})' fill='none'" : ''; | ||
| 555 | my $line = ($style =~ /\bl/) ? ' l-20 0' : sprintf(' m%.4g 0', -5 * $wid); | ||
| 556 | my $sw = ($style =~ /\bm/ ? 1.5 : 2) * $wid; # (wider for line-only style so colour is more visible) | ||
| 557 | print $fp "\n<path$mark stroke-width='${sw}' stroke='${col}' d='M$x $y m-7 -1${line}'/>"; | ||
| 558 | print $fp "\n<text x='${x}' y='${y}'>$name[$i]</text>"; | ||
| 559 | } | ||
| 560 | # print the data | ||
| 561 | foreach (0..$#name) { | ||
| 562 | $col{$name[$_]} = $$cols[$_]; | ||
| 563 | $class{$name[$_]} = $markID{$_} ? " class='$markID{$_}'" : ''; | ||
| 564 | } | ||
| 565 | my ($i0, $i1, $xsclr); | ||
| 566 | my $fill = ''; | ||
| 567 | if ($scat) { | ||
| 568 | ($i0, $i1) = (0, $#$xdat); | ||
| 569 | } elsif ($hist) { | ||
| 570 | ($i0, $i1) = (0, $#$hist); | ||
| 571 | $xscl = $width / @$hist; | ||
| 572 | $px0 = $margin[0]; | ||
| 573 | $xsclr = int($xscl * 100 + 0.5) / 100; | ||
| 574 | if ($style =~ /\bf/) { | ||
| 575 | my @m = split /-/, $$marks[0]; | ||
| 576 | my $op = $m[3] || ($style =~ /\bl/ ? 20 : 50); | ||
| 577 | $fill = " fill='$$cols[0]'"; | ||
| 578 | $fill .= " style='fill-opacity: $op%'" if $$cols[0] ne 'none'; | ||
| 579 | } | ||
| 580 | } else { | ||
| 581 | $i0 = int($xmin) - 1; | ||
| 582 | $i0 = 0 if $i0 < 0; | ||
| 583 | $i1 = int($xmax) + 1; | ||
| 584 | } | ||
| 585 | print $fp "\n</g>\n<!-- Datasets -->\n<g fill='none' clip-path='url(#plot-area)'", | ||
| 586 | " stroke-linejoin='round' stroke-linecap='round' stroke-width='",1.5*$wid,"'>"; | ||
| 587 | my $doLines = $style =~ /\bl/; | ||
| 588 | foreach (@name) { | ||
| 589 | my $stroke = ($hist and not $doLines) ? 'none' : $col{$_}; | ||
| 590 | my $dat = $$data{$_}; | ||
| 591 | print $fp "\n<!-- $_ -->"; | ||
| 592 | print $fp "\n<path$class{$_}$fill stroke='${stroke}' d='"; | ||
| 593 | print $fp 'M' if $doLines; | ||
| 594 | my $m = $doLines ? '' : ' M'; | ||
| 595 | for ($i=$i0; $i<=$i1; ++$i) { | ||
| 596 | next unless defined $$dat[$i]; | ||
| 597 | $y = int(($py0 - $$dat[$i] * $yscl) * 10 + 0.5) / 10; | ||
| 598 | if ($scat) { | ||
| 599 | next unless defined $$xdat[$i]; | ||
| 600 | $x = int(($px0 + $$xdat[$i] * $xscl) * 10 + 0.5) / 10; | ||
| 601 | } else { | ||
| 602 | $x = int(($px0 + $i * $xscl) * 10 + 0.5) / 10; | ||
| 603 | if ($hist) { | ||
| 604 | print $fp $m, ($i % 5 ? ' ' : "\n"), "$x $y h$xsclr"; | ||
| 605 | $m = ' L'; # (draw lines after the first point) | ||
| 606 | next; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | print $fp $m, ($i % 10 ? ' ' : "\n"), "$x $y"; | ||
| 610 | } | ||
| 611 | print $fp ' V', $margin[1]+$height, " H$margin[0] z" if $hist and $fill; | ||
| 612 | print $fp "'/>"; | ||
| 613 | } | ||
| 614 | print $fp "\n</g>"; | ||
| 615 | print $fp "\n</g>" if $numPlots > 1; | ||
| 616 | } | ||
| 617 | print $fp "</svg>\n" or $$self{Error} = 'Error writing output plot file'; | ||
| 618 | } | ||
| 619 | |||
| 620 | 1; # end | ||
| 621 | |||
| 622 | __END__ |