Silverlight Toolkit图表控件并不总是显示X轴值

时间:2013-01-10 12:45:45

标签: c# silverlight charts toolkit

我有一个使用Silverlight Toolkit的Silverlight 5应用程序。现在,当结果集中只有一个结果从我的webserivce返回时,Silverlight Toolkit图表控件并不总是显示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);
    }
}

你有什么想法吗?我没有可能的解决方案。

1 个答案:

答案 0 :(得分: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-block,并在集合只有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;
    }
}