Back to Devexpress

How to: Provide Interactive Editing for Series Points

windowsforms-5365-controls-and-libraries-chart-control-examples-end-user-interaction-how-to-provide-interactive-editing-for-series-points.md

latest4.9 KB
Original Source

How to: Provide Interactive Editing for Series Points

  • Dec 11, 2023
  • 2 minutes to read

This example describes how to make points in a ChartControl adjustable interactively. The key principals of this example include handling the ChartControl.ObjectHotTracked event and using the XYDiagram2D.PointToDiagram method to convert physical coordinates of the mouse to logical.

csharp
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;
}
vb
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

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e294/chart-for-winforms-how-to-make-points-in-a-chart-control-interactively-adjustable.

See Also

How to: Determine which Series Point Is Under the Test Point

How to: Provide Interactive Editing for a Constant Line

How to: Show Series Labels Only for Hot-tracked Points