Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
CandleStick!
Browse files Browse the repository at this point in the history
  • Loading branch information
gphat committed May 9, 2009
1 parent 0d0be19 commit fee3566
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@ Revision history for Perl extension Chart::Clicker.
2.26
- Use min/max functions from List::Util rather than rolling my own
- Refactor Series internals to more easily facilitate subclasses
- Add HighLow series
- Add CandleStick renderer

2.25
- Add some new examples: Multiple-Axes, Multiple-Renderers and Shared-Axes
Expand Down
37 changes: 0 additions & 37 deletions META.yml

This file was deleted.

49 changes: 49 additions & 0 deletions example/candlestick.pl
@@ -0,0 +1,49 @@
#!/usr/bin/perl
use strict;

use Chart::Clicker;
use Chart::Clicker::Data::DataSet;
use Chart::Clicker::Data::Series::HighLow;
use Chart::Clicker::Renderer::CandleStick;
use Graphics::Color::RGB;

my $cc = Chart::Clicker->new(width => 500, height => 250, format => 'png');

my @hours = qw(
);

my $series1 = Chart::Clicker::Data::Series::HighLow->new(
keys => [qw(1 2 3 4 5 6 7 8 9 10)],
highs => [qw(5 9 7 8 8 9 5 4 7 9)],

lows => [qw(1 4 2 3 1 4 1 1 1 4)],
opens => [qw(3 5 4 6 4 8 4 1 1 6)],
values => [qw(5 4 6 4 8 4 1 1 6 9)]
);

# We'll create a dataset with our first two series in it...
my $ds = Chart::Clicker::Data::DataSet->new(
series => [ $series1 ]
);

# Pretty stuff
$cc->border->width(0);

# Add the datasets to the chart
$cc->add_to_datasets($ds);

# Set some labels on the default context
my $defctx = $cc->get_context('default');
$defctx->range_axis->label('Lorem');
$defctx->range_axis->fudge_amount(.2);
$defctx->domain_axis->label('Ipsum');
$defctx->domain_axis->tick_label_angle(0.785398163);
$defctx->domain_axis->fudge_amount(.06);

# Here's the magic: You can set a renderer for any context. In this case
# we'll change the default to a Bar. Voila!
$defctx->renderer(Chart::Clicker::Renderer::CandleStick->new(bar_padding => 30));

$cc->draw;
$cc->write('foo.png');
20 changes: 20 additions & 0 deletions example/very-simple.pl
@@ -0,0 +1,20 @@
#!/usr/bin/perl
use strict;

use Chart::Clicker;
use Chart::Clicker::Data::DataSet;
use Chart::Clicker::Data::Series;

my $cc = Chart::Clicker->new(width => 500, height => 400);

my $series1 = Chart::Clicker::Data::Series->new(
keys => qw(1 2 3 4 5 6 7 8 9 10 11 12),
values => qw(5.8 5.0 4.9 4.8 4.5 4.25 3.5 2.9 2.5 1.8 .9 .8)
);

my $ds = Chart::Clicker::Data::DataSet->new(series => [ $series1 ]);

$cc->add_to_datasets($ds);

