Menu

Display only some data value labels

lbayuk
2012-02-09
2012-09-07
  • lbayuk

    lbayuk - 2012-02-09

    (Posted 2012-02-09 04:55:09 PST by robertmgibson on another thread, and moved
    to a new thread by me. -admin)

    New to this forum so not sure how to initiate a question, also new to PHPlot
    but making good progress. What I am trying to do is to present Y data labels
    on top of my bars in a bar chart, BUT only for the latest of the three bars in
    each series, leaving the first two clear. At the moment I can only have all 3
    labels, using $plot->SetYDataLabelPos('plotin'); or none. Does anyone know of
    a way, please. Very grateful of any help.

     
  • lbayuk

    lbayuk - 2012-02-09

    Unfortunately, there is currently no way to do what you want in PHPlot. The
    data value labels above the bars are either on or off. Nor is there any custom
    formatting available for this type of labels (I wasn't sure and I had to check
    - maybe this should be fixed). Even if custom formatting worked for these,
    PHPlot only gives you the value, not the bar number (row or column index) so
    there would be no way to blank out the labels you did not want.

    Speculatively, I think implementing this would require: (1) the ability to use
    a custom formatting function with data value labels, and (2) a custom
    formatting function having access to row and column indexes, so it can produce
    different results depending on which data value is being formatted. While I
    think these are feasible, I'm not sure how useful (2) would be, mainly because
    I can't see why someone would want to label different bars differently, or
    some bars only. Can you elaborate, convince me why this would be a useful
    thing for PHPlot to be able to do?

     
  • lbayuk

    lbayuk - 2012-02-11

    Nor is there any custom formatting available for this type of label (I
    wasn't sure and I had to check - maybe this should be fixed)

    Oops, my mistake. I don't know how I 'checked' and missed that. Of course
    custom formatting is available for data value labels, using
    SetYDataLabelType().

    Now the problem is that the custom formatting function only has access to the
    value (Y) to be formatted, not the current point's row and column. For
    example, I can have it label any bar with an odd number value, and omit the
    label for any bar with an even number value. (No, I don't know why that would
    be useful either.) But I can't have it label only the last 3 bars.

    I will look into that - maybe we can get the row and column included, without
    breaking anything.

     
  • Robert

    Robert - 2012-02-15

    Hi, thanks for taking an interest in my question, and for your prompt
    responses.

    You asked why I might want to create labels only for the last bar. Actually,
    the reason I wanted to, was that I was trying to replicate a graph in a
    presentation that someone gave me as a project. This gave portfolio weightings
    for the past three weeks, for several classes of investment, and whilst he
    showed the bars for the three time periods, to show the trend, he only wanted
    the actual number for the most recent figure. I replicated the graph as
    follows:

    require_once 'phplot.php';

    $data = array(

    array('SPX', 10.2, 12.7, 15.0), array('EMX', 0.2, 0.4, 0.6), array('REIT',
    5.9, 8.8, 13.3),

    array('CORP', 5.4, 8.9, 9.2), array('BOND', 97.9, 93.8, 85.9), array('Debt',
    -19.6, -24.6, -24.0),

    );

    $plot = new PHPlot(600, 450);

    $plot->SetImageBorderType('plain');

    $plot->SetPlotType('bars');

    $plot->SetDataType('text-data');

    $plot->SetDataValues($data);

    $plot->SetDefaultTTFont('C:\Windows\Fonts\arialbd.ttf');

    Main plot title:

    $plot->SetTitle("\nRecommended portfolio weights");

    $plot->setFontTTF('title','C:\Windows\Fonts\arialbd.ttf','14');

    $plot->SetLegend(array('Previous', 'Last week', 'Current'));

    $plot->SetLegendPosition(0, 0, 'image', 0, 0, 5, 5);

    $plot->SetXTickLabelPos('none');

    $plot->SetXTickPos('none');

    $plot->setFontTTF('x_label','C:\Windows\Fonts\arialbd.ttf','12');

    $plot->SetYDataLabelPos('plotin');

    $plot->SetPrecisionY(1);

    $plot->SetYDataLabelAngle(90);

    $plot->SetPlotAreaPixels(70, 100, 550, 400); //sets area of plot within image,
    in pixels

    $plot->SetDataColors(array('navy','SlateBlue','gray'));

    $plot->SetYLabelType('data', 0, '', '%');

    $plot->SetYDataLabelType('data', 1,'', '%');

    $plot->setFontTTF('y_label','C:\Windows\Fonts\arialbd.ttf','10');

    $plot->SetPlotAreaWorld(NULL, -50, NULL, 100);

    $plot->SetYTickIncrement(25);

    $plot->DrawGraph();

    It worked well, with the exception of not being able to show just one label
    above the bars. i suppose, if you replicate the graph, that you might agree
    that it looks a bit cluttered. That would be the only real objection to it...

    I have been pleased with my first forays into PHPlot, and expect to do more.

    Best wishes.

     
  • lbayuk

    lbayuk - 2012-02-15

    (Sorry if anyone saw that previous message - I somehow got it connected to the
    wrong thread)

     
  • lbayuk

    lbayuk - 2012-02-16

    OK, thanks for the example, makes perfect sense now why only one bar will be
    labelled. I am opening a new feature request linked to this thread
    for this feature, so it doesn't get lost.

    I did already look into a way to do this, but the impact was a little too
    high, so it needs more thought. Also, a few other changes are taking priority,
    and I have to sort through them and see what goes into the next release.

     
  • Robert

    Robert - 2012-02-24

    Thank you again for your interest and prompt actions

     
  • lbayuk

    lbayuk - 2012-02-24

    So now I have 3 ideas on implementing this, but I'm not happy about any of
    them or I would move ahead with it. Perhaps you could offer some feedback or
    suggestions?

    1. Let custom label formatting functions have access to the row and column of the current point. This means you could write a formatting function which filters out or alters labels based on which bar in a group is being plotted, for example. This provides a lot of flexibility, but the cost is high (in terms of changes, impact on the PHPlot code).

    2. Add a new callback to alter labels before formatting. The callback would have access to the current row and column. It could return an empty string to skip a label, or even change the label, based on the position. This has less impact on the PHPlot code, as the changes are much more localized. For example, I could add this to one plot type and/or label type first (such as data value labels for bar charts) and see how it goes.

    3. A simpler variation on (2), the callback would return a true/false flag to indicate skipping a label. This is easier to implement, and avoids some confusion about having 2 separate ways to format a label (the callback and label type 'custom'). But it is the least flexible approach, as it only allows for skipping or including labels, not changing them, based on position.

    Any one of these would do what you want, but the goal is to try to generalize,
    and add features that are more widely applicable.

     
  • lbayuk

    lbayuk - 2012-03-25

    This will be implemented in the next release. More here

     

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.