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

如何在LiveCharts 2(C# MAUI)中格式化数据标签?

解决MAUI LiveCharts 2数据标签千位分隔符格式化问题
  • 核心逻辑:DataLabelsFormatter是Func<ChartPoint, string>类型的委托,只需传入自定义格式化逻辑,就能将double?数值转为带千位分隔符的字符串。
  • 实现代码:替换DataLabelsFormatter的占位符为以下lambda表达式,处理数据并格式化:
// Serie #1 : Author 1
ColumnSeries<double?> seriesAuthor1 = new()
{
    Values = new List<double?> { groups.ElementAt(0).MessagesCnt },
    Name = groups.ElementAt(0).Sender,
    Stroke = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)) { StrokeThickness = 2 },
    Fill = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)),
    DataLabelsSize = 20,
    DataLabelsPaint = new SolidColorPaint(SKColor.Parse(App.thePrefs.Sender1Color)),
    // 处理nullable double,添加千位分隔符
    DataLabelsFormatter = point => 
    {
        if (point.Value is double numericValue)
        {
            // N0表示整数千位分隔,如需保留小数可改为N2(保留两位)等格式
            return numericValue.ToString("N0");
        }
        // 空值时返回空字符串或自定义占位符
        return string.Empty;
    }
};
  • 格式调整说明:
    • "N0":输出带千位分隔符的整数(例:1234 → 1,234)
    • "N2":输出保留两位小数的千位分隔格式(例:1234.56 → 1,234.56)
    • 若需统一跨区域格式,可指定文化信息:numericValue.ToString("N0", CultureInfo.InvariantCulture)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 00:35:06