Excel-Writer-XLSX

 view release on metacpan or  search on metacpan

lib/Excel/Writer/XLSX/Chart.pm  view on Meta::CPAN

#
# Set the properties of the secondary Y-axis.
#
sub set_y2_axis {

    my $self = shift;

    my $axis = $self->_convert_axis_args( $self->{_y2_axis}, @_ );

    $self->{_y2_axis} = $axis;
}


###############################################################################
#
# set_title()
#
# Set the properties of the chart title.
#
sub set_title {

    my $self  = shift;
    my %arg   = @_;
    my $title = {};

    my ( $name, $name_formula ) =
      $self->_process_names( $arg{name}, $arg{name_formula} );

    my $data_id = $self->_get_data_id( $name_formula, $arg{data} );

    $title->{_name}    = $name;
    $title->{_formula} = $name_formula;
    $title->{_data_id} = $data_id;

    # Set the font properties if present.
    $title->{_font} = $self->_convert_font_args( $arg{name_font} );
    if ( $arg{font} ) {
        $title->{_font} = $self->_convert_font_args( $arg{font} );
    }

    # Set the line properties.
    $title->{_line} = $self->_get_line_properties( $arg{line} );

    # Allow 'border' as a synonym for 'line'.
    if ( $arg{border} ) {
        $title->{_line} = $self->_get_line_properties( $arg{border} );
    }

    # Set the fill properties.
    $title->{_fill} = $self->_get_fill_properties( $arg{fill} );

    # Set the pattern properties.
    $title->{_pattern} = $self->_get_pattern_properties( $arg{pattern} );

    # Set the gradient fill properties.
    $title->{_gradient} = $self->_get_gradient_properties( $arg{gradient} );

    # Set the title layout.
    $title->{_layout} = $self->_get_layout_properties( $arg{layout}, 1 );

    # Set the title overlay option.
    $title->{_overlay} = $arg{overlay};

    # Set the no automatic title option.
    $title->{_none} = $arg{none};

    # Copy the title to the main chart object.
    $self->{_title} = $title;
}


###############################################################################
#
# set_legend()
#
# Set the properties of the chart legend.
#
sub set_legend {

    my $self = shift;

    # Convert the user defined properties to internal properties.
    $self->{_legend} = $self->_get_legend_properties( @_ );
}


###############################################################################
#
# set_plotarea()
#
# Set the properties of the chart plotarea.
#
sub set_plotarea {

    my $self = shift;

    # Convert the user defined properties to internal properties.
    $self->{_plotarea} = $self->_get_area_properties( @_ );
}


###############################################################################
#
# set_chartarea()
#
# Set the properties of the chart chartarea.
#
sub set_chartarea {

    my $self = shift;

    # Convert the user defined properties to internal properties.
    $self->{_chartarea} = $self->_get_area_properties( @_ );
}


###############################################################################
#
# set_style()
#
# Set on of the 48 built-in Excel chart styles. The default style is 2.
#

lib/Excel/Writer/XLSX/Chart.pm  view on Meta::CPAN

sub _write_c_minor_unit {

    my $self = shift;
    my $val  = shift;

    return unless $val;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:minorUnit', @attributes );
}


##############################################################################
#
# _write_c_major_time_unit()
#
# Write the <c:majorTimeUnit> element.
#
sub _write_c_major_time_unit {

    my $self = shift;
    my $val = shift || 'days';

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:majorTimeUnit', @attributes );
}


##############################################################################
#
# _write_c_minor_time_unit()
#
# Write the <c:minorTimeUnit> element.
#
sub _write_c_minor_time_unit {

    my $self = shift;
    my $val = shift || 'days';

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:minorTimeUnit', @attributes );
}


##############################################################################
#
# _write_legend()
#
# Write the <c:legend> element.
#
sub _write_legend {

    my $self          = shift;
    my $legend        = $self->{_legend};
    my $position      = $legend->{_position} || 'right';
    my $font          = $legend->{_font};
    my @delete_series = ();
    my $overlay       = 0;

    if ( defined $legend->{_delete_series}
        && ref $legend->{_delete_series} eq 'ARRAY' )
    {
        @delete_series = @{ $legend->{_delete_series} };
    }

    if ( $position =~ s/^overlay_// ) {
        $overlay = 1;
    }

    my %allowed = (
        right     => 'r',
        left      => 'l',
        top       => 't',
        bottom    => 'b',
        top_right => 'tr',
    );

    return if $position eq 'none';
    return unless exists $allowed{$position};

    $position = $allowed{$position};

    $self->xml_start_tag( 'c:legend' );

    # Write the c:legendPos element.
    $self->_write_legend_pos( $position );

    # Remove series labels from the legend.
    for my $index ( @delete_series ) {

        # Write the c:legendEntry element.
        $self->_write_legend_entry( $index );
    }

    # Write the c:layout element.
    $self->_write_layout( $legend->{_layout}, 'legend' );

    # Write the c:overlay element.
    $self->_write_overlay() if $overlay;

    # Write the c:spPr element.
    $self->_write_sp_pr( $legend );

    # Write the c:txPr element.
    if ( $font ) {
        $self->_write_tx_pr( $font );
    }

    $self->xml_end_tag( 'c:legend' );
}


