You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Infragistics UltraChart添加框选数据点的图层

实现UltraChart矩形框选数据点的方案

Hey there! 我之前在Infragistics的UltraChart上也碰到过一模一样的需求,折腾了好一阵终于搞定了,下面把具体的实现思路和代码分享给你,应该能直接复用:

核心思路

UltraChart本身没内置矩形框选的功能,所以得手动通过鼠标事件来实现,核心步骤就是:

  • 监听鼠标的按下、移动、释放事件,记录选框的起止坐标
  • 在图表上绘制临时的虚线矩形,给用户视觉反馈
  • 遍历所有数据点,判断哪些点落在选框范围内
  • 手动设置这些点的选中状态,触发后续业务逻辑

具体实现步骤

1. 定义变量跟踪选框状态

先在你的窗体/控件类里定义几个变量,用来记录选框的状态:

private bool _isSelecting;
private Point _selectStartPoint;
private Rectangle _selectionRect;
private UltraChart _targetChart; // 你的UltraChart实例

2. 绑定图表的鼠标与绘制事件

在窗体初始化或者图表加载完成后,绑定对应的事件:

_targetChart.MouseDown += TargetChart_MouseDown;
_targetChart.MouseMove += TargetChart_MouseMove;
_targetChart.MouseUp += TargetChart_MouseUp;
_targetChart.Paint += TargetChart_Paint;

3. 处理鼠标按下事件

记录选框的起始点,同时清空之前的选中状态:

private void TargetChart_MouseDown(object sender, MouseEventArgs e)
{
    // 只响应左键按下
    if (e.Button != MouseButtons.Left) return;
    
    _isSelecting = true;
    _selectStartPoint = e.Location;
    _selectionRect = new Rectangle(_selectStartPoint, Size.Empty);
    
    // 清空历史选中状态
    foreach (var series in _targetChart.Series)
    {
        foreach (var point in series.Points)
        {
            point.Selected = false;
        }
    }
    _targetChart.Refresh();
}

4. 处理鼠标移动事件

实时更新选框的大小,确保拖动方向不同时矩形也能正常显示:

private void TargetChart_MouseMove(object sender, MouseEventArgs e)
{
    if (!_isSelecting) return;
    
    // 计算选框的正确坐标(避免拖动方向反转导致矩形异常)
    int rectX = Math.Min(_selectStartPoint.X, e.X);
    int rectY = Math.Min(_selectStartPoint.Y, e.Y);
    int rectWidth = Math.Abs(_selectStartPoint.X - e.X);
    int rectHeight = Math.Abs(_selectStartPoint.Y - e.Y);
    
    _selectionRect = new Rectangle(rectX, rectY, rectWidth, rectHeight);
    _targetChart.Refresh(); // 触发Paint事件绘制选框
}

5. 处理鼠标释放事件

遍历所有数据点,判断是否在选框内并设置选中状态:

private void TargetChart_MouseUp(object sender, MouseEventArgs e)
{
    if (!_isSelecting) return;
    
    _isSelecting = false;
    
    // 遍历所有系列的所有数据点
    foreach (var series in _targetChart.Series)
    {
        foreach (var point in series.Points)
        {
            // 将数据点的屏幕坐标转换为图表控件内的像素坐标
            Point pixelPos = _targetChart.PointToClient(_targetChart.ChartArea.PointToScreen(point.ScreenLocation));
            
            // 判断点是否落在选框内
            if (_selectionRect.Contains(pixelPos))
            {
                point.Selected = true;
            }
        }
    }
    
    _targetChart.Refresh();
    // 这里可以添加选中后的业务逻辑,比如弹窗展示选中点详情等
}

6. 在Paint事件中绘制选框

给用户直观的选框视觉反馈:

private void TargetChart_Paint(object sender, PaintEventArgs e)
{
    if (_isSelecting && _selectionRect.Width > 0 && _selectionRect.Height > 0)
    {
        // 自定义选框样式,这里用蓝色虚线
        using (Pen selectPen = new Pen(Color.DeepSkyBlue, 1))
        {
            selectPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            e.Graphics.DrawRectangle(selectPen, _selectionRect);
        }
    }
}

小提示

  • 如果你的UltraChart版本不同,point.ScreenLocation的属性名可能有细微差异,记得根据实际API调整
  • 要是你的图表是实时刷新数据的,注意在数据更新时不要误清空选中状态(除非业务需要)
  • 可以把选框改成半透明填充样式,让选中区域更显眼

内容的提问来源于stack exchange,提问作者john true

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:36:05