C# Silverlight Toolkit图表控件并不总是显示X轴值
我有一个Silverlight 5应用程序,它使用Silverlight工具包。现在,当从我的webserivce返回的结果集中只有一个结果时,Silverlight Toolkit图表控件并不总是显示X轴值 第一幅图显示,当选择足够大的结果集时,我的图表已正确加载。 第二幅图显示,当1个项目的结果集存在时,它不存在 这是我的实现:C# Silverlight Toolkit图表控件并不总是显示X轴值,c#,silverlight,charts,toolkit,C#,Silverlight,Charts,Toolkit,我有一个Silverlight 5应用程序,它使用Silverlight工具包。现在,当从我的webserivce返回的结果集中只有一个结果时,Silverlight Toolkit图表控件并不总是显示X轴值 第一幅图显示,当选择足够大的结果集时,我的图表已正确加载。 第二幅图显示,当1个项目的结果集存在时,它不存在 这是我的实现: TimeSpan monthSpan = TimeSpan.FromDays(30.0); TimeSpan daySpan = TimeSpan.FromDa
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);
}
}
TimeSpan-monthSpan=TimeSpan.FromDays(30.0);
TimeSpan daySpan=TimeSpan.FromDays(1.0);
TimeSpan hourSpan=TimeSpan.FromHours(1.0);
foreach(e.NewItems中的TagValueResult res)
{
如果(res!=null)
{
LineSeries LineSeries=新的LineSeries()
{
Title=string.Format(“{0}”+Environment.NewLine+“{2}({1})”,res.Name,res.Attributes[“UOM”],res.Attributes[“Description”]),
ItemsSource=资源值,
DependentValueBinding=new System.Windows.Data.Binding(“值”),
IndependentValueBinding=new System.Windows.Data.Binding(“键”),
Tag=res,
PolylineStyle=资源[“thinLineStyle”]作为样式,
//DataPointStyle=资源[“DataPointStyle”]作为样式
};
if(res.Values.Any()&&chart.Series.Any()==false)
{
TimeSpan GraphPan=res.Values.ToList().Last().Key-res.Values.ToList().First().Key;
lineSeries.IndependentAxis=新的日期时间轴
{
最小值=res.Values.ToList().First().Key,
最大值=res.Values.ToList().Last().Key,
间隔=1,
方向=AxisOrientation.X,
位置=AxisLocation.Bottom
};
如果(图幅>月幅)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis).Interval=5;
}
否则如果(图幅>日幅和图幅<月幅)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis.Interval=1;
}
否则,如果(图幅>小时跨度和图幅<天跨度)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Hours;
((DateTimeAxis)lineSeries.IndependentAxis.Interval=1;
}
否则如果(图幅
你有什么想法吗?我没有可能的解决方案。包含一个项的集合在代码的多个位置将有不正确的行为 此处
graphSpan
将等于零:
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,
我建议您添加另一个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;
}
}
var values=res.values.ToList();
TimeSpan graphSpan=values.Last().Key-values.First().Key;
if(graphpan==TimeSpan.Zero)
{
lineSeries.IndependentAxis=新的日期时间轴
{
方向=AxisOrientation.X,
位置=AxisLocation.Bottom
};
}
其他的
{
lineSeries.IndependentAxis=新的日期时间轴
{
最小值=值。第一个()键,
最大值=值。最后一个()键,
方向=AxisOrientation.X,
位置=AxisLocation.Bottom
};
如果(图幅>月幅)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis).Interval=5;
}
否则如果(图幅>日幅和图幅<月幅)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis.Interval=1;
}
否则,如果(图幅>小时跨度和图幅<天跨度)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType=DateTimeIntervalType.Hours;
((DateTimeAxis)lineSeries.IndependentAxis.Interval=1;
}
否则如果(图幅
您将DateTimeAxis对象的最大值
属性和最小值
属性设置为相同的值。虽然它不起作用,但它仍然比我提出的代码要好with@Erwin是的,它可以工作,我用一个空的解决方案测试了它,使用了1个项目的集合。