C# 每个chartArea显示最小值、平均值、最大值

csharp 复制代码
private void AddStatisticsAnnotations(ChartArea chartArea, int channelIndex)
{
    RemoveExistingAnnotations(channelIndex);

    // 获取ChartArea的相对坐标(百分比)
    float chartAreaX = chartArea.Position.X; // X坐标(百分比)
    float chartAreaY = chartArea.Position.Y; // Y坐标(百分比)
    float chartAreaWidth = chartArea.Position.Width; // 宽度(百分比)
    float chartAreaHeight = chartArea.Position.Height; // 高度(百分比)

    // 设置相对偏移量(百分比)
    float offsetX = 10f;  // 水平偏移(百分比)
    float offsetY = 5f;  // 垂直偏移(百分比)

    // 计算注释的相对坐标(相对于整个Chart控件)
    float posX = chartAreaX + offsetX;
    float posY = chartAreaY + offsetY;

    // 创建并配置注释
    Color textColor = _channelColors[channelIndex % _channelColors.Length];

    // 创建包含所有统计信息的多行文本
    string annotationText = $"Max: - V\nAvg: - V\nMin: - V";

    TextAnnotation annotation = new TextAnnotation
    {
        Name = $"StatsAnnotation{channelIndex}",
        Text = annotationText,
        ForeColor = textColor,
        X = posX,
        Y = posY,
        Alignment = ContentAlignment.TopLeft,
        ClipToChartArea = chartArea.Name,
        IsSizeAlwaysRelative = true, // 使用相对坐标
        Font = new Font("Arial", 10)  // 设置合适的字体大小
    };

    chartWaveform.Annotations.Add(annotation);
    //Console.WriteLine($"Added {annotation.Name} at ({annotation.X}%, {annotation.Y}%)");        
}

private void RemoveExistingAnnotations(int channelIndex)
{       
    // 移除旧的统计注释
    string annotationName = $"StatsAnnotation{channelIndex}";
    var annotation = chartWaveform.Annotations.FindByName(annotationName);
    if (annotation != null)
    {
        chartWaveform.Annotations.Remove(annotation);
    }
}

private void UpdateChannelStatistics(int channelIndex, List<ushort> values)
{
    if (values == null || values.Count == 0) return;

    // 计算统计值
    double maxAdc = values[0];
    double minAdc = values[0];
    double sumAdc = 0;

    foreach (var value in values)
    {
        if (value > maxAdc) maxAdc = value;
        if (value < minAdc) minAdc = value;
        sumAdc += value;
    }

    double avgAdc = sumAdc / values.Count;

    // 转换为电压值
    double maxAdcValue = Math.Pow(2, adcBit) - 1;
    double maxVoltage = vref / 1000.0;

    double maxV = (maxAdc / maxAdcValue) * maxVoltage;
    double minV = (minAdc / maxAdcValue) * maxVoltage;
    double avgV = (avgAdc / maxAdcValue) * maxVoltage;

    // 更新单个注释的文本
    UpdateStatsAnnotationText(channelIndex, maxV, avgV, minV);
}

private void UpdateStatsAnnotationText(int channelIndex, double maxV, double avgV, double minV)
{
    string annotationName = $"StatsAnnotation{channelIndex}";
    var annotation = chartWaveform.Annotations.FindByName(annotationName) as TextAnnotation;
    if (annotation != null)
    {
        annotation.Text = $"Max: {maxV:F3}V\nAvg: {avgV:F3}V\nMin: {minV:F3}V";
    }
}  

// 为每个通道添加统计注释
AddStatisticsAnnotations(chartWaveform.ChartAreas[ch], ch);
// 计算并更新统计信息
UpdateChannelStatistics(ch, channelValues[ch]);
相关推荐
MapGIS技术支持19 小时前
MapGIS Objects Java计算一个三维点到平面的距离
java·开发语言·平面·制图·mapgis
程序员zgh19 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
小灰灰搞电子19 小时前
Qt 重写QRadioButton实现动态radioButton源码分享
开发语言·qt·命令模式
by__csdn20 小时前
Vue3 setup()函数终极攻略:从入门到精通
开发语言·前端·javascript·vue.js·性能优化·typescript·ecmascript
喵了meme20 小时前
C语言实战5
c语言·开发语言
廋到被风吹走20 小时前
【Java】常用设计模式及应用场景详解
java·开发语言·设计模式
Sammyyyyy20 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
小小代码团20 小时前
2026 Office Online Server (全网最新/最详细/含问题修复) 终极部署教程
windows·microsoft·c#
Luna-player20 小时前
在前端中,<a> 标签的 href=“javascript:;“ 这个是什么意思
开发语言·前端·javascript
小草cys21 小时前
项目7-七彩天气app任务7.4.2“关于”弹窗
开发语言·前端·javascript