【TeeChart .NET教程】(十一)滚动和缩放
【下载TeeChart.Net最新版本】
(一)如何使用鼠标进行缩放和滚动
1.1 缩放
要放大图表,请在要放大的区域的左上角按住鼠标左键并按住鼠标按钮,将矩形拖动到缩放区域的右下角。释放鼠标按钮,图表将重绘所选区域。要撤消缩放,请在“Chart图表”区域的任意位置按鼠标左键,然后按住鼠标按钮向上和向左拖动,释放按钮,图表将重绘为最初定义的图表区域。
1.2 滚动
要滚动图表,按右鼠标按钮并按住鼠标按钮,将鼠标拖动到您想要滚动图表的方向。释放鼠标按钮时,图表将保留在新位置。要撤消滚动,请在“Chart图表”区域的任意位置按鼠标左键,然后按住鼠标按钮向上和向左拖动。释放按钮,图表将重绘为最初定义的图表区域。
(二)如何通过代码缩放和滚动
2.1 缩放
默认情况下启用缩放,使用Zoom.Allow属性禁用缩放。有关与缩放关联的属性和方法的完整列表,请参见缩放类。要定义缩放的矩形区域,请使用ZoomRect方法。 示例
[C#.Net]
tChart1.Zoom.ZoomRect(new Rectangle(100,100,120,120));
[VB.Net]
TChart1.Zoom.ZoomRect(New Rectangle(100, 100, 120, 120))
ZoomRect坐标以屏幕像素定义,其中0,0是图表面板的左上角。以下代码将放大第2和第5个x轴点之间的区域,将y轴设置为整个图表的最大和最小点的比例:
[C#.Net]
int x = points1.CalcXPos(2); int y = tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MaxYValue); int height = tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MinYValue) - tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MaxYValue); int width = points1.CalcXPos(5) - x; Rectangle r = new Rectangle(x,y,width,height); tChart1.Zoom.ZoomRect(r);
[VB.Net]
Dim X As Integer = Points1.CalcXPos(2) Dim Y As Integer = TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MaxYValue) Dim Height As Integer = TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MinYValue) - TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MaxYValue) Dim Width As Integer = Points1.CalcXPos(5) - X Dim R As New Rectangle(X, Y, Width, Height) TChart1.Zoom.ZoomRect(R) TChart1.Zoom.Undo
2.2 动画缩放
动画缩放提供步进缩放。您可以将Animated设置为启用并为缩放定义交错步骤,而不是一步跳过“缩小”到“放大”。启用动画后,您可以使用鼠标或代码手动缩放。 例
[C#.Net]
int x = points1.CalcXPos(2); int y = tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MaxYValue); int height = tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MinYValue) - tChart1.Axes.Left.CalcYPosValue(tChart1.Axes.Left.MaxYValue); int width = points1.CalcXPos(5) - x; Rectangle r = new Rectangle(x,y,width,height); tChart1.Zoom.Animated = true; tChart1.Zoom.AnimatedSteps = 100; tChart1.Zoom.ZoomRect(r);
[VB.Net]
Dim X As Integer = Points1.CalcXPos(2) Dim Y As Integer = TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MaxYValue) Dim Height As Integer = TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MinYValue) - TChart1.Axes.Left.CalcYPosValue(TChart1.Axes.Left.MaxYValue) Dim Width As Integer = Points1.CalcXPos(5) - X Dim R As New Rectangle(X, Y, Width, Height) TChart1.Zoom.Animated = True TChart1.Zoom.AnimatedSteps = 100 TChart1.Zoom.ZoomRect(R)
2.3 缩放事件
手动缩放或通过代码缩放将触发TChart.Zoomed事件,默认情况下,缩小将触发TChart.UndoneZoom事件。
2.4 滚动
滚动在所有方向上启用,使用Scroll.Allow属性禁用Scroll或将Scroll限制为一个方向,按代码滚动的最简单方法是使用Axis Scroll方法:
[C#.Net]
tChart1.Axes.Bottom.Scroll(3, false);
[VB.Net]
TChart1.Axes.Bottom.Scroll(3, False)
该值是偏移量,'False'指的是TeeChart是否允许滚动超出系列值限制。控制滚动的另一种方法是定义Axis maximum和minumum以按代码滚动:
[C#.Net]
private void Form1_Load(object sender, System.EventArgs e) { int range = Convert.ToInt32(bar1.XValues.Maximum - bar1.XValues.Minimum / 2); bar1.FillSampleValues(20); tChart1.Panning.Allow = ScrollModes.None; hScrollBar1.Value = range; hScrollBar1.Minimum = range - 50; hScrollBar1.Maximum = range + 50; } private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) { tChart1.Axes.Bottom.Automatic = false; tChart1.Axes.Bottom.Minimum = e.NewValue; tChart1.Axes.Bottom.Maximum = e.NewValue + bar1.Count; }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Range As Integer = Bar1.XValues.Maximum - Bar1.XValues.Minimum / 2 Bar1.FillSampleValues(20) TChart1.Panning.Allow = Steema.TeeChart.ScrollModes.None HScrollBar1.Value = Range HScrollBar1.Minimum = Range - 50 HScrollBar1.Maximum = Range + 50 End Sub Private Sub HScrollBar1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll TChart1.Axes.Bottom.Automatic = False TChart1.Axes.Bottom.Minimum = e.NewValue TChart1.Axes.Bottom.Maximum = e.NewValue + Bar1.Count End Sub