Page 1 of 1
Add custom text describing the Average tool.
Posted: Mon Oct 06, 2008 9:55 am
by 13050248
I'm using the Average tool on a series.
I'm trying to add a label on the left axis at the average value. For now I precalculated the average myself to generate my custom text.
I don't really want to create custom labels. I'm happy with the labels chosen by TeeChart.
I'm just trying to overlay my text over the axis labels. I'd prefer it if the text stayed put if I zoomed in on the bottom axis, but moved if I zoomed in on the left axis.
Which tool would work the best in this situation?
It would be nice if I could just attach my custom text to the Average tool.
Posted: Mon Oct 06, 2008 10:34 am
by narcis
Hi Christo,
One option is using Annotation tool, for example:
Code: Select all
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
Steema.TeeChart.Styles.Points points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
points1.FillSampleValues();
Steema.TeeChart.Functions.Average avg1 = new Steema.TeeChart.Functions.Average();
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.Function = avg1;
line1.DataSource = points1;
tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(tChart1_AfterDraw);
Bitmap bmp = tChart1.Bitmap;
}
void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
tChart1.Tools.Clear();
Steema.TeeChart.Tools.Annotation annotation1 = new Steema.TeeChart.Tools.Annotation(tChart1.Chart);
double average = tChart1[1].YValues[0];
annotation1.Text = "Average: " + average.ToString();
int tmp = Convert.ToInt32(g.TextWidth(annotation1.Shape.Font, annotation1.Text));
tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
tChart1.Panel.MarginLeft = tmp + 10;
annotation1.Shape.CustomPosition = true;
annotation1.Shape.Left = 10;
annotation1.Shape.Top = tChart1.Axes.Left.CalcPosValue(average);
}
The other option is using custom text drawing:
Code: Select all
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
Steema.TeeChart.Styles.Points points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
points1.FillSampleValues();
Steema.TeeChart.Functions.Average avg1 = new Steema.TeeChart.Functions.Average();
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.Function = avg1;
line1.DataSource = points1;
tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(tChart1_AfterDraw);
Bitmap bmp = tChart1.Bitmap;
}
void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
double average = tChart1[1].YValues[0];
String myText = "Average: " + average.ToString();
int tmp = Convert.ToInt32(g.TextWidth(g.Font, myText));
tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
tChart1.Panel.MarginLeft = tmp + 10;
g.TextOut(10, tChart1.Axes.Left.CalcPosValue(average), myText);
}
Posted: Mon Oct 06, 2008 12:59 pm
by 13050248
Thanx. That helps.
I've opted for the second option.
I was wondering whether it might be a good idea to make my own custom tool.
Is there an example of how to create custom tools? I couldn't find anything.
Posted: Mon Oct 06, 2008 1:16 pm
by narcis
Hi Christo,
Not that I can think of but it would be very similar to implementing custom series:
http://www.teechart.net/support/viewtopic.php?t=4309
http://www.teechart.net/support/viewtopic.php?t=4110
http://www.teechart.net/support/viewtopic.php?t=6590
It may also help checking how existing tools are implemented using
.NET Reflector with TeeChart.dll.
Posted: Tue Oct 07, 2008 9:54 am
by 13050248
Thanks, .NET Reflector is helping a lot to learn TeeChart.
I've managed to implement a custom label which sits on top of the left axis and shows text. I had to use a TextShape to draw the actual text. I was missing something, because when I had two markers, the second one was always drawn black.
I'm now trying to add a similar label, but on the bottom axis. This label needs to be rotated 90 degrees. Unfortunately I couldn't find component in TeeChart which will draw a single label rotated by some angle. Now I'm implementing my own.
Is there maybe such a component I could use?
Thanks for the great support!
Posted: Tue Oct 07, 2008 10:12 am
by narcis
Hi Christo,
You could use TeeChart canvas' RotateLabel method for that:
Code: Select all
tChart1.Graphics3D.RotateLabel(x, y, Text, angle);
Or in the AfterDraw event:
Code: Select all
g.RotateLabel(x, y, Text, angle);
Hope this helps!