Wednesday, October 18, 2017

Fix error "Invalid data set: 0" in Perl module GD::Graph

Error Message:
Invalid data set: 0 at D:\New\TOOL\graph.pl line 24.

Problem Code:

my @a1;
my @a2 = [-9,8,0,1,-2,-5,6,-4,3,7];

foreach my $i (1..10)
{
    push @a1, $i;
}

my @data = (
    @a1,
    @a2
);

Solution:

my @a1;
my @a2 = [-9,8,0,1,-2,-5,6,-4,3,7];

foreach my $i (1..10)
{
    push @a1, $i;
}

my @data = (
    [@a1],
    @a2
);



Complete working code:

#!/usr/bin/env perl
use strict;
use warnings;
use GD::Graph;
use GD::Graph::bars;

my @a1;
my @a2 = [-9,8,0,1,-2,-5,6,-4,3,7];

foreach my $i (1..10)
{
    push @a1, $i;
}

my @data = (
    [@a1],
    @a2
);

my $x_Label = "XLabel";
my $Graph_Title = "Graph Title";

my $graph = new GD::Graph::bars(800,600);
my $gd = $graph->plot(\@data) or die $graph->error;
open my $IMG, '>', 'file2.png' or die $!;
binmode $IMG;
print $IMG $gd->png;