C#mschart控件框选删除部分数据及游标CursorXCursorY使⽤C# mschart 控件 框选 删除部分数据
实现功能 :
1、⿏标框选,选中chart控件中部分数据;
2、将选中的数据标红显⽰;
3、删除选中的红⾊数据;
demo源码下载:
核⼼代码说明:
1、对画图区域的设置,⾸先说明chart控件的⾯积问题。对于下图中的⼀个chart,有2个Position控制⾯积⼤⼩,
//写在窗体的load内:
//设置范围区域,很重要!
ChartArea ca = this.chart1.ChartAreas[0];
//设置画图的⼤⼩,这个是带有X Y 轴的⾯积
ca.Position.Auto = false;
ca.Position.Height = 100F;//为了⽅便,设置100%
ca.Position.Width = 100F;
//设置内部不包含X Y 轴的⽐例,
ca.InnerPlotPosition.Auto = false;
ca.InnerPlotPosition.Height = 90F;
ca.InnerPlotPosition.Width = 97F;
ca.InnerPlotPosition.X = 3F;//给左侧Y轴留下空间
ca.InnerPlotPosition.Y = 3F;//给上⾯顶部留点空间,
根据上⾯的说明,按照代码中的设置,每个点的位置,只要根据chart控件的⼤⼩,和innerplotposition的⽐例,可以计算每个点的实际坐标,和⿏标的框选区域坐标进⾏对⽐,确定选中的点。
2、坐标换算
//把数据值换算到坐标中,要特别注意。
ChartArea ca = this.chart1.ChartAreas[0];
double xpercent = ca.InnerPlotPosition.Width / (ca.AxisX.Maximum - ca.AxisX.Minimum);
double ypercent = ca.InnerPlotPosition.Height / (ca.AxisY.Maximum - ca.AxisY.Minimum);
double x = this.chart1.Width * (ca.InnerPlotPosition.X + (item.Key - ca.AxisX.Minimum) * (xpercent)) / 100;
double y = this.chart1.Height * (ca.InnerPlotPosition.Y + (ca.AxisY.Maximum - item.Value) * (ypercent)) / 100;
3、游标使⽤,CursorX CursorY ,可以实现对选中区域的最⼤化放⼤,
chartArea1.CursorX.IsUserEnabled = true;
chartArea1.CursorX.IsUserSelectionEnabled = true;
chartArea1.CursorX.SelectionColor = System.Drawing.Color.Red;
chartArea1.CursorY.IsUserEnabled = true;
chartArea1.CursorY.IsUserSelectionEnabled = true;
4、源码
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 ChartSelectPoint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
textbox控件边框设置Dictionary<int, double> dicData = new Dictionary<int, double>();
private void timer1_Tick(object sender, EventArgs e)
{
if (this.chart1.Series[0].Points.Count < 20)
{
this.chart1.Series[0].Points.AddXY(count, ra.Next(0, 50));
this.chart1.ChartAreas[0].AxisX.Minimum = 0;
this.chart1.ChartAreas[0].AxisX.Maximum = 19;
}
else
{
this.chart1.Series[0].Points.RemoveAt(0);
this.chart1.Series[0].Points.AddXY(count, ra.Next(0, 50));
this.chart1.ChartAreas[0].AxisX.Minimum = this.chart1.Series[0].Points[0].XValue; this.chart1.ChartAreas[0].AxisX.Maximum = this.chart1.Series[0].Points[19].XValue;
}
count++;
}
int count = 0;
Random ra = new Random();
private int maxCount = 60;
private void Form1_Load(object sender, EventArgs e)
{
//1 数据滚动显⽰
//timer1.Interval = 100;
//timer1.Enabled = true;
//设置范围区域,很重要!
ChartArea ca = this.chart1.ChartAreas[0];
//设置画图的⼤⼩,这个是带有X Y 轴的⾯积
ca.Position.Auto = false;
ca.Position.Height = 100F;//为了⽅便,设置100%
ca.Position.Width = 100F;
//设置内部不包含X Y 轴的⽐例,
ca.InnerPlotPosition.Auto = false;
ca.InnerPlotPosition.Height = 90F;
ca.InnerPlotPosition.Width = 97F;
ca.InnerPlotPosition.X = 3F;//给左侧Y轴留下空间
ca.InnerPlotPosition.Y = 3F;//给上⾯顶部留点空间,
//数据初始化
for (int i = 0; i < this.maxCount; i++)
{
var v = ra.Next(0, 50);
dicData.Add(i, v);
this.chart1.Series[0].Points.AddXY(i, v);
}
}
private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
//获得区域的点
MessageBox.Show("游标" + e.NewSelectionStart + " " + e.NewSelectionEnd);
}
Point start = new Point();
Point end = new Point();
private void chart1_MouseDown(object sender, MouseEventArgs e)
{
start.X = e.X;
start.Y = e.Y;
}
private void chart1_MouseUp(object sender, MouseEventArgs e)
{
//return;
end.X = e.X;
end.Y = e.Y;
//TODO 1、坐标计算是重点 ,开始,停⽌的 X Y ,各个点的X Y
Dictionary<int, double> dicDataSave = new Dictionary<int, double>();
Dictionary<int, double> dicDataDel = new Dictionary<int, double>();
//double minValue = (from d in dicData.Values select d).ToList().Min();
//double maxValue = (from d in dicData.Values select d).ToList().Max();
ChartArea ca = this.chart1.ChartAreas[0];
double xpercent = ca.InnerPlotPosition.Width / (ca.AxisX.Maximum - ca.AxisX.Minimum);
double ypercent = ca.InnerPlotPosition.Height / (ca.AxisY.Maximum - ca.AxisY.Minimum);
int dicCount = 0;
foreach (KeyValuePair<int, double> item in dicData)
{
//把数据值换算到坐标中,要特别注意。
double x = this.chart1.Width * (ca.InnerPlotPosition.X + (item.Key - ca.AxisX.Minimum) * (xpercent)) / 100;
double y = this.chart1.Height * (ca.InnerPlotPosition.Y + (ca.AxisY.Maximum - item.Value) * (ypercent)) / 100;
float minx = start.X < end.X ? start.X : end.X;
float maxx = start.X < end.X ? end.X : start.X;
float miny = start.Y < end.Y ? start.Y : end.Y;
float maxy = start.Y < end.Y ? end.Y : start.Y;
bool isSave = true;
if (x > minx && x < maxx)
{
if (y > miny && y < maxy)
{
isSave = false;
dicDataDel.Add(item.Key, item.Value);
this.chart1.Series[0].Points[dicCount].Color = Color.Red;
}
}
if (isSave)
{
dicDataSave.Add(item.Key, item.Value);
}
dicCount++;
}
if (MessageBox.Show("确定删除红⾊的点 ?", "提⽰", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
dicData = dicDataSave;//确定删除,才替换数据
this.chart1.Series[0].Points.Clear();
foreach (KeyValuePair<int, double> item in dicData)
{
this.chart1.Series[0].Points.AddXY(item.Key, item.Value);
}
}
else
{
foreach (var item in this.chart1.Series[0].Points)
{
if (item.Color == Color.Red)
{
item.Color = Color.Black;
}
}
}
}
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
//TODO 在拉动的过程中,增加⽅框虚线
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论