Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
98 - 1
<?php
2
/*
3
   PHPlot / contrib / prune_labels
4
   $Id: prune_labels.php 454 2009-12-09 03:45:55Z lbayuk $
5
   PHPlot contrib code - public domain - no copyright - use as you wish
6
 
7
Reduce the number of data labels along the X axis,  when the density is too
8
high.  This simply blanks out M-1 of every M labels in the data array.
9
There are other ways to do this, but we need to keep the labels uniformly
10
spaced.  You select the target maximum label count (maxlabels), and you will
11
get no more than maxlabels data labels.
12
 
13
  Arguments:
14
     $data  - The PHPlot data array (reference variable)
15
     $maxlabels - The maximum number of data labels you are willing to have,
16
  Returns: Nothing
17
     Modifies the $data array in place to remove some of the labels.
18
 
19
   Notes:
20
     The data array and its rows must be 0-based integer indexed arrays.
21
*/
22
function prune_labels(&$data, $maxlabels)
23
{
24
    # Do nothing if there are not already too many labels:
25
    if (($n = count($data)) <= $maxlabels) return;
26
 
27
    # Compute how many labels to erase. Keep 1 of every $m labels.
28
    $m = (int)ceil($n / $maxlabels);
29
 
30
    # Process the data array, zapping M-1 of every M labels:
31
    $k = 0;
32
    for ($i = 0; $i < $n; $i++) {
33
       if ($k > 0) $data[$i][0] = '';
34
       if (++$k >= $m) $k = 0;
35
    }
36
}