Menu

Left-justify data labels in horizontal stacked bar chart

ou_ryperd
2014-04-15
2014-04-17
  • ou_ryperd

    ou_ryperd - 2014-04-15

    Hi,
    I have a horizontal stacked bar chart, with various string lengths in the labels. The labels are right-aligned/justified, and I would like to right-align/justify them. I couldn't find any options in the documentation, apart from $plot->SetYDataLabelType('printf', '[%-100s]'); which doesn't work correctly. Any ideas ?

    Thanks in advance.

     
  • lbayuk

    lbayuk - 2014-04-15

    The labels are right-aligned/justified, and I would like to right-align/justify them

    I'm sure that isn't what you wanted to say...

    Also, which labels? There are Y axis data labels (left of the Y axis), and also X data value labels which are inside and at the end of each bar.

    Regardless, the text alignment of both these label types in PHPlot is currently fixed (not adjustable), to align the text in the direction which I thought made the most sense. If it is doing it wrong, I would like to see an example. Or explain why it should be configurable.

    Your SetYDataLabelType() does work as it should: it formats each Y axis data label in a field of 100 spaces, left aligned, inside the brackets. PHPlot then right-aligns those fields with the Y axis line. Since they are the same length now, the text is effectively left-aligned, but it is way over to the left of the axis.

     
  • ou_ryperd

    ou_ryperd - 2014-04-16

    Apologies, they are right justified, against the Y axis; I would like them left justified, against the left margin of the image. My image here:
    https://picasaweb.google.com/ouryperd/New#6002784275610202050

    At the moment all that I can see in the docs is using a custom callback (as described here http://phplot.sourceforge.net/phplotdocs/conc-labels.html#conc-labels-format-custom2) that uses php's printf with "%-40s", but the problem with that is:
    1) I have to first determine the length of the longest string, and
    2) I have no idea how to do this.

    My code currently looks like this:

    $plot = new PHPlot(1024, 768);
    $plot->SetDefaultTTFont($font);
    $plot->SetFontTTF('title', $font, 14);
    $plot->SetFontTTF('legend', $font, 8);
    $plot->SetFontTTF('generic', $font, 8);
    $plot->SetFontTTF('x_label', $font, 8);
    $plot->SetFontTTF('y_label', $font, 8);
    $plot->SetFontTTF('x_title', $font, 10);
    $plot->SetFontTTF('y_title', $font, 8);
    $plot->SetImageBorderType('plain');
    $plot->SetTitle("\nTest results per category\n");
    $plot->SetLegendPixels(5, 5);
    $plot->SetDataValues($data);
    $plot->SetDataType('text-data-yx');
    $plot->SetPlotType('stackedbars');
    $plot->SetShading(0);
    $plot->SetDataColors(array('green', 'red'));
    $plot->SetLegend(array('Passed', 'Failed'));
    $plot->SetXTitle('Number of tests');
    $plot->SetXDataLabelPos('plotstack');
    $plot->SetXDataLabelAngle(0);
    $plot->SetXDataLabelType('data', 0);
    $plot->SetXTickIncrement(20);
    $plot->SetYTickPos('none');
    $plot->DrawGraph();
    

    By the way, this is a fantastic library. I have in the last weeks tried libchart, phpgraphlib and pchart, but phplot is by far the best for my purposes.

     

    Last edit: ou_ryperd 2014-04-16
  • lbayuk

    lbayuk - 2014-04-16

    I think it (the image you linked to) looks quite good the way it is, given the long labels. But it's your chart.

    PHPlot can't currently do left-alignment of the Y axis data labels. It isn't just a matter of changing the text alignment - the reference point would have to move also, and it isn't a simple change.

    You can see what left-aligned text would look like using the code you posted above, which I changed slightly here:

    $plot->SetYDataLabelType('printf', "%-{$longest}s=>");
    

    here $longest is the string length of the longest label (calculated from the data array, perhaps), and "=>" is just an arrow of sorts at the end. If you leave that off, PHPlot will trim spaces from the end, which defeats the purpose.

    If using TrueType fonts, as you are, you must use a monospace font for this to work (something like Courier or LiberationMono). Using a proportional spaced font would be much more involved - using the calculated printing width of each label, rather than the string length. PHPlot does this sort of thing internally, but it doesn't give you enough control on label positioning to use it.

    With the monospace font, it does work for me, but I like the right-aligned one better...

    Another idea: Have you considered breaking the labels into multiple lines, by inserting 1 or 2 newlines? I'm not sure the data lends itself to that, but you have enough vertical space for 2 or 3 lines per bar. PHPlot will correctly handle multi-line labels, right-aligning each line.

     
  • ou_ryperd

    ou_ryperd - 2014-04-17

    Thanks for your feedback. I also like it the way it is. A manager asked me if we could do it the other way to see if it'll look better. I will drop this now. It is completely good enough.
    Thanks again.

     

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.