• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

Perl Graph: How to use more colors and display labels on the line?

(Perlscript, Linux OS)
1. I am generation Line graph ( using GD::Graph::linepoints). It will suport only 30 colors,

qw(blue yellow green red orange dpink purple white gray marine gold dgreen lblue dyellow lred dpurple pink dbrown dgray dblue lyellow lgreen lpurple lorange lgray lbrown dred black cyan)

In my chart the lines are more than 30 so the color has dublicated. Is any way to get more colors?

2. I want to display the lable on the line for example (data1,data2 etc). I am using legends but user needs the info on/besides the line.

How can I do this? Help me.

VS
0
jessi_vs
Asked:
jessi_vs
  • 6
  • 6
1 Solution
 
kanduraCommented:
you can create new colors with the GD::Graph::colour module:

    use GD::Graph::colour qw/:colours/;
    add_colour( colourname => [ $r, $g, $b ] );
    #or
    add_colour( '#7fe310' );

you can also read in a list of colors from a file (in the format of the X11 system):

    use GD::Graph::colour qw/:files/;
    read_rgb( $filename );


   
0
 
jessi_vsAuthor Commented:
Thanks, but the problem is I could not use add_colour values inside the

dclrs =>[qw(...)]

I can use add_colour values like:

axislableclr=>'#006633',
valuesclr='#009900',
.
.


VS
0
 
kanduraCommented:
jessi_vs,
> Thanks, but the problem is I could not use add_colour values inside the

are you saying that this doesn't work?

    use GD::Graph::colour qw/:colours/;
    add_colour( colourname => [ $r, $g, $b ] );

    $graph->set(
            dclrs => [ qw/ colourname / ]
    );
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jessi_vsAuthor Commented:
I have initialized $r,$g,$b. Inside loop I have changed the values and

add_colour( colourname => [ $r, $g, $b ] );

I will get all lines in black color only. Thanks. VS
0
 
kanduraCommented:
Here's a complete example, which works fine for me.
I add two new colors called 'geel' and 'blauw' (which is dutch for yellow and blue).
Then I create a bar chart which I copied straight from the GD manual.

If you run this, you should find a yellow-blue bar chart in jessi_vs.png.


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

    # create custom colors
    use GD::Graph::colour qw/:colours/;
    add_colour( geel  => [ 255, 255, 0 ] );
    add_colour( blauw => [ 0, 0, 255 ] );
   
    my @data = (
        ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
        [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
        [ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ]
      );
     
    my $graph = GD::Graph::bars->new(400, 300);
   
    $graph->set(
          x_label           => 'X Label',
          y_label           => 'Y label',
          title             => 'Some simple graph',
          y_max_value       => 8,
          y_tick_number     => 8,
          y_label_skip      => 2,
          dclrs             => [ qw/ geel blauw / ]         # set custom colors in this graph
      ) or die $graph->error;
     
    my $gd = $graph->plot(\@data) or die $graph->error;
   
    open(IMG, '>jessi_vs.png') or die $!;
    binmode IMG;
    print IMG $gd->png;
   
0
 
jessi_vsAuthor Commented:
fine, but in my case the number of lines are dynamic. if it is less than or equal to 30 then no problem I can use standard colour, but my graphs are having more than 30 lines that is why i need to avoid dublicate colours.yours is sound good, but how to I give colour_name? Thanks.
0
 
kanduraCommented:
Simply add the new colors you make to the array of defaults:

    my @colors = qw(blue yellow green red orange dpink purple white gray marine gold dgreen lblue dyellow lred dpurple pink dbrown dgray dblue lyellow lgreen lpurple lorange lgray lbrown dred black cyan geel blauw)

and set that array in your graphs:

    $graph->set(
        ...
        dclrs => \@colors
    );


You can create the colors array at the start of your program, and use it throughout.
0
 
jessi_vsAuthor Commented:
so you suggest me create/define 10 or more colour_name and add with standard colour array. Right?
0
 
kanduraCommented:
yup.

Alternatively, you can define the colors in a file, in the following format:

    ! $Xorg: rgb.txt,v 1.3 2000/08/17 19:54:00 cpqbld Exp $
    255 250 250             snow
    248 248 255             GhostWhite
    245 245 245             WhiteSmoke
    220 220 220             gainsboro
   
You can then read it in and get the color names like this:

    use GD::Graph::colour qw/:lists :files/;
    read_rgb('colorfile.txt');

    my @colors = colour_list();

0
 
jessi_vsAuthor Commented:
ok fine, I will try and let you know. Thaks.
0
 
jessi_vsAuthor Commented:
Tell me is it possible or not ?

>> 2. I want to display the lable on the line for example (data1,data2 etc). I am using legends but user needs the info on/besides the line.

I read GD documentation, I could not find solution. It will help me to close this question.
0
 
kanduraCommented:
I couldn't find anything like that in the documentation either, so it looks like you're on your own there.
I know you can put the labels on the x-axis, or in a legend, but it doesn't look like there's a method to put them in the graph itself.
You can do it manually, of course. See the "Character and String drawing" section in the manual for details.

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now