1SVG::TT::Graph::Bar(3)User Contributed Perl DocumentationSVG::TT::Graph::Bar(3)
2
3
4
6 SVG::TT::Graph::Bar - Create presentation quality SVG bar graphs easily
7
9 use SVG::TT::Graph::Bar;
10
11 my @fields = qw(Jan Feb Mar);
12 my @data_sales_02 = qw(12 45 21);
13
14 my $graph = SVG::TT::Graph::Bar->new({
15 'height' => '500',
16 'width' => '300',
17 'fields' => \@fields,
18 });
19
20 $graph->add_data({
21 'data' => \@data_sales_02,
22 'title' => 'Sales 2002',
23 });
24
25 print "Content-type: image/svg+xml\n\n";
26 print $graph->burn();
27
29 This object aims to allow you to easily create high quality SVG bar
30 graphs. You can either use the default style sheet or supply your own.
31 Either way there are many options which can be configured to give you
32 control over how the graph is generated - with or without a key, data
33 elements at each point, title, subtitle etc.
34
36 new()
37 use SVG::TT::Graph::Bar;
38
39 # Field names along the X axis
40 my @fields = qw(Jan Feb Mar);
41
42 my $graph = SVG::TT::Graph::Bar->new({
43 # Required
44 'fields' => \@fields,
45
46 # Optional - defaults shown
47 'height' => '500',
48 'width' => '300',
49
50 'show_data_values' => 1,
51
52 'min_scale_value' => '0',
53 'max_scale_value' => undef,
54 'stagger_x_labels' => 0,
55 'rotate_x_labels' => 0,
56 'bar_gap' => 1,
57
58 'show_x_labels' => 1,
59 'show_y_labels' => 1,
60 'scale_integers' => 0,
61 'scale_divisions' => '',
62 'y_label_formatter' => sub { return @_ },
63 'x_label_formatter' => sub { return @_ },
64
65 'show_path_title' => 0,
66 'show_title_fields' => 0,
67
68 'show_x_title' => 0,
69 'x_title' => 'X Field names',
70
71 'show_y_title' => 0,
72 'y_title_text_direction' => 'bt',
73 'y_title' => 'Y Scale',
74
75 'show_graph_title' => 0,
76 'graph_title' => 'Graph Title',
77 'show_graph_subtitle' => 0,
78 'graph_subtitle' => 'Graph Sub Title',
79
80 'key' => 0,
81 'key_position' => 'right',
82
83 # Stylesheet defaults
84 'style_sheet' => '/includes/graph.css', # internal stylesheet
85 'random_colors' => 0,
86 });
87
88 The constructor takes a hash reference, fields (the names for each
89 field on the X axis) MUST be set, all other values are defaulted to
90 those shown above - with the exception of style_sheet which defaults to
91 using the internal style sheet.
92
93 add_data()
94 my @data_sales_02 = qw(12 45 21);
95
96 $graph->add_data({
97 'data' => \@data_sales_02,
98 'title' => 'Sales 2002',
99 });
100
101 This method allows you to add data to the graph object. It can be
102 called several times to add more data sets in, but the likelihood is
103 you should be using SVG::TT::Graph::Line as it won't look great!
104
105 clear_data()
106 my $graph->clear_data();
107
108 This method removes all data from the object so that you can reuse it
109 to create a new graph but with the same config options.
110
111 burn()
112 print $graph->burn();
113
114 This method processes the template with the data and config which has
115 been set and returns the resulting SVG.
116
117 This method will croak unless at least one data set has been added to
118 the graph object.
119
120 config methods
121 my $value = $graph->method();
122 my $confirmed_new_value = $graph->method($value);
123
124 The following is a list of the methods which are available to change
125 the config of the graph object after it has been created.
126
127 height()
128 Set the height of the graph box, this is the total height of the
129 SVG box created - not the graph it self which auto scales to fix
130 the space.
131
132 width()
133 Set the width of the graph box, this is the total width of the SVG
134 box created - not the graph it self which auto scales to fix the
135 space.
136
137 compress()
138 Whether or not to compress the content of the SVG file
139 (Compress::Zlib required).
140
141 tidy()
142 Whether or not to tidy the content of the SVG file (XML::Tidy
143 required).
144
145 style_sheet()
146 Set the path to an external stylesheet, set to '' if you want to
147 revert back to using the defaut internal version.
148
149 Set to "inline:<style>...</style>" with your CSS in between the
150 tags. You can thus override the default style without requireing
151 an external URL.
152
153 The default stylesheet handles up to 12 data sets. All data series
154 over the 12th will have no style and be in black. If you have over
155 12 data sets you can assign them all random colors (see the
156 random_color() method) or create your own stylesheet and add the
157 additional settings for the extra data sets.
158
159 To create an external stylesheet create a graph using the default
160 internal version and copy the stylesheet section to an external
161 file and edit from there.
162
163 random_colors()
164 Use random colors in the internal stylesheet
165
166 show_data_values()
167 Show the value of each element of data on the graph
168
169 bar_gap()
170 Whether to have a gap between the bars or not, default is '1', set
171 to '0' if you don't want gaps.
172
173 min_scale_value()
174 The point at which the Y axis starts, defaults to '0', if set to ''
175 it will default to the minimum data value.
176
177 max_scale_value()
178 The maximum value for the Y axis. If set to '', it will default to
179 the maximum data value.
180
181 show_x_labels()
182 Whether to show labels on the X axis or not, defaults to 1, set to
183 '0' if you want to turn them off.
184
185 stagger_x_labels()
186 This puts the labels at alternative levels so if they are long
187 field names they will not overlap so easily. Default it '0', to
188 turn on set to '1'.
189
190 rotate_x_labels()
191 This turns the X axis labels by 90 degrees. Default it '0', to
192 turn on set to '1'.
193
194 show_y_labels()
195 Whether to show labels on the Y axis or not, defaults to 1, set to
196 '0' if you want to turn them off.
197
198 scale_integers()
199 Ensures only whole numbers are used as the scale divisions.
200 Default it '0', to turn on set to '1'. This has no effect if scale
201 divisions are less than 1.
202
203 scale_divisions()
204 This defines the gap between markers on the Y axis, default is a
205 10th of the max_value, e.g. you will have 10 markers on the Y axis.
206 NOTE: do not set this too low - you are limited to 999 markers,
207 after that the graph won't generate.
208
209 show_x_title()
210 Whether to show the title under the X axis labels, default is 0,
211 set to '1' to show.
212
213 x_title()
214 What the title under X axis should be, e.g. 'Months'.
215
216 show_y_title()
217 Whether to show the title under the Y axis labels, default is 0,
218 set to '1' to show.
219
220 y_title_text_direction()
221 Aligns writing mode for Y axis label. Defaults to 'bt' (Bottom to
222 Top). Change to 'tb' (Top to Bottom) to reverse.
223
224 y_title()
225 What the title under Y axis should be, e.g. 'Sales in thousands'.
226
227 show_graph_title()
228 Whether to show a title on the graph, defaults to 0, set to '1' to
229 show.
230
231 graph_title()
232 What the title on the graph should be.
233
234 show_graph_subtitle()
235 Whether to show a subtitle on the graph, defaults to 0, set to '1'
236 to show.
237
238 graph_subtitle()
239 What the subtitle on the graph should be.
240
241 key()
242 Whether to show a key, defaults to 0, set to '1' if you want to
243 show it.
244
245 key_position()
246 Where the key should be positioned, defaults to 'right', set to
247 'bottom' if you want to move it.
248
249 x_label_formatter ()
250 A callback subroutine which will format a label on the x axis. For
251 example:
252
253 $graph->x_label_formatter( sub { return '$' . $_[0] } );
254
255 y_label_formatter()
256 A callback subroutine which will format a label on the y axis. For
257 example:
258
259 $graph->y_label_formatter( sub { return '$' . $_[0] } );
260
261 show_path_title()
262 Whether to add the title attribute to the data path tags, which
263 will show "tooltips" when hovering over the bar area.
264
265 show_title_fields()
266 Whether to show field values as title elements in path tag,
267 defaults to 0, set to '1' to turn on. Suggest on single add_data
268 graphs, for overlapping graphs leave off to see the title value
269 used in the add_data call.
270
272 For examples look at the project home page
273 http://leo.cuckoo.org/projects/SVG-TT-Graph/
274
276 None by default.
277
279 SVG::TT::Graph, SVG::TT::Graph::Line, SVG::TT::Graph::BarHorizontal,
280 SVG::TT::Graph::BarLine, SVG::TT::Graph::Pie,
281 SVG::TT::Graph::TimeSeries, SVG::TT::Graph::XY, Compress::Zlib,
282 XML::Tidy
283
284
285
286perl v5.30.0 2019-07-26 SVG::TT::Graph::Bar(3)