Back to Devexpress

XYDiagram2D.PointToDiagram(Point) Method

corelibraries-devexpress-dot-xtracharts-dot-xydiagram2d-dot-pointtodiagram-x28-system-dot-drawing-dot-point-x29.md

latest11.3 KB
Original Source

XYDiagram2D.PointToDiagram(Point) Method

Converts the chart control screen point’s coordinates (in pixels) into an XY-Diagram plot area coordinates object.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
public DiagramCoordinates PointToDiagram(
    Point p
)
vb
Public Function PointToDiagram(
    p As Point
) As DiagramCoordinates

Parameters

NameTypeDescription
pPoint

A point within the Chart Control’s area.

|

Returns

TypeDescription
DiagramCoordinates

An object that contains information about the point’s argument and value, their scale types, associated axes and pane.

|

Remarks

Use the PointToDiagram method to convert a chart control screen point’s coordinates that are indexed from its upper-left corner and measured in pixels to the diagram coordinates in axis measurement units.

To convert a screen coordinate point to a point with chart coordinates, use the ChartControl.PointToClient method.

csharp
private void chartControl_MouseMove(object sender, MouseEventArgs e) {
    DiagramCoordinates coords = ((XYDiagram2D)chartControl.Diagram).PointToDiagram(chartControl.PointToClient(Cursor.Position));
    label.Text = string.Format("X:{0}, Y:{1}", coords.NumericalArgument, coords.NumericalValue);
}
vb
Private Sub chartControl_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim coords As DiagramCoordinates = CType(chartControl.Diagram,XYDiagram2D).PointToDiagram(chartControl.PointToClient(Cursor.Position))
    label.Text = String.Format("X:{0}, Y:{1}", coords.NumericalArgument, coords.NumericalValue)
End Sub

Note

Use the XYDiagram2D.DiagramToPoint method to convert diagram coordinates to chart coordinates with an overload appropriate to your axes’ scale types.

With the Radar and Polar diagram types, use the RadarDiagram.PointToDiagram and RadarDiagram.DiagramToPoint methods.

Example

The following example illustrates use of XYDiagram2D.PointToDiagram method for dragging the constant line with the mouse, and changing the series point values accordingly.

csharp
using System;
using System.Windows.Forms;
using DevExpress.XtraCharts;

namespace InteractiveConstantLine {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        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 = this.chart.Diagram as XYDiagram;
            this.line = this.diagram.AxisX.ConstantLines.GetConstantLineByName("ConstantLine1");

            // Add a title to the chart.
            ChartTitle chartTitle1 = new ChartTitle();
            chartTitle1.Text = "Drag the constant line...";
            this.chart.Titles.Add(chartTitle1);

        }

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

    }
}
vb
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraCharts

Namespace InteractiveConstantLine
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        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 = TryCast(Me.chart.Diagram, XYDiagram)
            Me.line = Me.diagram.AxisX.ConstantLines.GetConstantLineByName("ConstantLine1")

            ' Add a title to the chart.
            Dim chartTitle1 As New ChartTitle()
            chartTitle1.Text = "Drag the constant line..."
            Me.chart.Titles.Add(chartTitle1)

        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 Date 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 Date 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

    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the PointToDiagram(Point) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-chart-make-points-in-a-chartcontrol-adjustable-interactively/CS/Form1.cs#L20

csharp
DiagramCoordinates point =
    ((XYDiagram)(sender as ChartControl).Diagram).PointToDiagram(e.HitInfo.HitPoint);

winforms-chart-make-points-in-a-chartcontrol-adjustable-interactively/VB/Form1.vb#L22

vb
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

See Also

DiagramToPoint

Handling Client-side Scripts

XYDiagram2D Class

XYDiagram2D Members

DevExpress.XtraCharts Namespace