Menu

PlotType = 'Points', SetXDataLabelPos('plotin') ?

2014-03-21
2014-03-24
  • Timothy Edgar

    Timothy Edgar - 2014-03-21

    Is it possible to set XDataLabelPos to 'plotin' in a 'points' graph? I tried using SetYDataLabelPos to 'plotin', but this just labeled the points with their Y values. I want the points labeled with the text name associated with the point in the array.

    ~~~~~~~~~~~~
    // Make a points plot using the above data:
    $plot = new PHPlot($height, $width);
    $plot->SetDataType('data-data');
    $plot->SetDataValues($data);
    $plot->SetPlotType('points');
    $plot->SetTitle('Map');
    $plot->SetPlotAreaWorld(-1 * $worldrow["size"], -1 * $worldrow["size"], $worldrow["size"], #$worldrow["size"]);

    // Move axes and ticks to 0,0, but turn off tick labels:
    $plot->SetXAxisPosition(0); # Is default
    $plot->SetYAxisPosition(0);
    $plot->SetXTickPos('xaxis');
    $plot->SetXTickLabelPos('none');
    $plot->SetYTickPos('yaxis');
    $plot->SetYTickLabelPos('none');
    $plot->SetXDataLabelPos('plotin');
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

     

    Last edit: Timothy Edgar 2014-03-21
  • lbayuk

    lbayuk - 2014-03-21

    Yes it is possible, but it takes a bit of work. Firstly, though, what you want are Y data labels, not X data labels. I know this sounds wrong, but by default these labels display the Y values at the data points, so they are called Y data labels.

    Secondly, you will use a custom label formatting function to make the label text equal to the values from your data array. PHPlot-5.8.0 or higher is required in order to use the $row argument with custom label formatting.

    More information is in the manual here and also see the reference for SetXDataLabelType

    Here is a simple, complete example:

    <?php
    require_once "phplot.php";
    // Y Data Labels above the points, showing the labels from the data array.
    
    $data = array(
        array('Jan', 1, 0),
        array('Feb', 2, 10),
        array('Mar', 3, 20),
        array('Apr', 4, 25),
        array('May', 5, 18),
        array('Jun', 6, 12),
    );
    
    function get_label($ignored_label, $data, $row = NULL, $ignored_column = NULL)
    {
        return $data[$row][0];
    }
    
    $plot = new PHPlot(600, 400);
    $plot->SetDataType('data-data');
    $plot->SetDataValues($data);
    $plot->SetPlotType('points');
    $plot->SetYDataLabelType('custom', 'get_label', $data);
    $plot->SetYDataLabelPos('plotin');
    $plot->SetXTickLabelPos('none');
    $plot->SetXDataLabelPos('none');
    $plot->DrawGraph();
    
     
  • Timothy Edgar

    Timothy Edgar - 2014-03-24

    Awesome. Thank you! Works perfectly.

     

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.