2013-01-10 21 views
0

我有一個使用Silverlight工具包的Silverlight 5應用程序。現在,當從我的webserivce返回的結果集中只有一個結果時,Silverlight Toolkit圖表控件並不總是顯示X軸值。Silverlight工具包圖表控件並不總是顯示X軸值

X-Axis works!! With 1 item in the resultset, the X-Axis seems to disappear

第一張圖片顯示的是選擇一個足夠大的結果集時,我的表是否正確裝入。 第二張圖片顯示,當結果集存在1項時,它不會。

這是我實現:

TimeSpan monthSpan = TimeSpan.FromDays(30.0); 
TimeSpan daySpan = TimeSpan.FromDays(1.0); 
TimeSpan hourSpan = TimeSpan.FromHours(1.0); 

foreach (TagValueResult res in e.NewItems) 
{ 
    if (res != null) 
    { 
     LineSeries lineSeries = new LineSeries() 
     { 
      Title = string.Format("{0}" + Environment.NewLine + " {2} ({1})", res.Name, res.Attributes["UOM"], res.Attributes["Description"]), 
      ItemsSource = res.Values, 
      DependentValueBinding = new System.Windows.Data.Binding("Value"), 
      IndependentValueBinding = new System.Windows.Data.Binding("Key"), 
      Tag = res, 
      PolylineStyle = Resources["thinLineStyle"] as Style, 
      //DataPointStyle = Resources["dataPointStyle"] as Style 
     }; 
     if (res.Values.Any() && chart.Series.Any() == false) 
     { 
      TimeSpan graphSpan = res.Values.ToList().Last().Key - res.Values.ToList().First().Key; 

      lineSeries.IndependentAxis = new DateTimeAxis 
      { 
       Minimum = res.Values.ToList().First().Key, 
       Maximum = res.Values.ToList().Last().Key, 
       Interval = 1, 
       Orientation = AxisOrientation.X, 
       Location = AxisLocation.Bottom 
      }; 

      if (graphSpan > monthSpan) 
      { 
       ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
       ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 5; 
      } 
      else if (graphSpan > daySpan && graphSpan < monthSpan) 
      { 
       ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
       ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
      } 
      else if (graphSpan > hourSpan && graphSpan < daySpan) 
      { 
       ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Hours; 
       ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
      } 
      else if (graphSpan < hourSpan) 
      { 
       ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Minutes; 
       ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 15; 
      } 
      else 
      { 
       //sometimes all comparisons fail, just back up to a safe interval of 1 day. 
       ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
       ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
      } 
     } 
     chart.Series.Add(lineSeries); 
    } 
} 

你有任何想法的?我沒有可能的解決方案。

+0

您將DateTimeAxis對象的'Maximum'屬性和'Minimum'屬性設置爲相同的值。 – vorrtex

回答

1

一個項目的集合在你的代碼的幾個地方會有不正確的行爲。

這裏graphSpan將等於零:

TimeSpan graphSpan = res.Values.ToList().Last().Key - res.Values.ToList().First().Key; 

這裏MaximumMinimum將是相同的:

lineSeries.IndependentAxis = new DateTimeAxis 
{ 
    Minimum = res.Values.ToList().First().Key, 
    Maximum = res.Values.ToList().Last().Key, 

我建議你添加一個if區塊並構建了不同的軸當收集只有1件物品時的特殊情況。

var values = res.Values.ToList(); 
TimeSpan graphSpan = values.Last().Key - values.First().Key; 

if (graphSpan == TimeSpan.Zero) 
{ 
    lineSeries.IndependentAxis = new DateTimeAxis 
    { 
     Orientation = AxisOrientation.X, 
     Location = AxisLocation.Bottom 
    }; 
} 
else 
{ 
    lineSeries.IndependentAxis = new DateTimeAxis 
    { 
     Minimum = values.First().Key, 
     Maximum = values.Last().Key, 
     Orientation = AxisOrientation.X, 
     Location = AxisLocation.Bottom 
    }; 

    if (graphSpan > monthSpan) 
    { 
     ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
     ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 5; 
    } 
    else if (graphSpan > daySpan && graphSpan < monthSpan) 
    { 
     ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
     ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
    } 
    else if (graphSpan > hourSpan && graphSpan < daySpan) 
    { 
     ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Hours; 
     ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
    } 
    else if (graphSpan < hourSpan) 
    { 
     ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Minutes; 
     ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 15; 
    } 
    else 
    { 
     //sometimes all comparisons fail, just back up to a safe interval of 1 day. 
     ((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days; 
     ((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1; 
    } 
} 
+0

真棒,我會試試這個代碼 – Erwin

+0

嗯,它沒有工作,但它仍然是比我更好的代碼 – Erwin

+0

@Erwin是的,它使用一個空的解決方案,通過使用一個項目的集合測試它。 – vorrtex

相關問題