-1

I added now start point end point and mouse x and mouse y variables i want to use them to draw points on the chart control when clicking with the mouse on the left button.

But i want that the points to be draw only on the chart area and also only when the mouse is inside a square area in the chart so it will not draw a point on the squares borders lines or outside the chart control area.

And also to display when moving the mouse in the squares in the chart to show me the axis X and axis Y values on labels.

The left axis 1 to 120 present time and bottom axis 1 to 30 present days. So if i move the mouse in the first square area it should show me Day 1 Time 112 or Day 2 Time 33.

That's why i'm also not sure that the spaces in the axis X and axis Y are right. It should be 1 to 120 and 1 to 30 but i think every square should present inside 3 days and 120 time but in jumps of 1 steps of 1 so when i move with the mouse i can see in the first squares Day 1 Time 3 or Day 3 Time 66 The next row of squares will present days 4 to 6.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Test
{
    public partial class Form1 : Form
    {
        private Point startPoint = new Point();
        private Point endPoint = new Point();
        private int mouseX = 0;
        private int mouseY = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void chart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseX = System.Windows.Forms.Cursor.Position.X;
                mouseY = System.Windows.Forms.Cursor.Position.Y;
                chart1.Invalidate();

            }
        }

        private void chart1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g1 = this.CreateGraphics();
            Pen linePen = new Pen(Color.Green, 1);
            Pen ellipsePen = new Pen(Color.Red, 1);
            startPoint = new Point(mouseX, mouseY);
            endPoint = new Point(mouseX, mouseY);
            g1.DrawLine(linePen, startPoint, endPoint);
            g1.DrawEllipse(ellipsePen, mouseX - 2, mouseY - 2, 4, 4);
            linePen.Dispose();
            ellipsePen.Dispose();
            g1.Dispose();
        }
    }
}

The way the code is now, the points it's drawing are far out of the Chart control area.

Daniel Hamutel
  • 643
  • 1
  • 13
  • 30
  • 1
    possible duplicate of [How to draw a graph in chart control with mouse](http://stackoverflow.com/questions/29902288/how-to-draw-a-graph-in-chart-control-with-mouse) – TaW May 14 '15 at 09:18
  • TaW i tried your solution there and it does working. I took your second solution code and i changed something. Instead using the List of Points i'm using only the lastPoint variable in the paint event and i did: e.Graphics.DrawEllipse(pen, lastPoint.X, lastPoint.Y, 4, 4); i did it since i want to draw each time only one single point. The problem now is that it's deleting the last drawn point each time i click the mouse. How can i make that it will not delete the lastPoint ? – Daniel Hamutel May 14 '15 at 10:42
  • The post keeps adding the drawn points to the datapoints collection. If you don't want that and only draw one drawn point you still will add it to the series points, or maybe an extra series; then you can calculate its correct position and set the newPoint with these coodinates. Finally you would remove the datapoint.. Did I understand that right? – TaW May 14 '15 at 10:53
  • Do have a look at my [answer here](http://stackoverflow.com/questions/30247240/how-to-make-that-the-coordinates-of-a-drawing-point-will-be-from-two-textboxes/30248038#30248038). It contains general purpose code to transform value points into drawing points.. – TaW May 14 '15 at 22:03

1 Answers1

1

That's because you are using the wrong mouse coordinates. Replace these lines

mouseX = System.Windows.Forms.Cursor.Position.X;
mouseY = System.Windows.Forms.Cursor.Position.Y;

With this:

mouseX = e.X;
mouseY = e.Y;

The System.Windows.Forms.Cursor.Position returns the coordinates of the mouse using the form as a base, while the MouseEventArgs returns the coordinates of the mouse using the control that raised the event as a base.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121