Page 1 of 1
BeforeDraw and CalcPosValue
Posted: Mon Nov 28, 2011 10:38 pm
by 9641066
Hello,
I use TeeChart.NET 2.0.3309.32489 and want to display an rectangle at a certain position in the background. I use scrolling and zooming.
Code: Select all
Private Sub TChart1_BeforeDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDraw
Dim x As Integer = TChart1.Axes.Bottom.CalcPosValue(0)
Dim w As Integer = TChart1.Axes.Bottom.CalcPosValue(knownWITDH) - x
Dim y As Integer = TChart1.Axes.Left.CalcPosValue(knownHEIGTH)
Dim h As Integer = TChart1.Axes.Left.CalcPosValue(0) - y
g.FillRectangle(Brushes.Tomato, 0, 0, TChart1.Width, TChart1.Height) 'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h) 'fill only the desired rectangle
End Sub
I experience, that the calculation of the screen position is only correct, if the chart was already drawn.
Is the a way to force the calculation to update in the BeforeDraw event?
Or can I calculate the Screenpixel belonging to a certain position using another method?
Thanks
NEWBIE
Re: BeforeDraw and CalcPosValue
Posted: Wed Nov 30, 2011 3:48 pm
by 10050769
Hello Newbie,
I think you use other Event as you allow achieve as you want, so you need know if you use BeforeDraw event, the chart still isn't drawn, for this reason I think you can use BeforeDrawValues as I do in following lines:
Code: Select all
Private Sub Series1_BeforeDrawValues(sender As Object, g As Steema.TeeChart.Drawing.Graphics3D)
Dim x As Integer = tChart1.Axes.Bottom.CalcPosValue(0)
Dim w As Integer = tChart1.Axes.Bottom.CalcPosValue(Series1.XValues.Maximum) - x
Dim y As Integer = tChart1.Axes.Left.CalcPosValue(Series1.YValues.Maximum)
Dim h As Integer = tChart1.Axes.Left.CalcPosValue(0) - y
g.FillRectangle(Brushes.Tomato, 0, 0, tChart1.Width, tChart1.Height)
''Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h)
' 'fill only the desired rectangle
End Sub
Can you tell us, if previous code works as you expected?
I hope will helps.
Thanks,
Re: BeforeDraw and CalcPosValue
Posted: Wed Nov 30, 2011 6:29 pm
by 9641066
Hi Sandra,
thanks for your answer,
I tested your proposal, but it does not do the job.
It will be drawn only if there is a point in the series, and the Rectangles are drawn in front of the chart.
I want to have it in the background, behind the axes and all chart areas. And it should be drawn also on a empty chart.
NewBIE
Re: BeforeDraw and CalcPosValue
Posted: Wed Nov 30, 2011 10:07 pm
by 9641066
Hi there, I have a workaround, but it involves the chart to be drawn twice. I still hope for a better solution
Code: Select all
Dim TChart1_x As Integer, TChart1_y As Integer, TChart1_w As Integer, TChart1_h As Integer
Private Sub TChart1_BeforeDrawSeries(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDrawSeries
Dim x As Integer = TChart1.Axes.Bottom.CalcPosValue(0)
Dim w As Integer = TChart1.Axes.Bottom.CalcPosValue(knownWITDH) - x
Dim y As Integer = TChart1.Axes.Left.CalcPosValue(knownHEIGTH)
Dim h As Integer = TChart1.Axes.Left.CalcPosValue(0) - y
If Not (TChart1_x = x And TChart1_y = y And TChart1_w = w And TChart1_h = h) Then
TChart1.Invalidate() 'if something was changed between the drawing, this will force a redraw
End If
End Sub
Private Sub TChart1_BeforeDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDraw
Dim x As Integer = TChart1.Axes.Bottom.CalcPosValue(0)
Dim w As Integer = TChart1.Axes.Bottom.CalcPosValue(knownWITDH) - x
Dim y As Integer = TChart1.Axes.Left.CalcPosValue(knownHEIGTH)
Dim h As Integer = TChart1.Axes.Left.CalcPosValue(0) - y
TChart1_x = x : TChart1_y = y : TChart1_w = w : TChart1_h = h
g.FillRectangle(Brushes.Tomato, 0, 0, TChart1.Width, TChart1.Height) 'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h) 'fill only the desired rectangle
End Sub
Re: BeforeDraw and CalcPosValue
Posted: Thu Dec 01, 2011 11:14 am
by 10050769
Hello newbie,
I am glad that you can find a solution for your problem
. On the other hand, I have made a simple code to draw your rectangles in the BeferoDrawAxes and I think you can do something as next:
Code: Select all
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tChart1.Aspect.View3D = false;
checkBox1.Checked = false;
tChart1.Walls.Back.Visible = false;
tChart1.Axes.Bottom.Grid.Visible = false;
tChart1.Axes.Left.Grid.Visible = false;
tChart1.Axes.Left.SetMinMax(0, 700);
tChart1.Axes.Bottom.SetMinMax(0, 10);
tChart1.BeforeDrawAxes += new Steema.TeeChart.PaintChartEventHandler(tChart1_BeforeDrawAxes);
}
private Steema.TeeChart.Styles.Bar Series1;
private void InitializeSeries()
{
Series1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
Series1.FillSampleValues(10);
tChart1.Axes.Left.SetMinMax(0, Series1.YValues.Maximum);
tChart1.Axes.Bottom.SetMinMax(0, Series1.XValues.Maximum);
tChart1.Draw();
}
void tChart1_BeforeDrawAxes(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
if (Series1 != null)
{
int x = tChart1.Axes.Bottom.CalcPosValue(0);
int w = tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Maximum) - x;
int y = tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Maximum);
int h = tChart1.Axes.Left.CalcPosValue(0) - y;
g.FillRectangle(Brushes.Tomato, 0, 0, tChart1.Width, tChart1.Height);//'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h);// 'fill only the desired rect
}
else
{
int x = tChart1.Axes.Bottom.CalcPosValue(0);
int w = tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Maximum) - x;
int y = tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Maximum);
int h = tChart1.Axes.Left.CalcPosValue(0) - y;
g.FillRectangle(Brushes.Tomato, 0, 0, tChart1.Width, tChart1.Height);//'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h);// 'fill only the desired rect
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
InitializeSeries();
}
else
{
tChart1.Series.Clear();
tChart1.Axes.Left.SetMinMax(0, 700);
tChart1.Axes.Bottom.SetMinMax(0, 10);
}
}
Can you tell us if previous code works fine with your values? If it doesn't works as you expect, please send us a simple project so we can adapt the solution with your data and try to find a optimal solution for you.
I hope will helps.
Thanks,
Re: BeforeDraw and CalcPosValue
Posted: Thu Dec 01, 2011 11:46 pm
by 9641066
Hello Sandra,
Thanks, BeforeDrawAxes is better. Only the limiting lines of the chart area are overpainted and the legend.
So at the end of BeforeDrawAxes I force the legend to paint again, and I also draw the lines again inside this method.
Newbie
Re: BeforeDraw and CalcPosValue
Posted: Fri Dec 02, 2011 4:06 pm
by 10050769
Hello Newbie,
I suggest you draw the legend, using property CustomPosition of Legend as do in next code:
Code: Select all
public Form1()
{
InitializeComponent();
tChart1.Aspect.View3D = false;
checkBox1.Checked = false;
tChart1.Walls.Back.Visible = false;
tChart1.Axes.Bottom.Grid.Visible = false;
tChart1.Axes.Left.Grid.Visible = false;
tChart1.Axes.Left.SetMinMax(0, 700);
tChart1.Axes.Bottom.SetMinMax(0, 10);
tChart1.Panel.MarginRight = 15;
tChart1.Draw();
tChart1.Legend.CustomPosition = true;
tChart1.Legend.Top = tChart1.Axes.Left.IStartPos;
tChart1.Legend.Left= tChart1.Axes.Bottom.IEndPos+10;
tChart1.BeforeDrawAxes += new Steema.TeeChart.PaintChartEventHandler(tChart1_BeforeDrawAxes);
}
private Steema.TeeChart.Styles.Bar Series1;
private void InitializeSeries()
{
Series1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
Series1.FillSampleValues(10);
tChart1.Axes.Left.SetMinMax(0, Series1.YValues.Maximum);
tChart1.Axes.Bottom.SetMinMax(0, Series1.XValues.Maximum);
tChart1.Draw();
}
void tChart1_BeforeDrawAxes(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
if (Series1 != null)
{
int x = tChart1.Axes.Bottom.CalcPosValue(0);
int w = tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Maximum) - x;
int y = tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Maximum);
int h = tChart1.Axes.Left.CalcPosValue(0) - y;
g.FillRectangle(Brushes.Tomato, 0, 0, tChart1.Width, tChart1.Height);//'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h);// 'fill only the desired rect
}
else
{
int x = tChart1.Axes.Bottom.CalcPosValue(0);
int w = tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Maximum) - x;
int y = tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Maximum);
int h = tChart1.Axes.Left.CalcPosValue(0) - y;
g.FillRectangle(Brushes.Tomato, 0, 0, tChart1.Width, tChart1.Height);//'Fill the entire background
g.FillRectangle(Brushes.White, x, y, w, h);// 'fill only the desired rect
}
}
I hope will helps.
Thanks,