Skip to content

Commit 45d9c77

Browse files
Updated view model
1 parent 24dd16d commit 45d9c77

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

Cartesian_chart_sample/CartesianChartSample/MainPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
<ContentPage.BindingContext>
11-
<model:ViewModel></model:ViewModel>
11+
<model:PersonViewModel/>
1212
</ContentPage.BindingContext>
1313

1414
<ContentPage.Content>

Cartesian_chart_sample/CartesianChartSample/ViewModel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
namespace CartesianChartSample
88
{
9-
public class ViewModel
9+
public class PersonViewModel
1010
{
11-
public List<Person> Data { get; set; }
12-
public ViewModel()
11+
public List<PersonModel> Data { get; set; }
12+
public PersonViewModel()
1313
{
14-
Data = new List<Person>()
14+
Data = new List<PersonModel>()
1515
{
16-
new Person { Name = "David", Height = 170 },
17-
new Person { Name = "Michael", Height = 96 },
18-
new Person { Name = "Steve", Height = 65 },
19-
new Person { Name = "Joel", Height = 182 },
20-
new Person { Name = "Bob", Height = 134 }
16+
new PersonModel { Name = "David", Height = 170 },
17+
new PersonModel { Name = "Michael", Height = 96 },
18+
new PersonModel { Name = "Steve", Height = 65 },
19+
new PersonModel { Name = "Joel", Height = 182 },
20+
new PersonModel { Name = "Bob", Height = 134 }
2121
};
2222
}
2323

2424
}
25-
public class Person
25+
public class PersonModel
2626
{
2727
public string Name { get; set; }
2828
public double Height { get; set; }

README.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ namespace ChartGettingStarted
2727
public MainPage()
2828
{
2929
InitializeComponent();
30+
Grid grid = new Grid();
3031
SfCartesianChart chart = new SfCartesianChart();
32+
grid.Add(chart);
33+
this.Content = grid;
3134
}
3235
}
3336
}
@@ -71,36 +74,36 @@ namespace ChartGettingStarted
7174
Now, let us define a simple data model that represents a data point in the chart.
7275
######
7376
```C#
74-
public class Person
77+
public class PersonModel
7578
{
7679
public string Name { get; set; }
7780
public double Height { get; set; }
7881
}
7982
```
80-
Next, create a view model class and initialize a list of `Person` objects as follows.
83+
Next, create a PersonViewModel class and initialize a list of `PersonModel` objects as follows.
8184
###### C#
8285
```C#
83-
public class ViewModel
86+
public class PersonViewModel
8487
{
85-
public List<Person> Data { get; set; }
88+
public List<PersonModel> Data { get; set; }
8689

87-
public ViewModel()
90+
public PersonViewModel()
8891
{
89-
Data = new List<Person>()
92+
Data = new List<PersonModel>()
9093
{
91-
new Person { Name = "David", Height = 170 },
92-
new Person { Name = "Michael", Height = 96 },
93-
new Person { Name = "Steve", Height = 65 },
94-
new Person { Name = "Joel", Height = 182 },
95-
new Person { Name = "Bob", Height = 134 }
94+
new PersonModel { Name = "David", Height = 170 },
95+
new PersonModel { Name = "Michael", Height = 96 },
96+
new PersonModel { Name = "Steve", Height = 65 },
97+
new PersonModel { Name = "Joel", Height = 182 },
98+
new PersonModel { Name = "Bob", Height = 134 }
9699
};
97100
}
98101
}
99102
```
100103

101-
Set the `ViewModel` instance as the `BindingContext` of your page to bind `ViewModel` properties to the chart.
104+
Set the `PersonViewModel` instance as the `BindingContext` of your page to bind `PersonViewModel` properties to the chart.
102105

103-
* Add namespace of `ViewModel` class to your XAML Page, if you prefer to set `BindingContext` in XAML.
106+
* Add namespace of `PersonViewModel` class to your XAML Page, if you prefer to set `BindingContext` in XAML.
104107
###### Xaml
105108
```xaml
106109
<ContentPage
@@ -111,7 +114,7 @@ Set the `ViewModel` instance as the `BindingContext` of your page to bind `ViewM
111114
xmlns:model="clr-namespace:ChartGettingStarted">
112115

