下面的示例说明了当使用鼠标拖动 常数线 时,如何使用 XYDiagram2D.PointToDiagram 方法,并相应改变系列点取值。

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

Cursor defCursor;
bool dragging = false;
ChartControl chart;
XYDiagram diagram;
ConstantLine line;

private void Form1_Load(object sender, EventArgs e) {
    this.chart = this.chartControl1;
    this.diagram = (XYDiagram)this.chart.Diagram;
    this.line = this.diagram.AxisX.ConstantLines.GetConstantLineByName("ConstantLine1");
}

private void chartControl1_MouseDown(object sender, MouseEventArgs e) {
    if(diagram == null)
        return;

    // Get the information about the clicked point.
    DiagramCoordinates coords = diagram.PointToDiagram(e.Location);

    // If the point is within the diagram and in the constant line ...
    if(!coords.IsEmpty && line.AxisValue is DateTime &&
        coords.DateTimeArgument.Equals((DateTime)line.AxisValue)) {

        // Allow dragging, catch the mouse and change the cursor.
        dragging = true;
        chart.Capture = true;
        SetCursor();
    }
}

private void chartControl1_MouseUp(object sender, MouseEventArgs e) {
    dragging = false;
    chart.Capture = false;
}

private void chartControl1_MouseMove(object sender, MouseEventArgs e) {
    if(diagram == null)
        return;

    if(dragging && (e.Button & MouseButtons.Left) == 0) {
        dragging = false;
        chart.Capture = false;
    }

    DiagramCoordinates coords = diagram.PointToDiagram(e.Location);
    if(coords.IsEmpty)
        RestoreCursor();
    else {
        if(dragging)
            line.AxisValue = coords.DateTimeArgument;

        if(line.AxisValue is DateTime && coords.DateTimeArgument.Equals((DateTime)line.AxisValue))
            SetCursor();
        else
            RestoreCursor();
    }
}

void SetCursor() {
    if(defCursor == null)
        defCursor = Cursor.Current;
    Cursor.Current = Cursors.VSplit;
}

void RestoreCursor() {
    if(defCursor != null) {
        Cursor.Current = defCursor;
        defCursor = null;
    }
}
Visual BasicCopyCode image复制代码
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...

Private defCursor As Cursor
Private dragging As Boolean = False
Private chart As ChartControl
Private diagram As XYDiagram
Private line As ConstantLine

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Me.chart = Me.chartControl1
    Me.diagram = CType(Me.chart.Diagram, XYDiagram)
    Me.line = Me.diagram.AxisX.ConstantLines.GetConstantLineByName("ConstantLine1")
End Sub

Private Sub chartControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _ 
Handles chartControl1.MouseDown
    If diagram Is Nothing Then
        Return
    End If

    ' Get the information about the clicked point.
    Dim coords As DiagramCoordinates = diagram.PointToDiagram(e.Location)

    ' If the point is within the diagram and in the constant line ...
    If (Not coords.IsEmpty) AndAlso TypeOf line.AxisValue Is DateTime _ 
    AndAlso coords.DateTimeArgument.Equals(CDate(line.AxisValue)) Then

        ' Allow dragging, catch the mouse and change the cursor.
        dragging = True
        chart.Capture = True
        SetCursor()
    End If
End Sub

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

Private Sub chartControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) _ 
Handles chartControl1.MouseMove
    If diagram Is Nothing Then
        Return
    End If

    If dragging AndAlso (e.Button And MouseButtons.Left) = 0 Then
        dragging = False
        chart.Capture = False
    End If

    Dim coords As DiagramCoordinates = diagram.PointToDiagram(e.Location)
    If coords.IsEmpty Then
        RestoreCursor()
    Else
        If dragging Then
            line.AxisValue = coords.DateTimeArgument
        End If

        If TypeOf line.AxisValue Is DateTime _
        AndAlso coords.DateTimeArgument.Equals(CDate(line.AxisValue)) Then
            SetCursor()
        Else
            RestoreCursor()
        End If
    End If
End Sub

Private Sub SetCursor()
    If defCursor Is Nothing Then
        defCursor = Cursor.Current
    End If
    Cursor.Current = Cursors.VSplit
End Sub

Private Sub RestoreCursor()
    If defCursor IsNot Nothing Then
        Cursor.Current = defCursor
        defCursor = Nothing
    End If
End Sub

CodeCentralShow Me

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

Expand image参阅