本示例说明了如何使 ChartControl 中的点可以被交互调整。 本示例的关键点包括接管 ChartControl.ObjectHotTracked 事件,以及使用 XYDiagram2D.PointToDiagram 方法把鼠标的物理坐标转换为逻辑坐标。

C#CopyCode image复制代码
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...

static int lastY = -1;
static bool isPressed = false;
static SeriesPoint selectedPoint = null;

private void chartControl1_ObjectHotTracked(object sender, HotTrackEventArgs e) {
    if(e.HitInfo.SeriesPoint != null)
        selectedPoint = e.HitInfo.SeriesPoint;

    if(selectedPoint != null && isPressed) {
        DiagramCoordinates point = 
            ((XYDiagram)(sender as ChartControl).Diagram).PointToDiagram(e.HitInfo.HitPoint);

        if(lastY != -1) {
            AxisRange range = ((XYDiagram)(sender as ChartControl).Diagram).AxisY.Range;
            double delta = ((double)range.MaxValue - (double)range.MinValue) / 8;

            if(selectedPoint.Values[0] >= (double)range.MaxValue - delta)
                range.MaxValue = selectedPoint.Values[0] + delta;

            selectedPoint.Values[0] = point.NumericalValue;

            if(point.QualitativeArgument != "")
                selectedPoint.Argument = point.QualitativeArgument;
        }

        ((ChartControl)sender).RefreshData();
        lastY = e.HitInfo.HitPoint.Y;
        return;
    }

    lastY = -1;
}

private void chartControl1_MouseDown(object sender, MouseEventArgs e) {
    isPressed = true;
}

private void chartControl1_MouseUp(object sender, MouseEventArgs e) {
    isPressed = false;
}

private void chartControl1_MouseLeave(object sender, EventArgs e) {
    isPressed = false;
}
Visual BasicCopyCode image复制代码
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Private Shared lastY As Integer = -1
Private Shared isPressed As Boolean = False
Private Shared selectedPoint As SeriesPoint = Nothing

Private Sub chartControl1_ObjectHotTracked(ByVal sender As Object, _ 
ByVal e As HotTrackEventArgs) Handles chartControl1.ObjectHotTracked
    If e.HitInfo.SeriesPoint IsNot Nothing Then
        selectedPoint = e.HitInfo.SeriesPoint
    End If

    If selectedPoint IsNot Nothing AndAlso isPressed Then
        Dim point As DiagramCoordinates = _ 
            (CType((TryCast(sender, ChartControl)).Diagram, _ 
                XYDiagram)).PointToDiagram(e.HitInfo.HitPoint)

        If lastY <> -1 Then
            Dim range As AxisRange = _ 
                (CType((TryCast(sender, ChartControl)).Diagram, XYDiagram)).AxisY.Range
            Dim delta As Double = (CDbl(range.MaxValue) - CDbl(range.MinValue)) / 8

            If selectedPoint.Values(0) >= CDbl(range.MaxValue) - delta Then
                range.MaxValue = selectedPoint.Values(0) + delta
            End If

            selectedPoint.Values(0) = point.NumericalValue

            If point.QualitativeArgument <> "" Then
                selectedPoint.Argument = point.QualitativeArgument
            End If
        End If

        CType(sender, ChartControl).RefreshData()
        lastY = e.HitInfo.HitPoint.Y
        Return
    End If

    lastY = -1
End Sub

Private Sub chartControl1_MouseDown(ByVal sender As Object, _ 
ByVal e As MouseEventArgs) Handles chartControl1.MouseDown
    isPressed = True
End Sub

Private Sub chartControl1_MouseUp(ByVal sender As Object, _ 
ByVal e As MouseEventArgs) Handles chartControl1.MouseUp
    isPressed = False
End Sub

Private Sub chartControl1_MouseLeave(ByVal sender As Object, _ 
ByVal e As MouseEventArgs) Handles chartControl1.MouseLeave
    isPressed = False
End Sub

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E294。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

Expand image参阅