2013-01-10 5 views
0

J'ai une application Silverlight 5 qui utilise Silverlight Toolkit. Maintenant, le contrôle de graphique Silverlight Toolkit n'affiche pas toujours les valeurs de l'axe X lorsqu'il n'y a qu'un seul résultat dans le jeu de résultats qui renvoie de ma Webserivce.Le contrôle du graphique Silverlight Toolkit n'affiche pas toujours les valeurs de l'axe X

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

La première image montre que mon tableau est correctement chargé lors de la sélection d'un assez grand ResultSet. La deuxième image montre que ce n'est pas le cas lorsque le jeu de résultats existe pour 1 élément.

Ceci est ma mise en œuvre:

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

Avez-vous une idée de? Je n'ai plus de solutions possibles.

+0

Vous définissez la propriété 'Maximum' et la propriété' Minimum' de l'objet DateTimeAxis à la même valeur. – vorrtex

Répondre

1

Une collection avec un élément aura un comportement incorrect à plusieurs endroits de votre code.

ici graphSpan sera égal à zéro:

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

Et ici Maximum et Minimum sera le même:

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

Je vous suggère d'ajouter un autre si bloc et construire un autre axe pour la cas particulier lorsque la collection n'a qu'un seul article.

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

Génial, je vais essayer ce code – Erwin

+0

Eh bien, cela n'a pas fonctionné, mais c'est encore meilleur code que je suis venu avec – Erwin

+0

@Erwin Oui cela fonctionne, je l'ai testé avec une solution vide en utilisant une collection de 1 objet. – vorrtex

Questions connexes