我是Silverlight开发的新手,目前正在进入Charting领域。 我一直在关注几个教程,这些教程展示了使用ItemsSource(http://silverlight.net/forums/t/44166.aspx)将ColumnSeries绑定到数据源是多么容易。
我是以编程方式在画布上添加图表。
Chart BudgetChart = new Chart { Title = "budget", MaxHeight= 200, MaxWidth=500};
ColumnSeries cs = new ColumnSeries();
BudgetChart.Series.Add(cs);
cs.Title = "blarg";
cs.ItemsSource = o.Budget; //returns List<Budget>
cs.IndependentValueBinding = new System.Windows.Data.Binding("Budget");
cs.DependentValueBinding = new System.Windows.Data.Binding("Year");
此代码编译良好。但是,当我调试它时,这个错误就是抛出 但是,我一直在使用ItemsSource遇到很多困难。每次我分配ItemsSource时,都会收到一条错误,指出它是Sys.InvalidOperationException:控件'Xaml1'中的ManagedRuntimeError错误#4004:System.NullReferenceException System.NullReferenceException:对象未设置为对象的实例。在System.Windows.Control.DataVisualization.Charting.ColumnSeries。&lt;&gt; c__DisplayClass8.b__4()
请帮忙,这个错误让我发疯了!!!
答案 0 :(得分:1)
尝试将ItemsSource分配移动到绑定之后。在配置其绑定后,还要将系列添加到图表中。
Chart BudgetChart = new Chart { Title = "budget", MaxHeight= 200, MaxWidth=500};
ColumnSeries cs = new ColumnSeries();
cs.Title = "blarg";
cs.IndependentValueBinding = new System.Windows.Data.Binding("Budget");
cs.DependentValueBinding = new System.Windows.Data.Binding("Year");
BudgetChart.Series.Add(cs);
cs.ItemsSource = o.Budget; //returns List<Budget>
答案 1 :(得分:0)
确保您的第一个数据点在从属值中不包含null / Nothing。 当图表试图“嗅探”所需的从属轴类型时会发生错误,并且失败。
at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.GetAxes(DataPoint firstDataPoint, Func`2 independentAxisPredicate, Func`1 independentAxisFactory, Func`2 dependentAxisPredicate, Func`1 dependentAxisFactory)
在System.Windows.Controls.DataVisualization.Charting.ColumnSeries.GetAxes(DataPoint firstDataPoint)
似乎没有解决方法。一种选择是子类化一个覆盖CanPlot(值)的LinearAxis。目前NumericAxis.CanPlot因值== null
而失败 /// <summary>
/// Returns a value indicating whether a value can plot.
/// </summary>
/// <param name="value">The value to plot.</param>
/// <returns>A value indicating whether a value can plot.</returns>
public override bool CanPlot(object value)
{
double val;
return ValueHelper.TryConvert(value, out val);
}
答案 2 :(得分:0)
<toolkit:Chart Grid.Row="1" Margin="10" Name="columnChart" Grid.Column="0" Width="400" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<toolkit:Chart.Series>
<toolkit:ColumnSeries
IndependentValueBinding="{Binding Path=Value}"
DependentValueBinding="{Binding Path=Key}" IsHitTestVisible="True" IsSelectionEnabled="True" SelectionChanged="ColumnSeries_SelectionChanged">
</toolkit:ColumnSeries>
</toolkit:Chart.Series>
<toolkit:Chart.Template>
<ControlTemplate TargetType="toolkit:Chart">
<chartingPrimitivesToolkit:EdgePanel x:Name="ChartArea">
<Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
<Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
</chartingPrimitivesToolkit:EdgePanel>
</ControlTemplate>
</toolkit:Chart.Template>
<toolkit:Chart.Axes>
<toolkit:LinearAxis Orientation="Y" Minimum="0" Maximum="100" Interval="10" />
</toolkit:Chart.Axes>
</toolkit:Chart>
List<KeyValuePair<int, string>> lkvpGraph1 = new List<KeyValuePair<int, string>> { };
KeyValuePair<int, string> kvpGraph1 = new KeyValuePair<int, string>();
List<string> lstJob = new List<string>();
lstJob = lR1Acceptance.Where(r1 => r1.FCode == Facility).Select(r1 => r1.Job_No).Distinct().ToList();
double nWeeks = Math.Round((dtToDate - dtFromDate).Days / 7.0, 0);
nWeeks++;
foreach (string job_no in lstJob)
{
int TotalSubmission = lR1Acceptance.Where(rr => rr.Job_No == job_no).Count();
int TotalR1Accepted = lR1Acceptance.Where(rr => rr.Round_No == 1 && rr.Result == "A" && rr.Job_No == job_no).Count();
int dAcceptance = (int)Math.Round(TotalR1Accepted * 100.0 / TotalSubmission, 0);
if (TotalSubmission != 0)
{
kvpGraph1 =
new KeyValuePair<int, string>(dAcceptance, job_no);
lkvpGraph1.Add(kvpGraph1);
}
}
((ColumnSeries)columnChart.Series[0]).ItemsSource = lkvpGraph1;