5.43. Example - Custom Data Value Label Formatting
Prev | Chapter 5. PHPlot Examples | Next

## 5.43. Example - Custom Data Value Label Formatting

This example shows some advanced usage of label formatting. A user-defined function can be used to format labels, using the `custom` label type in SetXLabelType, SetYLabelType, SetXDataLabelType, and SetYDataLabelType. The user-defined function will have access to the row and column of the data point being plotted (for most data value labels), or the column (for axis data labels), and it can use these variables to control the formatting.

This example produces a linepoints plot with multiple data sets and with labels identifying the data values. The goal is to label only the greatest Y value for each X value. Before plotting, the data array is scanned to find the column index of the largest Y value for each row, and this information is stored in an array `$max_indexes`. That array is passed to the custom label formatting function as the pass-through argument. The label formatting function uses the array, along with the row and column of the point being labeled, to determine if a label should be produced or not.

Access to the data point row and column index was added in PHPlot-5.8.0.

Example 5.43. Custom Data Value Label Formatting

    <?php
    # PHPlot Example: Custom label formatting using row and column
    # This example produces a linepoints plot with only the maximum Y value
    # for each X value labeled. This is an example of a custom label
    # formatting with access to the data array position.
    # This requires PHPlot > 5.7.0
    require_once 'phplot.php';

    # Build a data array. The values are psuedo-random integers, but the first
    # and last rows have all Y=0. Return the completed data array.
    function make_data_array($n_rows, $n_y_per_row)
    {
        mt_srand(10); // For repeatable results
        $data[0] = array('', 0) + array_fill(2, $n_y_per_row, 0);
        for ($i = 1; $i < $n_rows - 1; $i++) {
            $row = array('', $i);
            for ($j = 0; $j < $n_y_per_row; $j++) $row[] = mt_rand(0, 999);
            $data[] = $row;
        }
        if ($n_rows > 1)
            $data[] = array('', $n_rows - 1) + array_fill(2, $n_y_per_row, 0);
        return $data;
    }

    # Find the index of the largest Y for each X in the data array.
    # Build an array $max_indexes such that $max_indexes[$row]=$column means
    # the $column-th Y value is the largest in row $row (where $row and $column
    # are zero-based). However, if max Y<=0 in the row, mark this with $column=-1
    # to prevent labeling. Returns the array $max_indexes.
    function find_max_indexes($data)
    {
        $max_indexes = array();
        foreach ($data as $row) {
            $max_index = 0;
            $max_y = $row[2]; // Skip label and X value
            // Process remaining values in the row:
            for ($j = 3; $j < count($row); $j++) {
                if ($row[$j] > $max_y) {
                    $max_y = $row[$j];
                    $max_index = $j - 2; // Offset by 2 for label and X value
                }
            }
            if ($max_y <= 0) $max_index = -1; // Will suppress the label
            $max_indexes[] = $max_index;
        }
        return $max_indexes;
    }

    # Custom label formatting function: Return an empty string, unless this is
    # the largest value in the row.
    function fmt_label($value, $maxes, $row, $column)
    {
        if ($maxes[$row] == $column) return $value;
        return "";
    }

    # Get the data array with 11 rows, 6 values per row:
    $data = make_data_array(11, 6);
    # Process the data array to find the largest Y per row:
    $max_indexes = find_max_indexes($data);

    # Now plot the data:
    $plot = new PHPlot(800, 600);
    $plot->SetImageBorderType('plain'); // For presentation in the manual
    $plot->SetPlotType('linepoints');
    $plot->SetDataType('data-data');
    $plot->SetDataValues($data);
    $plot->SetTitle('Linepoints plot with only max Y values labeled');
    $plot->SetYDataLabelPos('plotin');
    $plot->SetYDataLabelType('custom', 'fmt_label', $max_indexes);
    $plot->SetLineStyles('solid');
    $plot->SetYTickIncrement(100);
    $plot->DrawGraph();

Prev | Up | Next
5.42. Example - DrawMessage | Home | 5.44. Example - Image Map from Bar Chart