Menu

Y-Tick Increment calculation

Badda
2011-10-23
2012-09-07
  • Badda

    Badda - 2011-10-23

    Hi there,

    First of all, THANK YOU for this absolutely great library for generating
    reports. It makes my life so much easier :-))

    However, Im facing one problem right now:

    I believe the standard functionality of calcuating Y-Tick intervals (fixed to
    1/10th of the Y data range) is not optimal. Thats why I would like to
    implement a new functionality that takes into account:

    • Y-ticks should always be anchored on Y=0
    • Y-tick increments should be something that adds up to 10,100, etc (like 2, 2.5, 5, 10, 20, 25, 50, 100, ...)
    • There should not be more Y-ticks (and Tick Lables if applicable) than can fit on the Y-Axis with a reasonable bottom- and/or top-margin. Eg: If I have a Y-Axis length of 100px, a Y-tick-label height of 8px and a tick bottom margin of 4px, no more than 8 ticks should be printed because 8*(8px+4px)=96px, which is just smaller than 100px.

    To be able to accomplish this, I need the following information BEFORE drawing
    the plot:

    • Y-Axis total lenght (in px)
    • Y-Axis total length (in World Coordinates)
    • Y-Tick-Label height using the currently selected font

    with this information, I can calculate the desired Y-Tick Increment.

    Now my question: Is there a way, PHPlot can give me the desitred information?

    Answers are highly appreciated :-))

    Regards, Sebastian

     
  • lbayuk

    lbayuk - 2011-10-24

    Improvements to the automatic tick interval and range calculations have been
    in the works for a long time now. Here are the 2 bug reports on the tracker:
    Improve tick interval calculations and Fix automatic Y range calcula
    tions
    .

    It turns out to be a much harder problem than it seems. Calculating tick
    intervals as K * 10 ^ N, where K is 1, 2, or 5 and N is any integer, is easy
    enough, but there are other complications, such as date/time ticks intervals.
    I still plan to do this, but it keeps getting pushed aside for other changes.

    As for the Y ticks anchored on Y=0, I agree this would have been a better
    default. But the ability to anchor the ticks at an X or Y value was only added
    in PHPlot-5.4.0, so making SetYTickAnchor(0) the default could have changed
    the appearance of many existing plots. I try hard to minimize cases where that
    happens.

    There isn't currently any capability for limiting tick labels based on
    available space. I didn't think this would be a problem (I expected the plot
    would become cluttered and hard to read with too many labels well before
    running out of space). But this is somthing I need to think about.

     
  • Badda

    Badda - 2011-10-24

    Ok, I accomplished what I wanted to do with this code before calling
    DrawGraph:

    $plot->SetYTickAnchor(0);
    $plot->SetCallback("draw_setup", function () use (&$plot) {
        // Y-Tick setup variables
        $k=array(1, 2, 2.5, 5);
        $margin=10; // minimum distance between Y-Tick lables in px
    
        // Calculate Y-Tick Increment
        $mintickinc=($plot->fonts["y_label"]["height"]+$margin)/$plot->yscale;
        $n=ceil(log10($mintickinc)*count($k));
        $inc=$k[$n%count($k)]*pow(10,floor($n/count($k)));
        $plot->SetYTickIncrement($inc);
    });
    
     
  • lbayuk

    lbayuk - 2011-10-24

    OK, but I have to give you the standard caution about using PHPlot class
    internal variables in your script, in this case fonts and yscale. There is a
    real risk that your script will break any time you upgrade PHPlot, because
    class variables often change, without much regard for compatibility.

    (P.S. now I have to go read up on anonymous functions - I haven't seen that
    syntax used before, especially the 'use' clause which is very briefly
    documented, it seems.)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.