$cc->draw;
$cc->write('foo.png');
4 changes: 0 additions & 4 deletions lib/Chart/Clicker.pm
Expand Up @@ -497,10 +497,6 @@ format provided to the constructor (which defaults to Png).
$c->write('/path/to/the.png');
=head2 BUILD
Documenting so tests pass. Moose stuff.
=head1 AUTHOR
Cory 'G' Watson <gphat@cpan.org>
Expand Down
1 change: 0 additions & 1 deletion lib/Chart/Clicker/Data/Series.pm
Expand Up @@ -37,7 +37,6 @@ sub find_range {
my ($self) = @_;

my $values = $self->values;
my $keys = $self->keys;

return Chart::Clicker::Data::Range->new(
lower => min(@{ $self->values }), upper => max(@{ $self->values})
Expand Down
157 changes: 157 additions & 0 deletions lib/Chart/Clicker/Data/Series/HighLow.pm
@@ -0,0 +1,157 @@
package Chart::Clicker::Data::Series::HighLow;
use Moose;

extends 'Chart::Clicker::Data::Series';

use List::Util qw(max min);

has 'highs' => (
metaclass => 'Collection::Array',
is => 'rw',
isa => 'ArrayRef',
default => sub { [] },
provides => {
'push' => 'add_to_highs',
'count' => 'high_count',
'get' => 'get_high'
}
);

has 'lows' => (
metaclass => 'Collection::Array',
is => 'rw',
isa => 'ArrayRef',
default => sub { [] },
provides => {
'push' => 'add_to_lows',
'count' => 'low_count',
'get' => 'get_low'
}
);

has 'opens' => (
metaclass => 'Collection::Array',
is => 'rw',
isa => 'ArrayRef',
default => sub { [] },
provides => {
'push' => 'add_to_opens',
'count' => 'open_count',
'get' => 'get_open'
}
);

sub find_range {
my ($self) = @_;

return Chart::Clicker::Data::Range->new(
lower => min(@{ $self->lows }),
upper => max(@{ $self->highs })
);
}

__PACKAGE__->meta->make_immutable;

no Moose;

1;

__END__
=head1 NAME
Chart::Clicker::Data::Series::HighLow
=head1 DESCRIPTION
Chart::Clicker::Data::Series::HighLow is an extension of the Series class
that provides storage for a three new variables called for use with the
CandleStick renderer. The general idea is:
--- <-- High
|
|
- <-- max of Open, Value
| |
| |
- <-- min of Open, Value
|
|
--- <-- Low
=head1 SYNOPSIS
use Chart::Clicker::Data::Series::HighLow;
my $series = Chart::Clicker::Data::Series::HighLow->new({
keys => \@keys,
values => \@values,
highs => \@highs,
lows => \@lows,
opens => \@opens
});
=head1 METHODS
=head2 new
Creates a new, empty Series::Size
=head2 add_to_highs
Adds a high to this series.
=head2 add_to_lows
Adds a high to this series.
=head2 add_to_opens
Adds an open to this series.
=head2 get_high
Get a high by it's index.
=head2 get_low
Get a low by it's index.
=head2 get_open
Get an open by it's index.
=head2 highs
Set/Get the highs for this series.
=head2 lows
Set/Get the lows for this series.
=head2 opens
Set/Get the opens for this series.
=head2 high_count
Gets the count of sizes in this series.
=head2 low_count
Gets the count of lows in this series.
=head2 open_count
Gets the count of opens in this series.
=head1 AUTHOR
Cory 'G' Watson <gphat@cpan.org>
=head1 LICENSE
You can redistribute and/or modify this code under the same terms as Perl
itself.
1;
16 changes: 8 additions & 8 deletions lib/Chart/Clicker/Drawing/ColorAllocator.pm
Expand Up @@ -162,14 +162,6 @@ colors are well ditinguishable from one another and have appropriate contrast.
=head1 ATTRIBUTES
=head2 seed_hue
The interger value of the first hue used when computing the tetrade color
scheme. Setting this will affect the hue of the first color allocated.
Subsequent colors will be allocated based on their distance from this color
to maintain sifficient contrast between colors. If not specified the seed_hue
will default to 270, blue.
=head2 color_scheme
A lazy-building L<Color::Scheme> object used to generate the color scheme of
Expand All @@ -181,6 +173,14 @@ An array reference of evenly spaced seed hues for color allocation. By default
it will use the seed hue plus 0, 45, 75, 15, 60 and 30 which is enough to cover
all web-safe colors when using a tetrade color scheme.
=head2 seed_hue
The interger value of the first hue used when computing the tetrade color
scheme. Setting this will affect the hue of the first color allocated.
Subsequent colors will be allocated based on their distance from this color
to maintain sifficient contrast between colors. If not specified the seed_hue
will default to 270, blue.
=head2 shade_order
An array reference of the order in which the different shades of each color
Expand Down

0 comments on commit fee3566

Please sign in to comment.