Wednesday, October 18, 2017

Fix error "Not a valid source data structure" in Perl module GD::Graph

Error Message:
Not a valid source data structure at D:\New\TOOL\graph.pl line 24.

Problem Code:
my $gd = $graph->plot(@data) or die $graph->error;

Solution:
my $gd = $graph->plot(\@data) or die $graph->error;

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;