0

我有一个使用 Silverlight Toolkit 的 Silverlight 5 应用程序。现在,当从我的网络服务返回的结果集中只有一个结果时,Silverlight Toolkit 图表控件并不总是显示 X 轴值。

X轴工作!! 结果集中有 1 个项目,X 轴似乎消失了

第一个图像显示,选择足够大结果集时,我的图表已正确加载。第二张图片显示,当结果集存在 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);
    }
}

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

4

1 回答 1

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;
    }
}
于 2013-01-16T21:00:21.030 回答