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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.
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:
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
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:
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.
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.