Search code examples
c#wpfstringdynamic-data-display

WPF DynamicDataDisplay - Putting strings in Y Axis instead of numbers


Using WPF (not Silverlight) D3, is it possible to put strings on the Y axis? I am specifically talking about a situation in which I have a graph of a timeline with status values (let's say "High" and "Medium" and "Low" mapped as 1, 0 and -1) and want the status on the y axis ticks instead of the numbers...

Thanks!


Solution

  • You can use a LabelProvider to achieve your desired effect. You will have to make a new LabelProvider class that stems off of either LabelProviderBase, or more specifically in your case, NumericLabelProviderBase. You should be able to take all the ticks you have (1,0-1) and change them into strings using an if or a switch in your new LabelProviderClass. There's tons of examples in the source of label providers you can use as a base.

    The method you are looking to override is CreateLabels. Here is a quick method I whipped up that should (hopefully!) get you where you need to be. Implement in the new class you create.

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

    To assign this new LabelProvider to a ChartPlotter, name the axis that you want to label in XAML or create it as an object in C# :

    yAxis.LabelProvider = new ZazkapulskLabelProvider();