2013-03-19 70 views
1

我需要使用DynamicDataDisplay3繪製一些圖表。一切工作正常,除了我找不到一種方法來將X軸更改爲字符串而不是日期或整數。這是我想做到這一點,但我只得到1 X軸值:DynamicDataDisplay圖表水平字符串軸

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

回答

2

我做我自己的LabelProvider來處理類似這樣的東西。我想將我的DateTime標籤覆蓋爲整數,以表示不同的東西。你的情況,你可以使用這樣的事情:

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

您可以構建你的蜱名單,並將其發送到您所創建的LabelProvider。像這樣:

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