【问题标题】:WPF DynamicDataDisplay - Putting strings in Y Axis instead of numbersWPF DynamicDataDisplay - 将字符串放在 Y 轴而不是数字
【发布时间】:2012-11-13 16:45:37
【问题描述】:

使用 WPF(不是 Silverlight)D3,是否可以在 Y 轴上放置字符串? 我特别谈论一种情况,其中我有一个带有状态值的时间线图(假设“高”、“中”和“低”映射为 1、0 和 -1)并且想要 y 轴上的状态记号而不是数字...

谢谢!

【问题讨论】:

    标签: c# wpf string dynamic-data-display


    【解决方案1】:

    您可以使用 LabelProvider 来达到您想要的效果。您将必须创建一个新的 LabelProvider 类,该类源自 LabelProviderBase,或者更具体地说,在您的情况下,NumericLabelProviderBase。您应该能够获取您拥有的所有刻度 (1,0-1),并使用新 LabelProviderClass 中的 if 或开关将它们更改为字符串。标签提供者的源代码中有大量示例可供您用作基础。

    您要覆盖的方法是 CreateLabels。这是我提出的一种快速方法,它应该(希望!)让你到达你需要的地方。在您创建的新类中实现。

    public override UIElement[] CreateLabels(ITicksInfo<double> ticksInfo) {            
    
            var ticks = ticksInfo.Ticks;
            Init(ticks);            
    
            UIElement[] res = new UIElement[ticks.Length];
            LabelTickInfo<double> tickInfo = new LabelTickInfo<double> { Info = ticksInfo.Info };
            for (int i = 0; i < res.Length; i++) {
                tickInfo.Tick = ticks[i];
                tickInfo.Index = i;
                string labelText = "";
    
                if(Convert.ToInt32(tickInfo.Tick) == 1) {
                    labelText = "High";
                } else if(Convert.ToInt32(tickInfo.Tick) == 0) {
                    labelText = "Medium"
                } else if(Convert.ToInt32(tickInfo.Tick) == -1) {
                    labelText = "Low"
                } else {
                    labelText = ""
                }
    
                TextBlock label = (TextBlock)GetResourceFromPool();
                if (label == null) {
                    label = new TextBlock();
                }
    
                label.Text = labelText;
                label.ToolTip = ticks[i].ToString();
    
                res[i] = label;
    
                ApplyCustomView(tickInfo, label);
            }
            return res;
        }
    

    要将这个新的 LabelProvider 分配给 ChartPlotter,请在 XAML 中命名要标记的轴或在 C# 中将其创建为对象:

    yAxis.LabelProvider = new ZazkapulskLabelProvider();
    

    【讨论】:

    • Jason,我应该带你去聘用。再次感谢。你能告诉我如何在 plotter = new ChartPlotter 的定义中使用这个方法吗?
    • 杰森,你的地址是什么?送花到哪里?顺便说一句,你见过这个吗 - stackoverflow.com/questions/13378180/…
    • 哈哈,不用担心!我使用这个库已经有一段时间了,很遗憾看到没有文档。我很高兴帮助那些不得不经历与我一样的挣扎的人!只要您接受答案并可能投一些赞成票,我就很高兴;)
    【解决方案2】:

    不要使用上面的解决方案。使我的 VS 出现许多编译器错误。我在 VS2013 上尝试了 3 个小时的解决方案,但没有奏效。使用函数绘制图形的解决方案:

     dimSeviyesiAxis.LabelProvider.CustomFormatter = (tickInfo) =>
            {
                return "%" + tickInfo.Tick.ToString();
            };
    

    dimSeviyesiAxis 是 xaml 轴名称,轴标签以“%”开头。在返回之前,您可以编写任何条件语句。这是源链接:

    http://dynamicdatadisplay.codeplex.com/discussions/462842

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多