Positioning datalabels
Hello again Tom !
but the OP said: " I know the height and width of the label"
So he did, I wonder how
One way I had in mind was placing the text into an autosize textbox with
same font and with no internal margins, generally works well.
Another way would be to position labels left & right, top & bottom, trapping
their left & top coord's in each position.
eg, .Position = xlLabelPositionLeft
Would learn just about all you need to know about label size as well as the
Point position. A bit of a kludge though.
Regards,
Peter T
"Tom Ogilvy" wrote in message
...
for some reason data labels don't seem to expose there
width & height properties,
That was my thinking/experience,
but the OP said: " I know the height and width of the label"
--
Regards,
Tom Ogilvy
"Peter T" wrote:
If for example I want to place a particular label exactly over a
point,
I know that's not your real question but if that was, simply
ActiveChart.SeriesCollection(1).DataLabels.Positio n =
xlLabelPositionCenter
But if I follow, your real question is how to get the XY coordinates of
your
XY points. Have a go with the following
Create an XY embedded chart with one series, keep it simple let each X:Y
make a straight line starting with X:Y = 0:0, 1:1 etc
Option Explicit
Sub test()
Dim i As Long
Dim cht As Chart
Dim sr As Series
Dim aX As Axis, aY As Axis
Dim x0 As Single, y0 As Single, xP As Single, yP As Single
Dim xf As Double, yf As Double
Dim arrX, arrY
Set cht = ActiveSheet.ChartObjects(1).Chart
Set aX = cht.Axes(1)
Set aY = cht.Axes(2)
x0 = aY.Left + aY.Width
y0 = aX.Top + aX.Height
yf = y0 - aY.Top
xf = (aX.Left + aX.Width - x0)
xf = (aX.Left + aX.Width - x0) / (aX.MaximumScale)
yf = (y0 - aY.Top) / aY.MaximumScale
Set sr = cht.SeriesCollection(1)
With sr
arrX = .XValues
arrY = .Values
For i = 1 To .Points.Count
xP = x0 + arrX(i) * xf
yP = y0 - arrY(i) * yf
With .DataLabels(i)
.Left = xP
.Top = yP
End With
Next
End With
End Sub
I know yesterday you said you are working with an MS Graph, I think it
should work pretty much the same way but try the above in Excel. First
with
a straight line, then your "difficult" data.
The idea is to create your own co-ord system centred in the intersection
of
the XY axes, then scale X & Y according to the length to right and above
the
intersection. Where a chart does not have either of those axes would
use
the plot-area instead.
I'll leave the hard part to you, as to how to work out quite where to
place
those labels on a crowded chart. Actually I'd be interested to see!
Almost forgot to say, for some reason data labels don't seem to expose
there
width & height properties, hence in this demo top-left label corners are
placed over points (if it works as intended). I imagine you'll be able
to
guesstimate sizes depending on text & font, otherwise there's a much
more
long winded way to get a good idea of the size.
Regards,
Peter T
"Phil Stanton" wrote in message
et...
I have a scatter chart and I want to be able to position the labels
differently for each point ( depending on the space available on the
chart)
I know the X & Y co-ordinates of the points in the series.
The chart is 622 point wide and the interior width of the plot area is
422
point wide. I know the height and width of the label
If for example I want to place a particular label exactly over a
point,
what
is the calculation using the X co-ordinate, widths to give me the
DataLabel.Left value
Thanks for any help
Phil
|