我现在添加了起点终点和鼠标x和鼠标y变量,我想用它们在图表控件上用左键单击鼠标时画点。如何使用鼠标点击事件和Windows窗体图表控件的绘图事件来绘制图表上的点?
但我想只绘制点的图表区域,也只有当鼠标位于图表的正方形区域内,因此它不会在正方形边界线上或图表控制区域外绘制点。
还可以在图表中的正方形中移动鼠标以显示标签上的轴X和轴Y值时显示。
左轴1至120现在的时间轴和底轴1至30天。 因此,如果我在第一个方块区域移动鼠标,它应该会显示第1天时间112或第2天时间33.
这就是为什么我也不确定X轴和Y轴的空间是否正确。 它应该是1到120和1到30,但我认为每个方块应该在3天内和120个时间内出现,但是在1步的1个跳跃中,所以当我用鼠标移动时,我可以在第一个方块中看到第1天时间3或第3天时间66 方块的下一行将呈现天4〜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();
}
}
}
方式的代码现在,它的绘图的点是远图表控制区。
[如何用鼠标在图表控件中绘制图形]的可能重复(http://stackoverflow.com/questions/29902288/how-to-draw-a-graph-in-chart-control-with-mouse ) – TaW
我试过你的解决方案,它确实工作。我把你的第二个解决方案代码,我改变了一些。相反,使用点列表我只使用paint事件中的lastPoint变量,我做了:e.Graphics.DrawEllipse(pen,lastPoint.X,lastPoint.Y,4,4);我做了它,因为我想绘制每一次只有一个单一的点。现在的问题是,每次单击鼠标时都会删除最后绘制的点。我怎样才能使它不会删除lastPoint? –
该帖子不断将绘制的点添加到数据点集合。如果你不想要,只画出一个点,你仍然会将它添加到系列点,或者可能是一个额外的系列;那么你可以计算它的正确位置并用这些坐标设置newPoint。最后,你会删除数据点。我明白了吗? – TaW