##############################################################################
#
# _write_legend_pos()
#
# Write the <c:legendPos> element.
#
sub _write_legend_pos {

    my $self = shift;
    my $val  = shift;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:legendPos', @attributes );
}


##############################################################################
#
# _write_legend_entry()
#
# Write the <c:legendEntry> element.
#
sub _write_legend_entry {

    my $self  = shift;
    my $index = shift;

    $self->xml_start_tag( 'c:legendEntry' );

    # Write the c:idx element.
    $self->_write_idx( $index );

    # Write the c:delete element.
    $self->_write_delete( 1 );

    $self->xml_end_tag( 'c:legendEntry' );
}


##############################################################################
#
# _write_overlay()
#
# Write the <c:overlay> element.
#
sub _write_overlay {

    my $self = shift;
    my $val  = 1;

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:overlay', @attributes );
}


##############################################################################
#
# _write_plot_vis_only()
#
# Write the <c:plotVisOnly> element.
#
sub _write_plot_vis_only {

    my $self = shift;
    my $val  = 1;

    # Ignore this element if we are plotting hidden data.
    return if $self->{_show_hidden_data};

    my @attributes = ( 'val' => $val );

    $self->xml_empty_tag( 'c:plotVisOnly', @attributes );
}


##############################################################################
#
# _write_print_settings()
#
# Write the <c:printSettings> element.
#
sub _write_print_settings {

    my $self = shift;

    $self->xml_start_tag( 'c:printSettings' );

    # Write the c:headerFooter element.
    $self->_write_header_footer();

    # Write the c:pageMargins element.
    $self->_write_page_margins();

    # Write the c:pageSetup element.
    $self->_write_page_setup();

    $self->xml_end_tag( 'c:printSettings' );
}


##############################################################################
#
# _write_header_footer()
#
# Write the <c:headerFooter> element.
#
sub _write_header_footer {

    my $self = shift;

    $self->xml_empty_tag( 'c:headerFooter' );
}

lib/Excel/Writer/XLSX/Chart.pm  view on Meta::CPAN

        'r'      => $r,
        't'      => $t,
        'header' => $header,
        'footer' => $footer,
    );

    $self->xml_empty_tag( 'c:pageMargins', @attributes );
}


##############################################################################
#
# _write_page_setup()
#
# Write the <c:pageSetup> element.
#
sub _write_page_setup {

    my $self = shift;

    $self->xml_empty_tag( 'c:pageSetup' );
}


##############################################################################
#
# _write_auto_title_deleted()
#
# Write the <c:autoTitleDeleted> element.
#
sub _write_auto_title_deleted {

    my $self = shift;

    my @attributes = ( 'val' => 1 );

    $self->xml_empty_tag( 'c:autoTitleDeleted', @attributes );
}


##############################################################################
#
# _write_title_rich()
#
# Write the <c:title> element for a rich string.
#
sub _write_title_rich {

    my $self      = shift;
    my $title     = shift;
    my $is_y_axis = shift;

    $self->xml_start_tag( 'c:title' );

    # Write the c:tx element.
    $self->_write_tx_rich( $title->{_name}, $is_y_axis, $title->{_font} );

    # Write the c:layout element.
    $self->_write_layout( $title->{_layout}, 'text' );

    # Write the c:overlay element.
    $self->_write_overlay() if $title->{_overlay};

    # Write the c:spPr element.
    $self->_write_sp_pr( $title );

    $self->xml_end_tag( 'c:title' );
}


##############################################################################
#
# _write_title_formula()
#
# Write the <c:title> element for a formulas
#
sub _write_title_formula {

    my $self      = shift;
    my $title     = shift;
    my $is_y_axis = shift;

    $self->xml_start_tag( 'c:title' );

    # Write the c:tx element.
    $self->_write_tx_formula( $title->{_formula}, $title->{_data_id} );

    # Write the c:layout element.
    $self->_write_layout( $title->{_layout}, 'text' );

    # Write the c:overlay element.
    $self->_write_overlay() if $title->{_overlay};

    # Write the c:spPr element.
    $self->_write_sp_pr( $title );

    # Write the c:txPr element.
    $self->_write_tx_pr( $title->{_font}, $is_y_axis );

    $self->xml_end_tag( 'c:title' );
}


##############################################################################
#
# _write_title_format_only()
#
# Write the <c:title> for a title with formatting but not text change.
#
sub _write_title_format_only {

    my $self  = shift;
    my $title = shift;

    $self->xml_start_tag( 'c:title' );

    # Write the c:layout element.
    $self->_write_layout( $title->{_layout}, 'text' );

    # Write the c:overlay element.
    $self->_write_overlay() if $title->{_overlay};

    # Write the c:spPr element.
    $self->_write_sp_pr( $title );

    $self->xml_end_tag( 'c:title' );
}


##############################################################################
#
# _write_tx_rich()
#
# Write the <c:tx> element.
#
sub _write_tx_rich {

    my $self          = shift;
    my $title         = shift;
    my $is_y_axis     = shift;
    my $font          = shift;

    $self->xml_start_tag( 'c:tx' );

    # Write the c:rich element.
    $self->_write_rich( $title, $font, $is_y_axis );

    $self->xml_end_tag( 'c:tx' );
}


