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));

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

相关推荐
程序员小刘22 分钟前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans14 小时前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽1 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf
code bean2 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go2 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19722 天前
自定义事件wpf
wpf
code bean2 天前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静3 天前
WPF可拖拽ListView
c#·wpf
界面开发小八哥3 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon4 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf