• WPF livecharts 折线图遮挡数字问题


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

    如LineSeries设置代码如下:

    第一个折线图的DataLabelsTemplate

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

    第二个折线图的DataLabelsTemplate

    1. var stackPanelFactory2 = new FrameworkElementFactory(typeof(StackPanel));
    2. stackPanelFactory2.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
    3. var textBlockFactoryA2 = new FrameworkElementFactory(typeof(TextBlock));
    4. textBlockFactoryA2.SetValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));
    5. textBlockFactoryA2.SetBinding(TextBlock.TextProperty, new Binding("FormattedText"));
    6. textBlockFactoryA2.SetValue(TextBlock.MarginProperty, new Thickness(0, 0, 0, -40));
    7. stackPanelFactory2.AppendChild(textBlockFactoryA2);
    8. var dataTemplate2 = new DataTemplate() { VisualTree = stackPanelFactory2 };

    设置LineSeries

    1. Func<double, string> Formatter;
    2. SeriesCollection = new SeriesCollection
    3. {
    4. new LineSeries
    5. {
    6. Title = "",
    7. DataLabels=true,
    8. //注意设置在这里
    9. DataLabelsTemplate= dataTemplate,
    10. Values = inValueList,
    11. Stroke=new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5BABEF")),
    12. Foreground= new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5BABEF")),
    13. FontSize=26,
    14. },
    15. new LineSeries
    16. {
    17. Title="",
    18. DataLabels=true,
    19. //注意设置在这里
    20. DataLabelsTemplate= dataTemplate2,
    21. Values= outValueList,
    22. Stroke =new SolidColorBrush(Colors.White),
    23. Foreground= new SolidColorBrush(Colors.White),
    24. FontSize=26,
    25. },
    26. };
    27. Formatter = value => value.ToString("N");
    28. lvcWeekLabel.Labels = Labels;
    29. lvcWeekLabel.LabelFormatter = Formatter;
    30. lvcWeek.Series = SeriesCollection;

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

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

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

  • 相关阅读:
    【服务器数据恢复】RAID6中3块磁盘离线崩溃的数据恢复案例
    IPv6与VoIP——配置Cisco CME实现VoIP实验
    使用IDEA开发JavaWeb项目的基本配置最新教程
    【群晖NAS】Synology drive套件安装设置与结合内网穿透实现远程访问——“cpolar内网穿透”
    1. @Component注解的原理剖析
    Linux安装MySQL【Ubuntu20.04】
    速卖通、lazada店铺增加销量有哪些技巧,你知道吗?
    C 风格的枚举还存在 “成员名称全局有效” 和 “可以隐式转换为整型” 的缺陷
    关于 eBPF 安全可观测性,你需要知道的那些事儿
    linux挖矿病毒kthreaddk横行,如何灭掉它?
  • 原文地址:https://blog.csdn.net/jsrgckf/article/details/133684901