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

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

相关推荐
军训猫猫头10 小时前
5.浏览本地文件获取路径与文件名称 C#例子 WPF例子
开发语言·c#·wpf
步、步、为营18 小时前
.NET + WPF框架开发聊天、网盘、信息发布、视频播放功能
.net·wpf·音视频
code bean1 天前
【WPF实战】MVVM中如何从数据模型反查自定义控件实例(ImageView + Halcon)
wpf
lph19721 天前
ValueConverter转换器WPF
wpf
Sally璐璐1 天前
Memcache核心技术解析与实战应用
运维·wpf·memcached
悟能不能悟2 天前
Dubbo跨越分布式事务的最终一致性陷阱
分布式·wpf·dubbo
I'mSQL2 天前
C#与FX5U进行Socket通信
运维·服务器·自动化·wpf
观无3 天前
关于wpf的自适应
wpf
时光追逐者3 天前
一款开源免费、通用的 WPF 主题控件包
开源·c#·.net·wpf
甜甜不吃芥末4 天前
Windows 应用程序的 UI 框架:WPF、WinUI 3 和 UWP的差异区别
windows·ui·wpf