113116
<ContentPage.BindingContext>
114-
<model:ViewModel></model:ViewModel>
117+
<model:PersonViewModel/>
115118
</ContentPage.BindingContext>
116119
</ContentPage>
117120
```
@@ -153,7 +156,7 @@ Run the project and check if you get following output to make sure you have conf
153156

154157
## Populate Chart with data
155158

156-
As we are going to visualize the comparison of heights in the data model, add [ColumnSeries](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ColumnSeries.html?tabs=tabid-1) to [Series](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.SfCartesianChart.html#Syncfusion_Maui_Charts_SfCartesianChart_Series) property of chart, and then bind the `Data` property of the above `ViewModel` to the `ColumnSeries.ItemsSource` as follows.
159+
As we are going to visualize the comparison of heights in the data model, add [ColumnSeries](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ColumnSeries.html?tabs=tabid-1) to [Series](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.SfCartesianChart.html#Syncfusion_Maui_Charts_SfCartesianChart_Series) property of chart, and then bind the `Data` property of the above `PersonViewModel` to the `ColumnSeries.ItemsSource` as follows.
157160

158161
* The cartesian chart has [Series](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.SfCartesianChart.html#Syncfusion_Maui_Charts_SfCartesianChart_Series) as its default content.
159162
* You need to set [XBindingPath](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.ChartSeries.html#Syncfusion_Maui_Charts_ChartSeries_XBindingPath) and [YBindingPath](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Charts.XYDataSeries.html#Syncfusion_Maui_Charts_XYDataSeries_YBindingPath)
@@ -200,7 +203,7 @@ chart.YAxes.Add(secondaryAxis);
200203
ColumnSeries series = new ColumnSeries();
201204
series.Label = "Height";
202205
series.ShowDataLabels = true;
203-
series.ItemsSource = (new ViewModel()).Data;
206+
series.ItemsSource = (new PersonViewModel()).Data;
204207
series.XBindingPath = "Name";
205208
series.YBindingPath = "Height";
206209

@@ -279,17 +282,17 @@ The legend provides information about the data point displayed in the chart. The
279282
<chart:SfCartesianChart>
280283
. . .
281284
<chart:ColumnSeries Label="Height"
282-
ItemsSource="{Binding Data}"
283-
XBindingPath="Name"
284-
YBindingPath="Height">
285+
ItemsSource="{Binding Data}"
286+
XBindingPath="Name"
287+
YBindingPath="Height">
285288
</chart:ColumnSeries>
286289
</chart:SfCartesianChart>
287290
```
288291

289292
###### C#
290293
```C#
291294
ColumnSeries series = new ColumnSeries ();
292-
series.ItemsSource = (new ViewModel()).Data;
295+
series.ItemsSource = (new PersonViewModel()).Data;
293296
series.XBindingPath = "Name";
294297
series.YBindingPath = "Height";
295298
series.Label = "Height";
@@ -314,7 +317,7 @@ Tooltips are used to show information about the segment, when a user hovers over
314317
###### C#
315318
```C#
316319
ColumnSeries series = new ColumnSeries();
317-
series.ItemsSource = (new ViewModel()).Data;
320+
series.ItemsSource = (new PersonViewModel()).Data;
318321
series.XBindingPath = "Name";
319322
series.YBindingPath = "Height";
320323
series.EnableTooltip = true;
@@ -332,7 +335,7 @@ The following code example gives you the complete code of above configurations.
332335
xmlns:model="clr-namespace:ChartGettingStarted">
333336

334337
<ContentPage.BindingContext>
335-
<model:ViewModel></model:ViewModel>
338+
<model:PersonViewModel/>
336339
</ContentPage.BindingContext>
337340

338341
<ContentPage.Content>
@@ -411,7 +414,7 @@ namespace ChartGettingStarted
411414
{
412415
Label = "Height",
413416
ShowDataLabels = true,
414-
ItemsSource = (new ViewModel()).Data,
417+
ItemsSource = (new PersonViewModel()).Data,
415418
XBindingPath = "Name",
416419
YBindingPath = "Height",
417420
DataLabelSettings = new CartesianDataLabelSettings

0 commit comments

Comments
 (0)