##############################################################################
#
# _write_tx_value()
#
# Write the <c:tx> element with a simple value such as for series names.
#
sub _write_tx_value {

    my $self  = shift;
    my $title = shift;

    $self->xml_start_tag( 'c:tx' );

    # Write the c:v element.
    $self->_write_v( $title );

    $self->xml_end_tag( 'c:tx' );
}


##############################################################################
#
# _write_tx_formula()
#
# Write the <c:tx> element.
#
sub _write_tx_formula {

    my $self    = shift;
    my $title   = shift;

lib/Excel/Writer/XLSX/Chart.pm  view on Meta::CPAN



=head2 set_size()

The C<set_size()> method is used to set the dimensions of the chart. The size properties that can be set are:

     width
     height
     x_scale
     y_scale
     x_offset
     y_offset

The C<width> and C<height> are in pixels. The default chart width is 480 pixels and the default height is 288 pixels. The size of the chart can be modified by setting the C<width> and C<height> or by setting the C<x_scale> and C<y_scale>:

    $chart->set_size( width => 720, height => 576 );

    # Same as:

    $chart->set_size( x_scale => 1.5, y_scale => 2 );

The C<x_offset> and C<y_offset> position the top left corner of the chart in the cell that it is inserted into.


Note: the C<x_scale>, C<y_scale>, C<x_offset> and C<y_offset> parameters can also be set via the C<insert_chart()> method:

    $worksheet->insert_chart( 'E2', $chart, { x_offset =>2,    y_offset => 4,
                                              x_scale  => 1.5, y_scale  => 2 } );


=head2 set_title()

The C<set_title()> method is used to set properties of the chart title.

    $chart->set_title( name => 'Year End Results' );

The properties that can be set are:

=over

=item * C<name>

Set the name (title) for the chart. The name is displayed above the chart. The name can also be a formula such as C<=Sheet1!$A$1>. The name property is optional. The default is to have no chart title.

=item * C<font>

Set the font properties for the chart title. See the L</CHART FONTS> section below.

=item * C<fill>

Set the fill properties of the legend such as colour. See the L</CHART FORMATTING> section below.

=item * C<pattern>

Set the pattern fill properties of the legend. See the L</CHART FORMATTING> section below.

=item * C<gradient>

Set the gradient fill properties of the legend. See the L</CHART FORMATTING> section below.

=item * C<overlay>

Allow the title to be overlaid on the chart. Generally used with the layout property below.

=item * C<layout>

Set the C<(x, y)> position of the title in chart relative units:

    $chart->set_title(
        name    => 'Title',
        overlay => 1,
        layout  => {
            x => 0.42,
            y => 0.14,
        }
    );

See the L</CHART LAYOUT> section below.

=item * C<none>

By default Excel adds an automatic chart title to charts with a single series and a user defined series name. The C<none> option turns this default title off. It also turns off all other C<set_title()> options.

    $chart->set_title( none => 1 );

=back


=head2 set_legend()

The C<set_legend()> method is used to set properties of the chart legend.


The properties that can be set are:

=over

=item * C<none>

The C<none> option turns off the chart legend. In Excel chart legends are on by default:

    $chart->set_legend( none => 1 );

Note, for backward compatibility, it is also possible to turn off the legend via the C<position> property:

    $chart->set_legend( position => 'none' );

=item * C<position>

Set the position of the chart legend.

    $chart->set_legend( position => 'bottom' );

The default legend position is C<right>. The available positions are:

    top
    bottom
    left
    right
    top_right
    overlay_left
    overlay_right
    overlay_top_right
    none

=item * C<border>

Set the border properties of the legend such as colour and style. See the L</CHART FORMATTING> section below.

=item * C<fill>

Set the fill properties of the legend such as colour. See the L</CHART FORMATTING> section below.

=item * C<pattern>

Set the pattern fill properties of the legend. See the L</CHART FORMATTING> section below.

=item * C<gradient>

Set the gradient fill properties of the legend. See the L</CHART FORMATTING> section below.


=item * C<font>

Set the font properties of the chart legend:

    $chart->set_legend( font => { bold => 1, italic => 1 } );

See the L</CHART FONTS> section below.

=item * C<delete_series>

This allows you to remove 1 or more series from the legend (the series will still display on the chart). This property takes an array ref as an argument and the series are zero indexed:

    # Delete/hide series index 0 and 2 from the legend.
    $chart->set_legend( delete_series => [0, 2] );

=item * C<layout>

Set the C<(x, y)> position of the legend in chart relative units:

    $chart->set_legend(
        layout => {
            x      => 0.80,
            y      => 0.37,
            width  => 0.12,
            height => 0.25,
        }
    );

See the L</CHART LAYOUT> section below.

=back


=head2 set_chartarea()

The C<set_chartarea()> method is used to set the properties of the chart area.

    $chart->set_chartarea(
        border => { none  => 1 },
        fill   => { color => 'red' }
    );



( run in 2.667 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )