JohnK's Message was very helpful ..
This may be a little late but here is the code I used on my custom labels to control overlap. I've only implemented the horizontal so far but the vertical would be similar.
Code:
private void axis_GetAxisDrawLabel(object sender, GetAxisDrawLabelEventArgs e)
{
//flags indicating overlap with the previous and next labels.
bool overlapPrv = false;
bool overlapNxt = false;
//Get the axis
Steema.TeeChart.Axis axis = (Steema.TeeChart.Axis)sender;
//get the size of the label text
SizeF currentSize = this.graphics.MeasureString(e.Text, axis.Labels.Font.DrawingFont);
Rectangle currentRect = new Rectangle(e.X, e.Y, (int)currentSize.Width, (int)currentSize.Height);
//graphAxisLabels is a local ArrayList of the custom labels.
//this is needed to get the index value of the label in the array.
int index = this.graphAxisLabels.IndexOf(e.Text);
//if first or last label set e.DrawLabel to true and return.
if(index <= 0 || index >= axis.Labels.Items.Count - 1)
{
e.DrawLabel = true;
return;
}
//get the AxisLabelItem we are trying to draw.
Steema.TeeChart.AxisLabelItem currentLabel = axis.Labels.Items[index];
//if we set visible to false before GetAxisDrawLabel then set e.DrawLabel to false;
if(currentLabel.Visible == false)
e.DrawLabel = false;
//Get the previous and next labels.
Steema.TeeChart.AxisLabelItem prev = axis.Labels.Items[index - 1];
Steema.TeeChart.AxisLabelItem next = axis.Labels.Items[index + 1];
//if this axis is horizontal then the concern is the width of the label.
//TODO implementation for the vertical.
if(axis.Horizontal == true)
{
//if previous is visible we need to see if it will overlap with the current.
if(prev.Visible == true)
{
//get the previous labels text size.
SizeF prevSize = this.graphics.MeasureString(prev.Text, this.chartFont.DrawingFont);
//get the previous label rectangle.
Rectangle prvRect = new Rectangle(
axis.CalcXPosValue(prev.Value), e.Y, (int)prevSize.Width, (int)prevSize.Height);
//set the overlapPrv flag by checking IntersectsWith
overlapPrv = currentRect.IntersectsWith(prvRect);
}
//Check next label
if(next.Visible == true)
{
//get next label text size.
SizeF nextSize = this.graphics.MeasureString(next.Text, axis.Labels.Font.DrawingFont);
//get next label rectangle.
Rectangle nxtRect = new Rectangle(
axis.CalcXPosValue(next.Value), e.Y, (int)nextSize.Width, (int)nextSize.Height);
//set overlapNxt flag by checking IntersectsWith
overlapNxt = currentRect.IntersectsWith(nxtRect);
}
//if any overlap or if e.DrawLabel is false set e.DrawLabel to false.
if(overlapPrv || overlapNxt || e.DrawLabel == false)
e.DrawLabel = false;
}
}
There are some changes because of version ..
Code: Select all
//flags indicating overlap with the previous and next labels.
bool overlapPrv = false;
bool overlapNxt = false;
//Get the axis
Steema.TeeChart.Axis axis = (Steema.TeeChart.Axis)sender;
//get the size of the label text
SizeF currentSize = this.Graphics3D.MeasureString(axis.Labels.Font,e.Text);
Rectangle currentRect = new Rectangle(e.X, e.Y, (int)currentSize.Width, (int)currentSize.Height);
//graphAxisLabels is a local ArrayList of the custom labels.
//this is needed to get the index value of the label in the array.
int index = IndexOfLabel(axis,e.Text);//My custom function
//if first or last label set e.DrawLabel to true and return.
if (index <= 0 || index >= axis.Labels.Items.Count - 1)
{
e.DrawLabel = true;
return;
}
//get the AxisLabelItem we are trying to draw.
Steema.TeeChart.AxisLabelItem currentLabel = axis.Labels.Items[index];
//if we set visible to false before GetAxisDrawLabel then set e.DrawLabel to false;
if (currentLabel.Visible == false)
e.DrawLabel = false;
//Get the previous and next labels.
Steema.TeeChart.AxisLabelItem prev = axis.Labels.Items[index - 1];
Steema.TeeChart.AxisLabelItem next = axis.Labels.Items[index + 1];
//if this axis is horizontal then the concern is the width of the label.
//TODO implementation for the vertical.
if (axis.Horizontal == true)
{
//if previous is visible we need to see if it will overlap with the current.
if (prev.Visible == true)
{
//get the previous labels text size.
SizeF prevSize = this.Graphics3D.MeasureString(axis.Labels.Font, prev.Text);
//get the previous label rectangle.
Rectangle prvRect = new Rectangle(
axis.CalcXPosValue(prev.Value), e.Y, (int)prevSize.Width, (int)prevSize.Height);
//set the overlapPrv flag by checking IntersectsWith
overlapPrv = currentRect.IntersectsWith(prvRect);
}
//Check next label
if (next.Visible == true)
{
//get next label text size.
SizeF nextSize = this.Graphics3D.MeasureString(axis.Labels.Font,next.Text);
//get next label rectangle.
Rectangle nxtRect = new Rectangle(
axis.CalcXPosValue(next.Value), e.Y, (int)nextSize.Width, (int)nextSize.Height);
//set overlapNxt flag by checking IntersectsWith
overlapNxt = currentRect.IntersectsWith(nxtRect);
}
//if any overlap or if e.DrawLabel is false set e.DrawLabel to false.
if (overlapPrv || overlapNxt || e.DrawLabel == false)
e.DrawLabel = false;
}
But,still there is a little problem ..
On Axis_GetAxisDrawLabel we couldnt access CustomAxisLabel ..
Is there any possibility to pass CustomAxisLabels (not it's text) to event handler..Because I have diffrent CustomAxisLabels with same Texts!
(1998-Nov-Dec-1999-Feb-........-Nov-Dec)
Or what can we do for this problem ?