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]);
相关推荐
Terio_my2 小时前
Java bean 数据校验
java·开发语言·python
Tony Bai2 小时前
【Go开发者的数据库设计之道】07 诊断篇:SQL 性能诊断与问题排查
开发语言·数据库·后端·sql·golang
超级大只老咪2 小时前
何为“类”?(Java基础语法)
java·开发语言·前端
我笑了OvO3 小时前
C++类和对象(1)
java·开发语言·c++·类和对象
渡我白衣5 小时前
C++ 异常处理全解析:从语法到设计哲学
开发语言·c++·面试
悦悦子a啊5 小时前
[Java]PTA: jmu-Java-02基本语法-08-ArrayList入门
java·开发语言·算法
毕设源码-郭学长7 小时前
【开题答辩全过程】以 PHP茶叶同城配送网站的设计与实现为例,包含答辩的问题和答案
开发语言·php
JavaPub-rodert8 小时前
用 go-commons 打造更优雅的字符串处理工具
开发语言·后端·golang
Archie_IT8 小时前
嵌入式八股文篇——P1 关键字篇
c语言·开发语言·单片机·mcu·物联网·面试·职场和发展
workflower8 小时前
将图片中的图形转换为可编辑的 PPT 图形
java·开发语言·tomcat·powerpoint·个人开发·结对编程