New to Telerik UI for WinFormsStart a free 30-day trial

How to add DataPoint on mouse click location.

Updated over 6 months ago

Environment

Product VersionProductAuthor
2023.1.314RadChartView for WinFormsDinko Krastev

Description

In this tutorial, we will demonstrate how to add DataPoint on mouse click location. In addition, we will add a border to the newly added data point to distinguish the points added using the mouse.

chartview-add-datapoint-onclick001

Solution

To make this work, you will need to get the X and Y axis value depending on the mouse location. Using these values, we can create new DataPoint and add it to the DataPoints collection of the series. For the purpose of this tutorial we are going to use ScatterSeries.

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ScatterSeries scatterSeries = new ScatterSeries();
        scatterSeries.BorderColor = Color.Red;
        scatterSeries.Shape = new CircleShape();
        scatterSeries.PointSize = new SizeF(15,15);
        scatterSeries.Name = "";
        scatterSeries.DataPoints.Add(new ScatterDataPoint(15, 19));
        scatterSeries.DataPoints.Add(new ScatterDataPoint(18, 10));
        scatterSeries.DataPoints.Add(new ScatterDataPoint(13, 15));
        scatterSeries.DataPoints.Add(new ScatterDataPoint(10, 8));
        scatterSeries.DataPoints.Add(new ScatterDataPoint(5, 12));
        this.radChartView1.Series.Add(scatterSeries);
        this.radChartView1.MouseDown += RadChartView1_MouseDown;
    }

    private object GetHorizontalAxisValueFromMouse(MouseEventArgs e)
    {
        LinearAxis axis = radChartView1.Axes[1] as LinearAxis;
        double delta = axis.ActualRange.Maximum - axis.ActualRange.Minimum;
        double totalHeight = axis.Model.LayoutSlot.Height;
        double ratio = 1 - (e.Location.Y - this.radChartView1.Area.View.Viewport.Y - axis.Model.LayoutSlot.Y) / totalHeight;
        double value = axis.ActualRange.Minimum + delta * ratio;

        return value;
    }

    private object GetVerticalAxisValueFromMouse(MouseEventArgs e)
    {
        LinearAxis axis = radChartView1.Axes[0] as LinearAxis;
        double delta = axis.ActualRange.Maximum - axis.ActualRange.Minimum;
        double totalWidth = axis.Model.LayoutSlot.Width;
        double ratio = (e.Location.X - this.radChartView1.Area.View.Viewport.X - axis.Model.LayoutSlot.X) / totalWidth;
        double value = axis.ActualRange.Minimum + delta * ratio;

        return value;
    }

    private void RadChartView1_MouseDown(object sender, MouseEventArgs e)
    {
        ScatterSeries scatterSeries = this.radChartView1.Series[0] as ScatterSeries;                   
        object hValue = this.GetVerticalAxisValueFromMouse(e);
        object vValue = this.GetHorizontalAxisValueFromMouse(e);
        double horizontalValue = Math.Round(double.Parse(hValue.ToString()), 2);
        double verticvalValue = Math.Round(double.Parse(vValue.ToString()), 2);
        scatterSeries.DataPoints.Add(new ScatterDataPoint(horizontalValue, verticvalValue));
        scatterSeries.Children.Last().BorderWidth = 2;
                          
    }
}
      
       
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support