银光图表工具包中的ColumnSeries问题

3
我是新手,正在涉足Silverlight开发,并且现在正在接触图表制作。我一直在跟随几个教程学习如何使用ItemsSource将ColumnSeries绑定到数据源上(http://silverlight.net/forums/t/44166.aspx)。
我正在通过编程方式向画布添加图表。
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.<>c__DisplayClass8.b__4()。

请帮帮我,这个错误让我疯了!!!

3个回答

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>

嗨,AnthonyWJones, 感谢您的回复。我尝试按照您建议的方式操作,但问题仍然存在。还有其他建议吗? - hantu7
如果需要帮助的话,我正在使用工具包的三月发布版本。 - hantu7
我终于学会了如何阅读。我之前绑定错误了,混淆了依赖和独立绑定。感谢您的帮助AnthonyWJones。 - hantu7

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)中

似乎没有解决方法。一个选项是子类化LinearAxis,覆盖CanPlot(value)。目前NumericAxis.CanPlot对于value==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);
    }

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接