Menu

My solution for over-popilated X labels

2010-08-24
2012-09-07
  • Jon Allport

    Jon Allport - 2010-08-24

    I, like many others, have been suffering with too many labels on the X-axis
    (especially for date/time based data).

    Feel free to comment on the workaround below which suits my needs. The data
    are date/time against values and depending on the 'depth' (i.e. number of
    hours back) of data I've elected to only pull a selection of the date/time
    values into the $data array, turning the rest into spaces.

    My criteria are:

    1 hour = labels each minute

    1-6 hours = labels each 10th minute

    6-24 hours = labels each hour

    24 hours = labels each 6th hour

    So far that has worked for the graph I want to produce, typically 24-72 hours
    wide, drilling down to 1 hour sometimes.

    while ($myrow = mysql_fetch_array($result)) {
            if ($depth == 1) {
                    $DT = $myrow[0];
            }
            else if ($depth >1 && $depth <= 6) {
                    if (substr($myrow[0],15,1)=='0') {
                            $DT = $myrow[0];
                    }
                    else {
                            $DT = ' ';
                    }
            }
            else if ($depth > 6 && $depth <=24) {
                    if (substr($myrow[0],14,2)=='00') {
                            $DT = $myrow[0];
                    }
                    else {
                            $DT = ' ';
                    }
            }
            else {
                    if (is_integer(substr($myrow[0],11,2)/6) && substr($myrow[0],14,2)=='00') {
                            $DT = $myrow[0];
                    }
                    else {
                            $DT = ' ';
                    }
            };
            $data[] = array($DT, $myrow[1], $myrow[2], $myrow[3], $myrow[4], $myrow[5], $myrow[6], $myrow[7], $myrow[8], $myrow[9], $myrow[10], $myrow[11]);
    };
    

    DT field is in the form "YYYY/MM/DD hh:mm:ss"

    Hope that's of use to someone.

     
  • lbayuk

    lbayuk - 2010-08-24

    Thanks for posting that.

    FYI, there is a short script in the PHPlot "contrib" directory called
    "prune_labels" which does something like this. However, it is simplistic, and
    probably would not work well with date/time labels. Given the maximum number
    of labels you want along the X axis (M), it zaps (M-1) out of every M from
    your data array.

    Depending on how your database records store the timestamps (assuming they are
    not pre-formatted into the database as strings), another approach would be to
    make those the X values (not labels) in integer form. I think you do that in
    MySQL using the UNIX_TIMESTAMP function. Then you can let PHPlot format them
    as date/times, and your X tick increment determines the label density.

     

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.