1、图形自动滚动设置
chart1.ChartAreas[0].AxisX.Maximum 横坐标显示区域最大值
chart1.ChartAreas[0].AxisX.Minimum 横坐标显示区域最小值
显示宽度 = chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum
chart1.ChartAreas[0].AxisX.Maximum = x_data;
if (x_data > AaxisXScrollNum)
{
chart1.ChartAreas[0].AxisX.Minimum = x_data - AaxisXScrollNum;
}
else
{
chart1.ChartAreas[0].AxisX.Minimum = 0;
}
2、图形移动
chart1.ChartAreas[0].AxisX.ScaleView.Position X轴相对于起始点被移动的距离
chart1.ChartAreas[0].AxisY.ScaleView.Position Y轴相对于起始点被移动的距离
3、图表缩放
chart1.ChartAreas[0].AxisX.ScaleView.Size X轴在原基础上的缩放比列
chart1.ChartAreas[0].AxisX.ScaleView.Size Y轴在原基础上的缩放比列
4、坐标轴显示格式
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "N1"; //限制X轴标签显示浮点数一位小数
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "N1"; //限制Y轴标签显示浮点数一位小数
5、显示效果
代码上一波,方便以后复制
/// <summary>
/// 图标数据刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
x_data += 1;
double y = Math.Sin(x_data * Math.PI / 180.0);
chart1.ChartAreas[0].AxisX.Maximum = x_data;
if (x_data > AaxisXScrollNum) //调整显示区域跟随
{
chart1.ChartAreas[0].AxisX.Minimum = x_data - AaxisXScrollNum;
}
else
{
chart1.ChartAreas[0].AxisX.Minimum = 0;
}
chart1.Series[0].Points.AddXY(x_data, y);
}
double x_data = 0;
private void button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < 360 * 2; i++)
{
x_data += 1;
double y = Math.Sin(i * Math.PI / 180.0);
chart1.Series[0].Points.AddXY(x_data, y);
}
}
public bool isMouseDown = false;
public int lastMove = 0; // 用于记录鼠标上次移动的点,用于判断是左移还是右移
double mouseDownPosX, mouseRateX;
int mouseDownXoldX, mouseDownxNewX;
double mouseDownPosY, mouseRateY;
int mouseDownXoldY, mouseDownxNewY;
#region 鼠标移动效果
private void chart1_MouseUp(object sender, MouseEventArgs e)
{
IsMoveChart = false;
}
bool IsMoveChart;
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
// if (checkBox1.Checked)
{
mouseDownPosX = chart1.ChartAreas[0].AxisX.ScaleView.Position;
mouseDownXoldX = e.X;
mouseRateX = chart1.ChartAreas[0].AxisX.PixelPositionToValue(2) - chart1.ChartAreas[0].AxisX.PixelPositionToValue(1);
IsMoveChart = true;
}
// if (checkBox2.Checked)
{
mouseDownPosY = chart1.ChartAreas[0].AxisY.ScaleView.Position;
mouseDownXoldY = e.Y;
mouseRateY = chart1.ChartAreas[0].AxisY.PixelPositionToValue(2) - chart1.ChartAreas[0].AxisY.PixelPositionToValue(1);
IsMoveChart = true;
}
}
/// <summary>
/// 精确随鼠标移动
/// </summary>
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
if (IsMoveChart)
{
// if (checkBox1.Checked)
{
mouseDownxNewX = e.X;
chart1.ChartAreas[0].AxisX.ScaleView.Position = mouseDownPosX + (mouseDownXoldX - mouseDownxNewX) * mouseRateX;
}
// if (checkBox2.Checked)
{
mouseDownxNewY = e.Y;
chart1.ChartAreas[0].AxisY.ScaleView.Position = mouseDownPosY + (mouseDownXoldY - mouseDownxNewY) * mouseRateY;
}
Text = "SCA:" + chart1.ChartAreas[0].AxisX.ScaleView.Size + " " + chart1.ChartAreas[0].AxisY.ScaleView.Size + " POS:" + chart1.ChartAreas[0].AxisX.ScaleView.Position + " " + chart1.ChartAreas[0].AxisY.ScaleView.Position;
}
}
#endregion
#region 鼠标缩放效果
/// <summary>
/// 以鼠标中心缩放大小
/// </summary>
private void ResetPosVal(MouseEventArgs e)
{
if (checkBox3.Checked)
{
double newSizeXz = 0;//
if (e.Delta < 0) //设定放大或者缩小范围
{
newSizeXz = chart1.ChartAreas[0].AxisX.ScaleView.Size * 1.2f;
}
else
{
newSizeXz = chart1.ChartAreas[0].AxisX.ScaleView.Size * 0.8f;
}
if (newSizeXz > 0)
{
double xMouseNew;
double xMouseOld = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
chart1.ChartAreas[0].AxisX.ScaleView.Size = newSizeXz;
xMouseNew = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
double newPosition = xMouseOld - xMouseNew;
chart1.ChartAreas[0].AxisX.ScaleView.Position += Math.Round(newPosition, 3);
}
}
if (checkBox4.Checked)
{
double newSizeYz = 0;//
if (e.Delta < 0) //设定放大或者缩小范围
{
newSizeYz = chart1.ChartAreas[0].AxisY.ScaleView.Size * 1.2f;
}
else
{
newSizeYz = chart1.ChartAreas[0].AxisY.ScaleView.Size * 0.8f;
}
if (newSizeYz > 0)
{
double xMouseNew;
double xMouseOld = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
chart1.ChartAreas[0].AxisY.ScaleView.Size = newSizeYz;
xMouseNew = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
double newPosition = xMouseOld - xMouseNew;
chart1.ChartAreas[0].AxisY.ScaleView.Position += Math.Round(newPosition, 3);
}
}
Text = "SCA:" + chart1.ChartAreas[0].AxisX.ScaleView.Size + " " + chart1.ChartAreas[0].AxisY.ScaleView.Size + " POS:" + chart1.ChartAreas[0].AxisX.ScaleView.Position + " " + chart1.ChartAreas[0].AxisY.ScaleView.Position;
}
#endregion
/// <summary>
/// 恢复缩放位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset(0);
Text = "SCA:" + chart1.ChartAreas[0].AxisX.ScaleView.Size + " " + chart1.ChartAreas[0].AxisY.ScaleView.Size + " POS:" + chart1.ChartAreas[0].AxisX.ScaleView.Position + " " + chart1.ChartAreas[0].AxisY.ScaleView.Position;
timer1.Start();
}
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chart1_MouseWheel(object sender, MouseEventArgs e)
{
ResetPosVal(e);
}
/// <summary>
/// 停止数据更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
timer1.Stop();
// 设置不显示chart自带的滚动条
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
chart1.ChartAreas[0].AxisY.ScaleView.Size = chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum - chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum;
chart1.ChartAreas[0].AxisX.ScaleView.Size = chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum - chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
Text = "SCA:" + chart1.ChartAreas[0].AxisX.ScaleView.Size + " " + chart1.ChartAreas[0].AxisY.ScaleView.Size + "POS:" + chart1.ChartAreas[0].AxisX.ScaleView.Position + " " + chart1.ChartAreas[0].AxisY.ScaleView.Position;
}