syncfusion图表不显示数据 [英] syncfusion chart does not display data

查看:24
本文介绍了syncfusion图表不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了 SFchart,它没有错误并且可以编译.它显示一个空的图表视图.我在 Xamarin.IOS 中使用 MVVMcross

I added SFchart, it had no errors and it compiles. It shows an empty chartview. I'm using MVVMcross in Xamarin.IOS

我请求的数据在那里,它包含大约 200 行,数据是从我的 api 使用方法 override void viewAppearing 请求的.

The data I requested is there, it contains about 200 rows, the data is requested from my api with the method override void viewAppearing.

我在 viewdidload 中的视图:

My view in viewdidload:

 //Initialize the Chart with required frame. This frame can be any rectangle, which bounds inside the view.
        SFChart chart = new SFChart();
        chart.Frame = this.headerView.Frame;

        //Adding Primary Axis for the Chart.
        SFCategoryAxis primaryAxis = new SFCategoryAxis();
        chart.PrimaryAxis = primaryAxis;

        //Adding Secondary Axis for the Chart.
        SFNumericalAxis secondaryAxis = new SFNumericalAxis();
        chart.SecondaryAxis = secondaryAxis;
        chart.Series.Add(new SFColumnSeries()
        {

            ItemsSource = (this.ViewModel as UserCoinViewModel).CoinHistory,

            XBindingPath = "price_btc",

            YBindingPath = "timestamp"

        });


        this.View.AddSubview(chart);

视图模型:

private List<CoinHistoryModel> _CoinHistory;

    public List<CoinHistoryModel> CoinHistory
    {
        get
        {
            return _CoinHistory;
        }
        set
        {
            _CoinHistory = value;
            RaisePropertyChanged(() => CoinHistory);
        }
    }

推荐答案

因为你使用的是 MVVMcross,所以你应该使用 bind 方法来设置你的 SeriesItemsSource.您只需将 ItemsSource 设置为 viewModel 实例的属性,当值更改时它不会通知视图.所以它似乎显示了一个空图表.

Because you are using MVVMcross, you should use bind method to set your Series's ItemsSource. You just set the ItemsSource to the viewModel instance's property, when the value changed it will not notify the View. So it seems to show an empty chart.

修改您的代码以绑定如下:

Modify your code to bind like:

SFColumnSeries series = new SFColumnSeries()
{
    XBindingPath = "price_btc",

    YBindingPath = "timestamp"
};
chart.Series.Add(series);

var set = this.CreateBindingSet<YourView, UserCoinViewModel>();
...
        set.Bind(series).For(s => s.ItemsSource).To(vm => vm.CoinHistory);
...
set.Apply();

然后在您的 ViewModel 中,创建如下命令:

Then in your ViewModel, create a command like:

private MvxCommand loadDataCommand;
public ICommand LoadDataCommand
{
    get
    {
        return loadDataCommand ?? (loadDataCommand = new MvxCommand(ExecuteloadDataCommand));
    }
}
private void ExecuteloadDataCommand()
{
    CoinHistory = new List<CoinHistoryModel>()
    {
        new CoinHistoryModel{ price_btc = "First", timestamp = 10 },
        new CoinHistoryModel{ price_btc = "Second", timestamp = 20 },
        new CoinHistoryModel{ price_btc = "Third", timestamp = 30 }
    };
    // Change it to your data request here
}

最后在你的 ViewWillAppear() 事件中触发这个命令:

At last trigger this command in your ViewWillAppear() event:

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    (ViewModel as UserCoinViewModel).LoadDataCommand.Execute(null);
    // You can also bind a button to this command to trigger it manually.
}

这篇关于syncfusion图表不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