in reply to chart::clicker x-axis text instead of numbers ?

Hi srk2992, Welcome to the monastery.

Chart::Clicker is a powerful and somewhat complex platform for charting and graphing. It's impossible to diagnose your problem properly without seeing your code, so you should please read up on How do I post a question effectively and post a small script that demonstrates your problem.

I can tell you that my implementations have involved a lot of trial and error testing, but here are a few pointers in response to your questions.

Yes, you can use strings for axis labels. You can subclass Chart::Clicker::Axis, or if you want to use a date/time as the tick label, you might be interested in Chart::Clicker::Axis::DateTime.

For spacing, you may have to get creative. I usually take the approach of manipulating a list of values first and then passing it to the Axis class in tick_values.

E.g., you could see what is the maximum number of values per day in your data set, and calculate spacing based on the product of that number times the number of days. Then add a label to your list only for each day, or 1st of the month, or whatever looks good in your chart, then pass the list. You do *not* have to allow Chart::Clicker to make all the decisions.

Sometimes you may even want to insert dummy values in the data set, e.g. repeat the same value for the "missing" daily results when you don't have the full set.

As far as extreme data points -- same approach: push/unshift an empty value onto each end of the list of datapoints, to extend the background of the chart and give your lowest/highest labels room to breathe.

Without knowing your exact circumstances, the only other general advice I can give is to study *all* the examples in the distro's example/ directory.

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: chart::clicker x-axis text instead of numbers ?
by srk2992 (Initiate) on Dec 16, 2016 at 19:16 UTC
    Thanks for the reply.

    Before doing any manipulation I wanted to check if I can avoid it. I would like non-intelligent behavior as of MS Excel where I can just treat the x-axis as text axis and can write any thing on it.

    My code is roughly as below. The values in the keys are date that I pick up from some other scripts.

    use strict; use Chart::Clicker; use Chart::Clicker::Data::Series; use Chart::Clicker::Data::DataSet; my $cc = Chart::Clicker->new; my $series = Chart::Clicker::Data::Series->new( values => [ 1, 2, 3, 4, 5], keys => [ 20161201, 20161201, 20161202, 20161203,20161204 ], ); my $ctx = $cc->get_context('default'); $ctx->domain_axis->format('%s'); my $ds = Chart::Clicker::Data::DataSet->new(series => [ $series ]); $cc->add_to_datasets($ds); $cc->write_output('foo.png');

    I get the key values in the format of yyyymmdd from other script that I don't maintain.

    The key values can be date or it can be git commits hash which can be strings like a4dsfe. If I put these then the script gives me error.

    I even tried by replacing $ctx->domain_axis->format('%s'); in the above code with

    $ctx->domain_axis(Chart::Clicker::Axis->new(position => 'bottom', orie +ntation => 'horizontal',format => '%s'));

    Attribute (keys) does not pass the type constraint because: Validation failed for 'ArrayRefNum' with value ARRAY(0x6c992f8) at cons tructor Chart::Clicker::Data::Series::new (defined at C:/Perl64/site/lib/Chart/Clicker/Data/Series.pm line 112) line 13. Chart::Clicker::Data::Series::new('Chart::Clicker::Data::Series', 'values', 'ARRAY(0x6d4b820)', 'keys', 'ARRAY(0x6c992f8)') called at

    Another question on Chart::Clicker::Axis::DateTime is it needs time in which format? For example if I've values in yyyymmdd format, How do I convert to format that is acceptable?

      To answer your second question first:

      You will need to convert the date/time strings you have into epoch times, and pass them as the tick values, and then tell the program what format you want your labels to have. Here's some code that prints labels on the X-axis only for the first of each month.

      my $cc = Chart::Clicker->new( width => 800, height => 300 ); my ( @k, @v, @x_tick_values ); # assumes data is in a hash keyed by dates as YYYYMMDD for my $datetime ( sort { $a <=> $b } keys %data ) { my ( $y, $m, $d ) = unpack("a4 a2 a2", $datetime); my $epoch = DateTime->new( year => $y, month => $m, day => $d )->e +poch; push @k, $epoch; push @v, $data{ $datetime }; push @x_tick_values, $epoch if $d eq '01'; } my $series = Chart::Clicker::Data::Series->new( name => 'Sales', keys => \@k, values => \@v, ); my $dataset = Chart::Clicker::Data::DataSet->new( series => [ $series +] ); $cc->add_to_datasets( $dataset ); $cc->title->text('Daily Report'); $cc->title->font->size( 20 ); $cc->title->padding->bottom( 5 ); my $context = $cc->get_context('default'); my $renderer = Chart::Clicker::Renderer::Line->new( opacity => .6 ); $renderer->brush->width( 2 ); $context->renderer( $renderer ); $context->domain_axis( Chart::Clicker::Axis::DateTime->new( format => "%Y-%m", # Specify format here. I'm using "% +Y-%m" since I # output only one label per month ticks => scalar keys %data, tick_values => \@x_tick_values, position => 'bottom', tick_label_angle => 0.78539816, # 45 deg in radians orientation => 'vertical', fudge_amount => .05, ) ); $context->range_axis->format( '%d' ); $context->range_axis->fudge_amount( .02 ); my $chart_path = "/path/to/your/chart.png"; $cc->write_output( $chart_path );

      As far as non-numeric axis labels: as I mentioned, you can't, directly. As the documentation for Chart::Clicker::Data::Series states:

      Despite the name (keys and values) it is expected that all keys and values will be numeric. Values is pretty obvious, but it is important that keys also be numeric, as otherwise we'd have no idea how to order the data.

      If you want to use text labels for your domain's see Chart::Clicker::Axis's tick_labels method.

      ... which reads:

      The arrayref of labels to show for ticks on this Axis. This arrayref is consulted for every tick, in order. So placing a string at the zeroeth index will result in it being displayed on the zeroeth tick, etc, etc.

      Hope this helps!


      The way forward always starts with a minimal test.