I am trying to make a method which when called adds a SFChart in a ListView (which may or may not already have charts in it) with all the data being taken from a json (the json is mapped using json.NET).
I've uploaded the method that is supposed to add the chart (uploaded as addFunc.PNG), the XAML (Xaml.png), the JSON(json.png) and the models (models.PNG,Command.PNG and chartData.PNG). I am having a hard time figuring out where the logic is falling apart and would appreciate if someone can shed some light.
MainPage:
<ContentPage>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Text="Add Chart" Clicked="Button_Clicked"/>
<syncfusion:SfListView x:Name="listView" Grid.Row="1"
ItemSize="120" BackgroundColor="#FFE8E8EC"
DragStartMode="OnHold,OnDragIndicator"
SelectionMode="None">
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<Frame HasShadow="True" BackgroundColor="White" Padding="0"
InputTransparent="true">
<chart:SfChart HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis/>
</chart:SfChart.PrimaryAxis>
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis/>
</chart:SfChart.SecondaryAxis>
<chart:LineSeries ItemsSource="{Binding Datas}" XBindingPath="Country" YBindingPath="Count"/>
</chart:SfChart>
</Frame>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>
</Grid>
</ContentPage>
Model
public class FullModel
{
public String Message;
public String status;
public Model[] Gold;
public FullModel()
{
}
}
public class Model
{
public Model()
{ }
public Model(string name, double count)
{
Country = name;
Count = count;
}
}
public class ChartModel
{
public ObservableCollection<Model> Datas { get; set; }
public ChartModel(ObservableCollection<Model> data)
{
Datas = data;
}
}
ViewModel
public class ViewModel
{
public ObservableCollection<ChartModel> ChartData
{
get;
set;
}
public FullModel Items
{
get; private set;
}
public ViewModel()
{
string json = @"{'Gold':[{'Country':'USA','Count':46},{'Country':'China','Count':36},{'Country':'Japan','Count':63},{'Country':'Australia','Count':54}]}";
Items= JsonConvert.DeserializeObject<FullModel>(json);
ChartData = new ObservableCollection<ChartModel>();
Random r = new Random();
for (int i=0;i<4;i++)
{
AddItem();
}
}
public void AddItem()
{
var data = new ChartModel(new ObservableCollection<Model>()
{
new Model()
{
Country=Items.Gold[0].department,
Count=Items.Gold[0].count
},
new Model()
{
Country=Items.Gold[1].department,
Count=Items.Gold[1].count
},
new Model()
{
Country=Items.Gold[2].department,
Count=Items.Gold[2].count
},
new Model()
{
Country=Items.Gold[3].department,
Count=Items.Gold[3].count
}
});
ChartData.Add(data);
}
}
|
public partial class MainPage : ContentPage
{
ViewModel viewModel;
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
viewModel = (listView.BindingContext as ViewModel);
Random r = new Random();
viewModel.ChartData.Clear();
InitializeTimer();
}
private async void InitializeTimer()
{
await WaitAndExecute(1000, () =>
{
viewModel.AddItem();
});
}
private async Task WaitAndExecute(int milisec, Action actionToExecute)
{
await Task.Delay(1000);
actionToExecute();
}
}
|