如何从代码后面显示SfChart

本文关键字:显示 SfChart 代码 | 更新日期: 2023-09-27 18:14:32

我已经在我的代码后面创建了一个SfChart。我得到没有错误,但图表不显示在页面上。我想这可能是我的Xaml的问题,但我不确定。我试着遵循这里的例子。我的问题是类似的,一切构建和部署和步骤通过调试器,我可以看到不同的属性获得数据等。如果有任何帮助就太好了。

XAML:

  <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <Grid x:Name="ChartGrid">
            <syncfusion:SfChart>
            </syncfusion:SfChart>
        </Grid>
  </StackLayout>

背后的代码:

 ChartGrid.BindingContext = theViewModel;
                chart.PrimaryAxis = new DateTimeAxis();
                chart.PrimaryAxis.Title.Text = "Date";
                chart.Legend = new ChartLegend();
                chart.Legend.DockPosition = LegendPlacement.Top;
                chart.Legend.Title.Text = "Test";
                chart.Legend.BindingContext = testnames;
                chart.PrimaryAxis = new CategoryAxis() { Interval = 2, LabelPlacement = LabelPlacement.BetweenTicks };
                chart.SecondaryAxis = new NumericalAxis() { Minimum = 0, Maximum = 100, Interval = 5 };
                chart.Title.Text = "Player test scores";
                chart.Series.Add(new ColumnSeries()
                {
                    ItemsSource = playertestdata,
                    XBindingPath = dates.ToString(),
                    YBindingPath = testscores.ToString()
                });
                ChartGrid.Children.Add(chart);

如何从代码后面显示SfChart

SfChart没有显示,因为网格的布局选项丢失了。我们已经修改了提供的代码片段,它可以在

下面引用。
<Grid x:Name="ChartGrid" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <syncfusion:SfChart > 
            </syncfusion:SfChart>
 </Grid>

我们已经准备了一个样品供你参考,可以在这里下载