WPF livecharts 折线图遮挡数字问题

在WPF里使用livecharts,如果折线图或者柱状图有多个的时候,可能会出现两个数字遮挡问题,这时候要设置DataLabelsTemplate 属性。

如LineSeries设置代码如下:

第一个折线图的DataLabelsTemplate

cs 复制代码
var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
var textBlockFactoryA = new FrameworkElementFactory(typeof(TextBlock));
textBlockFactoryA.SetValue(TextElement.ForegroundProperty, new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5BABEF")));
textBlockFactoryA.SetBinding(TextBlock.TextProperty, new Binding("FormattedText"));
textBlockFactoryA.SetValue(TextBlock.MarginProperty, new Thickness(0, 0, 0, -8));
stackPanelFactory.AppendChild(textBlockFactoryA);
var dataTemplate = new DataTemplate() { VisualTree = stackPanelFactory };

第二个折线图的DataLabelsTemplate

cs 复制代码
 var stackPanelFactory2 = new FrameworkElementFactory(typeof(StackPanel));
 stackPanelFactory2.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
 var textBlockFactoryA2 = new FrameworkElementFactory(typeof(TextBlock));
 textBlockFactoryA2.SetValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));
 textBlockFactoryA2.SetBinding(TextBlock.TextProperty, new Binding("FormattedText"));
 textBlockFactoryA2.SetValue(TextBlock.MarginProperty, new Thickness(0, 0, 0, -40));
 stackPanelFactory2.AppendChild(textBlockFactoryA2);
 var dataTemplate2 = new DataTemplate() { VisualTree = stackPanelFactory2 };

设置LineSeries

cs 复制代码
Func<double, string> Formatter;
SeriesCollection = new SeriesCollection
{
    new LineSeries
    {
        Title = "",
        DataLabels=true,
        //注意设置在这里
        DataLabelsTemplate= dataTemplate,
        Values = inValueList,
        Stroke=new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5BABEF")),
        Foreground= new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5BABEF")),
        FontSize=26,
    },
    new LineSeries
    {
        Title="",
        DataLabels=true,
        //注意设置在这里
        DataLabelsTemplate= dataTemplate2,
        Values= outValueList,
        Stroke =new SolidColorBrush(Colors.White),
        Foreground= new SolidColorBrush(Colors.White),
        FontSize=26,
    },

};

Formatter = value => value.ToString("N");
lvcWeekLabel.Labels = Labels;
lvcWeekLabel.LabelFormatter = Formatter;
lvcWeek.Series = SeriesCollection;

具体位置可能要根据你的图表大小调整,主要就是调整

textBlockFactoryA2.SetValue(TextBlock.MarginProperty, new Thickness(0, 0, 0, -40));

这句话。另外字体大小什么的也可以根据需求调整

相关推荐
我是苏苏12 小时前
WPF开发01:基础控件及常用模板篇
开发语言·c#·html·wpf
dalong101 天前
WPF:3D顶点共享与法线设置对光照效果的影响
3d·wpf
柔蘭2 天前
WPF中的CommunityToolkit.Mvvm框架
wpf
aGdF8E3gQ2 天前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
程序员-Benothing3 天前
如何实现高并发系统订单30分钟自动关闭?
开发语言·c#·wpf
一水3 天前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
dalong103 天前
WPF:3D甜甜圈
3d·c#·wpf·甜甜圈
别催小唐敲代码4 天前
ROS2从入门到实战:去中心化机器人操作系统详解
wpf·ros2
脚踏实地皮皮晨4 天前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨4 天前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio