int x1 = 0;
int y1 = 0; //鼠标焦点左上角
double x_begin = 0;
double y_end = 0; //chart坐标左上角
private void Form1_Load(object sender, EventArgs e)
{
for (double i = 1; i < 20; i++)
{
chart1.Series["Series1"].Points.AddXY(i, i); /////简单初始化 chart
}
}
//chart 鼠标按下事件
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
x1 = e.X;
y1 = e.Y;
x_begin = chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(Convert.ToDouble(x1)); //鼠标焦点坐标 转换为chart坐标
y_end= chart1.ChartAreas["ChartArea1"].AxisY.PixelPositionToValue(Convert.ToDouble(y1));
can_fit = true;
}
//chart 鼠标移动事件
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
{
if (can_fit)
{
int x = e.X;
int y = e.Y;
double xx = chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(Convert.ToDouble(x));
double yy = chart1.ChartAreas["ChartArea1"].AxisY.PixelPositionToValue(Convert.ToDouble(y));
label1.Text = xx.ToString() + " " + yy.ToString();
this.Refresh();
Pen blackPen = new Pen(Color.Red, 2);
blackPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
chart1.CreateGraphics().DrawRectangle(blackPen, x1, y1, x - x1, y - x1); //矩形框
}
}
}
//chart 鼠标松开事件
private void chart1_MouseUp(object sender, MouseEventArgs e)
{
can_fit = false ;
this.Refresh();
double x_end = chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(Convert.ToDouble(e.X));
double y_begin = chart1.ChartAreas["ChartArea1"].AxisY.PixelPositionToValue(Convert.ToDouble(e.Y));
chart1.ChartAreas["ChartArea1"].AxisX.Minimum = Convert.ToDouble(((int)x_begin));
chart1.ChartAreas["ChartArea1"].AxisY.Minimum = Convert.ToDouble(((int)y_begin));
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = Convert.ToDouble(((int)x_end));
chart1.ChartAreas["ChartArea1"].AxisY.Maximum = Convert.ToDouble(((int)y_end));
}
效果图如下:
放大后: