Search code examples
c#wpfdynamic-data-display

DynamicDataDisplay chart horizontal string axis


I need to draw some charts using DynamicDataDisplay3. Everything works fine, except I can't find a way to change X axis to strings instead of dates or integers. This is how I tried to do it but I get only 1 value on X axis:

int i = 0;
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        i++;
                        Analyze build = new Analyze();
                        build.id = i;
                        build.build = Convert.ToString(reader[0]);
                        builds.Add(build);
                        n1.Add(Convert.ToInt32(reader[1]));
                    }
                }

                var datesDataSource = new EnumerableDataSource<Analyze>(builds);
                datesDataSource.SetXMapping(x => x.id);
                var numberOpenDataSource = new EnumerableDataSource<int>(n1);
                numberOpenDataSource.SetYMapping(y => y);

                CompositeDataSource compositeDataSource1 = new CompositeDataSource(datesDataSource, numberOpenDataSource);
                chBuild.AddLineGraph(compositeDataSource1, new Pen(Brushes.Blue, 2), new CirclePointMarker { Size = 6, Fill = Brushes.Blue }, new PenDescription(Convert.ToString(cmbBuildVertical.SelectedItem)));
                chBuild.Viewport.FitToView();

Solution

  • I made my own LabelProvider to handle something similar to this. I wanted to override my DateTime labels into integers, to represent something different. In your case you could use something like this :

    public class StringLabelProvider : NumericLabelProviderBase {
    
        private List<String> m_Labels;
        public List<String> Labels {
            get { return m_Labels; }
            set { m_Labels = value; }
        }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="ToStringLabelProvider"/> class.
        /// </summary>
        public StringLabelProvider(List<String> labels) {                                                
            Labels = labels;                                    
        }
    
        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 = "";
    
                labelText = Labels[Convert.ToInt32(tickInfo.Tick)];
    
                TextBlock label = (TextBlock)GetResourceFromPool();
                if (label == null) {
                    label = new TextBlock();
                }
    
                label.Text = labelText;
    
                res[i] = label;
    
                ApplyCustomView(tickInfo, label);
            }
            return res;
        }
    }
    

    You can construct your list of ticks, and send it to the LabelProvider you create. Like this :

    StringLabelProvider labelProvider = new StringLabelProvider(yourLabelList);
    yourAxis.LabelProvider = labelProvider;