jmcnamara / excel-writer-xlsx

Perl module to create Excel XLSX files.
https://metacpan.org/pod/distribution/Excel-Writer-XLSX/lib/Excel/Writer/XLSX.pm
Other
100 stars 51 forks source link

Cannot get rid of gridlines for Scatter Chart. #246

Closed Avucci closed 4 years ago

Avucci commented 4 years ago

Hello, I am trying to get rid of the gridlines on the y-axis (not the axis itself, but just the gridlines). I have a scatter chart and have tried the various methods below, yet when the chart is generated it still has gridlines. I know that you can delete the gridlines if you open the excel file and then manually hover over the gridlines and then press the delete key, but I was hoping there would be a way to automate this.

#! /bin/perl5.8.8 -w
use strict;
use warnings;

use Excel::Writer::XLSX;

my $workbook;
my %worksheet     = ();

&create_excel();
&build_scatter_graph();
&close_excel();

sub create_excel {
  my $filename = "avu.test.xlsx";
  $workbook = Excel::Writer::XLSX->new( $filename );
}

sub close_excel {
  $workbook->close;
}
sub build_scatter_graph {
  $worksheet{SCATTER_GRAPH} = $workbook->add_worksheet('SCATTER_GRAPH');
  my $chart = $workbook->add_chart( type => 'scatter', embedded => 1,);
  $chart->add_series (
    categories => '=SCATTER_GRAPH!$Y$2:$Y$7',
    values => '=SCATTER_GRAPH!$Z$2:$Z$7',
    marker => {type => 'square', size =>3},
  );
  my $data = [
    [ 'Category' , 2, 3, 4, 5, 6, 7 ],
    [ 'Value',     1, 4, 5, 2, 1, 5,],
  ];
  $worksheet{SCATTER_GRAPH}->write( 'Y2', $data );
  $chart->set_y_axis( major_gridlines => {visible => 0});
  $chart->set_y_axis( minor_gridlines => {visible => 0});
  $chart->set_x_axis( major_gridlines => {visible => 0});
  $chart->set_x_axis( minor_gridlines => {visible => 0});
  $chart->set_y_axis( line => { none => 1 } );
  $chart->set_y_axis( line => { none => 1 } );
  $chart->set_x_axis( line => { none => 1 } );
  $chart->set_x_axis( line => { none => 1 } );
  $chart->set_y_axis(
    major_gridlines => {
        visible => 0,
        line    => {width => 0},
    }
  );
  $chart->set_size  ( width => 1440, height => 864 );
  $chart->set_title ( name=> 'SCATTER_GRAPH' );
  $chart->set_x_axis ( name=> 'X-COORD (um)' );
  $chart->set_y_axis ( name=> 'Y-COORD (um)' );
  $worksheet{SCATTER_GRAPH}->set_zoom(90);
  $worksheet{SCATTER_GRAPH}->insert_chart('B2', $chart);
}

Is there a better way of doing this? Thanks.

jmcnamara commented 4 years ago

You should only call set_x_axis() or set_y_axis() once. Other calls will overwrite the previous call. If you want to change more than one property of the axis you need to group the properties into one call. Like this:

#! /bin/perl5.8.8 -w
use strict;
use warnings;

use Excel::Writer::XLSX;

my $workbook;
my %worksheet     = ();

&create_excel();
&build_scatter_graph();
&close_excel();

sub create_excel {
  my $filename = "avu.test.xlsx";
  $workbook = Excel::Writer::XLSX->new( $filename );
}

sub close_excel {
  $workbook->close;
}
sub build_scatter_graph {
  $worksheet{SCATTER_GRAPH} = $workbook->add_worksheet('SCATTER_GRAPH');
  my $chart = $workbook->add_chart( type => 'scatter', embedded => 1,);
  $chart->add_series (
    categories => '=SCATTER_GRAPH!$Y$2:$Y$7',
    values => '=SCATTER_GRAPH!$Z$2:$Z$7',
    marker => {type => 'square', size =>3},
  );
  my $data = [
    [ 'Category', 2, 3, 4, 5, 6, 7 ],
    [ 'Value',    1, 4, 5, 2, 1, 5],
  ];

  $worksheet{SCATTER_GRAPH}->write( 'Y2', $data );

  $chart->set_y_axis( major_gridlines => {visible => 0},
                      minor_gridlines => {visible => 0},
                      line => { none => 1 },
                      name=> 'Y-COORD (um)');

  $chart->set_x_axis( major_gridlines => {visible => 0},
                      minor_gridlines => {visible => 0},
                      line => { none => 1 },
                      name=> 'X-COORD (um)');

  $chart->set_size  ( width => 1440, height => 864 );
  $chart->set_title ( name=> 'SCATTER_GRAPH' );
  $worksheet{SCATTER_GRAPH}->set_zoom(90);
  $worksheet{SCATTER_GRAPH}->insert_chart('B2', $chart);
}

Output:

screenshot

Avucci commented 4 years ago

Ahh thank you for your reply. I have adjusted my code to your suggestion and now it works.