VS::Chart::Renderer - Base class for renderers


VS-Chart documentation Contained in the VS-Chart distribution.

Index


Code Index:

NAME

Top

VS::Chart::Renderer - Base class for renderers

ATTRIBUTES

Top

BACKGROUND

background = ( 0 | 1 | COLOR )

Controls whether a background should be drawn for all of the resulting picture. Defaults to 1. Standard color is white.

INTERFACE

Top

CLASS METHODS

new

Creates a new renderer instance.

set_defaults ( CHART )

Sets defaults attribute for the chart and returns a list of keys it's added. If an attribute already exists it's unaffected.

render ( CHART, SURFACE )

Renders the chart to the Cairo surface.

render_background ( CHART, SURFACE )

Renders the chart background if one is declared.

set_font ( CHART, SURFACE, SECTION)

Sets the current font face and size to what is defined by the settings for SECTION. For example, labels_font_face, labels_font_size, labels_font_slant and labels_font_weight.


VS-Chart documentation Contained in the VS-Chart distribution.

package VS::Chart::Renderer;

use strict;
use warnings;

use Scalar::Util qw(blessed);

my %Defaults = (
    background  => 1,
);

sub set_defaults {
    my ($self, $chart) = @_;

    my @keys;
    while (my ($key, $value) = each %Defaults) {
       unless ($chart->has($key)) {
           $chart->set($key => $value);
           push @keys, $key;
       }
    }
    
    return @keys;
}

sub new {
    my ($pkg) = @_;
    my $self = bless \do { my $v; }, $pkg;
    return $self;
}

sub render {
    my ($self, $chart, $surface) = @_;
    $self->render_background($chart, $surface);
}

sub render_background {
    my ($self, $chart, $surface) = @_;
    return unless $chart->get("background");
    my $cx = Cairo::Context->create($surface);
    my $color = VS::Chart::Color->get($chart->get("background"), "white");
    $color->set($cx, $surface, $chart->get("width"), $chart->get("height"));
    $cx->paint;
}

sub set_font {
    my ($self, $cx, $chart, $section) = @_;
    
    my $face = $chart->get("${section}_font_face");
    if ($face) {
        my $slant = $chart->get("${section}_font_slant") || "normal";
        my $weight = $chart->get("${section}_font_weight") || "normal";
        $cx->select_font_face($face, $slant, $weight);
    }
    
    my $size = $chart->get("${section}_font_size");
    if ($size) {
        $cx->set_font_size($size);
    }
}

1;
__END__