gphat / chart-clicker

Extensible, Beautiful Charts for Perl

This URL has Read+Write access

chart-clicker / lib / Chart / Clicker / Tutorial.pod
344b7b24 » gphat 2009-06-12 Tutorial and shape_brush 1 =head1 NAME
2
3 Chart::Clicker::Tutorial
4
5 =head1 DESCRIPTION
6
7 This document aims to provide a tutorial for using Chart::Clicker.
8
9 =head1 DISCLAIMER
10
11 This is a work in progress. If you find errors or would like to make
12 contributions, drop me a line!
13
14 =head1 EXAMPLES
15
16 =head2 Simple chart from a single data source
17
18 # grab the needed modules
19 use Chart::Clicker;
20 use Chart::Clicker::Data::Series;
21 use Chart::Clicker::Data::DataSet;
22
23 # build the chart
24 my $chart = Chart::Clicker->new;
25
26 # build the series (static here, will usually be supplied arrayrefs from elsewhere)
27 my $series = Chart::Clicker::Data::Series->new(
28 keys => [ 1,2,3,4,5 ],
29 values => [ 52,74,52,82,14 ],
30 );
31
32 # build the dataset
33 my $dataset = Chart::Clicker::Data::DataSet->new(
34 series => [ $series ],
35 );
36
37 # add the dataset to the chart
38 $chart->add_to_datasets($dataset);
39
40 # draw and write the chart to a file
41 $chart->draw;
42 $chart->write('chart.png');
43
44 =head2 Simple chart from multiple data sources
45
46 use Chart::Clicker;
47 use Chart::Clicker::Data::Series;
48 use Chart::Clicker::Data::DataSet;
49
50 my $chart = Chart::Clicker->new;
51
52 # start an array that will hold the series data
53 my $series1 = Chart::Clicker::Data::Series->new(
54 keys => [ 1,2,3,4,5 ],
55 values => [ 52,74,52,82,14 ]
56 );
57 my $series2 = Chart::Clicker::Data::Series->new(
58 keys => [ 1,2,3,4,5 ],
59 values => [ 34,67,89,45,67 ]
60 );
61
62 # add the array of series data to the dataset
63 my $dataset = Chart::Clicker::Data::DataSet->new(
64 series => [ $series1, $series2 ]
65 );
66
67 $chart->add_to_datasets($dataset);
68
69 $chart->draw;
70 $chart->write('chart.png');
71
72 =head2 Simple chart with multiple data sources and custom colors
73
74 use Chart::Clicker;
75 use Chart::Clicker::Data::Series;
76 use Chart::Clicker::Data::DataSet;
77 # some new modules, these are only needed if you want to monkey with color changing
78 use Graphics::Color::RGB;
79 use Chart::Clicker::Drawing::ColorAllocator;
80
81 # build the color allocator
82 my $ca = Chart::Clicker::Drawing::ColorAllocator->new;
83 # this hash is simply here to make things readable and cleaner, you can always call G::C::R inline
84 my $red => Graphics::Color::RGB->new({
85 red =>.75, green =>0, blue =>0, alpha =>.8
86 });
87 my $green => Graphics::Color::RGB->new({
88 red => 0,green => .75, blue=> 0, alpha=>.8
89 });
90 my $blue => Graphics::Color::RGB->new({
91 red => 0, green => 0, blue => .75, alpha => .8
92 }),
93
94 my $chart = Chart::Clicker->new;
95
96 # Create an empty dataset that we can add to
97 my $dataset = Chart::Clicker::Data::DataSet->new;
98
99 $dataset->add_to_series(Chart::Clicker::Data::Series->new(
100 keys => [ 1,2,3,4,5 ],
101 values => [ 52,74,52,82,14 ]
102 ));
103 # add a color - note that the order of colors and the order of the
104 # series must match, the first series will use the first color and so on
105 # see contexts and axes for alternate ways of doing this
106 $ca->add_to_colors($blue);
107
108 $dataset->add_to_series(Chart::Clicker::Data::Series->new(
109 keys => [ 1,2,3,4,5 ],
110 values => [ 34,67,89,45,67 ],
111 ));
112 # add a second color
113 $ca->add_to_colors($colorset{red});
114
115 $dataset->add_to_series(Chart::Clicker::Data::Series->new(
116 keys => [ 1,2,3,4,5 ],
117 values => [ 11,22,33,44,55 ],
118 ));
119 # add a third color
120 $ca->add_to_colors($colorset{green});
121
122 $chart->add_to_datasets($dataset);
123
124 # assign the color allocator to the chart
125 $chart->color_allocator($ca);
126
127 $chart->draw;
128 $chart->write('chart.png');
129
130 =head2 Example 4 : Simple chart with a different render type
131
132 use Chart::Clicker;
133 use Chart::Clicker::Data::Series;
134 use Chart::Clicker::Data::DataSet;
135 # add in the module of the renerer(s) you want to use
136 use Chart::Clicker::Renderer::Area;
137
138 my $chart=Chart::Clicker->new;
139
140 my $series=Chart::Clicker::Data::Series->new(
141 keys => [ 1,2,3,4,5 ],
142 values => [ 52,74,52,82,14 ]
143 );
144
145 my $dataset = Chart::Clicker::Data::DataSet->new(
146 series => [ $series ]
147 );
148
149 $chart->add_to_datasets($dataset);
150
151 # build the renderer to use
152 my $renderer = Chart::Clicker::Renderer::Area->new(
153 opacity => .75,
154 );
155
156 # assign the renderer to the default context
157 $chart->get_context('default')->renderer($renderer);
158
159 $chart->draw;
160 $chart->write('chart.png');
161
162 =head1 AUTHOR
163
164 Cory G Watson <gphat@cpan.org>
165
166 =head1 CONTRIBUTORS
167
168 Steve Bradford
169
170 =head1 LICENSE
171
172 You can redistribute and/or modify this code under the same terms as Perl
173 itself.