Skip to content

Commit 4bb22f5

Browse files
Sample for Tooltip template for grouped values.
1 parent 6797830 commit 4bb22f5

File tree

124 files changed

+15306
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+15306
-1
lines changed

README.md

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,168 @@
1-
# How-to-bind-the-Xamarin-Pie-Chart-Tooltip-to-Others-category-values
1+
# How-to-bind-the-Xamarin-Pie-Chart-Tooltip-to-Others-category-values
2+
3+
We always like to group smaller pie chart values into the category "Others" to increase chart readability. But we also think about ways to show those grouped values in UI.
4+
5+
In this article, we will discuss in detail “How to show grouped values of Xamarin.Forms Pie Chart using tooltip?” By the following ways, we can display the average of clustered values, either can display individual value present in it.
6+
7+
## How to show sum of values that grouped at Xamarin Pie Chart
8+
9+
The example of the code below shows “How to calculate and display the average of grouped value using tooltip template?”
10+
11+
12+
### Step 1: Declaration IValueConverter to calculate average values.
13+
14+
{% tabs %}
15+
16+
public class ChartAvgValueConverter : IValueConverter
17+
{
18+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
19+
{
20+
if (value != null)
21+
{
22+
//Return string value.
23+
if (parameter != null && parameter.ToString() == "Label")
24+
{
25+
return value is List<object> ? "Others" : (value as DataModel).XData;
26+
}
27+
else
28+
{
29+
//Return average value.
30+
if (value is List<object>)
31+
{
32+
var collection = value as List<object>;
33+
var sum = collection.Sum(item => (item as DataModel).YData);
34+
return sum / collection.Count;
35+
}
36+
else
37+
{
38+
return (value as DataModel).YData;
39+
}
40+
}
41+
}
42+
43+
return null;
44+
}
45+
46+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
47+
{
48+
return value;
49+
}
50+
}
51+
52+
{% endhighlight %}
53+
54+
### Step 2: DataTemplate declarations.
55+
56+
{% tabs %}
57+
<ContentView.Resources>
58+
<local:ChartAvgValueConverter x:Key="sumOfValuesConverter"/>
59+
<DataTemplate x:Key="TooltipTemplate">
60+
<StackLayout Orientation="Horizontal">
61+
<Label Text="{Binding Converter={StaticResource sumOfValuesConverter}, ConverterParameter='Label'}". . />
62+
<Label Text="{Binding Converter={StaticResource sumOfValuesConverter}}" ../>
63+
</StackLayout>
64+
</DataTemplate>
65+
</ContentView.Resources>
66+
{% endhighlight %}
67+
68+
### Step 3: DataTemplate defined in the Series Tooltip Template.
69+
70+
{% tabs %}
71+
<chart:SfChart BackgroundColor="Transparent">
72+
. . .
73+
<chart:SfChart.Series>
74+
<chart:PieSeries
75+
ItemsSource="{Binding MonthlyExpenses}"
76+
XBindingPath="XData"
77+
YBindingPath="YData"
78+
EnableTooltip="true"
79+
GroupMode="Value"
80+
GroupTo="50"
81+
TooltipTemplate="{StaticResource TooltipTemplate}"
82+
>
83+
84+
</chart:PieSeries>
85+
</chart:SfChart.Series>
86+
<chart:SfChart.ChartBehaviors>
87+
<chart:ChartTooltipBehavior BackgroundColor="LightBlue
88+
</chart:SfChart.ChartBehaviors>
89+
</chart:SfChart>
90+
{% endhighlight %}
91+
92+
## How to bind the Xamarin.Forms Pie Chart grouped data collection to the Tooltip
93+
94+
The example of the code below shows “How to display the values which present inside the group ‘Others’?”
95+
96+
Step 1: Declaration IValueConverter to generate BindableLayout ItemsSource
97+
98+
{% tabs %}
99+
public class ChartValueConverter : IValueConverter
100+
{
101+
//Which returns ItemsSource for bindable layout.
102+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
103+
{
104+
105+
if(value != null)
106+
{
107+
IList<DataModel> dataList = new List<DataModel>();
108+
if (value is DataModel)
109+
{
110+
dataList.Add(value as DataModel);
111+
return dataList;
112+
}
113+
else
114+
{
115+
return value;
116+
}
117+
}
118+
119+
return value;
120+
}
121+
122+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
123+
{
124+
return value;
125+
}
126+
}
127+
{% endhighlight %}
128+
129+
Step 2: DataTemplate declarations with any of the BindableLayout.
130+
131+
{% tabs %}
132+
<ContentView.Resources>
133+
<local:ChartValueConverter x:Key="itemsSourceConverter"/>
134+
<DataTemplate x:Key="TooltipTemplate">
135+
<StackLayout BindableLayout.ItemsSource="{Binding Converter={StaticResource itemsSourceConverter}}" >
136+
<BindableLayout.ItemTemplate>
137+
<DataTemplate>
138+
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
139+
<Label Text="{Binding XData}" HorizontalTextAlignment="Center"/>
140+
<Label Text="{Binding YData}" HorizontalTextAlignment="Center"/>
141+
</StackLayout>
142+
</DataTemplate>
143+
</BindableLayout.ItemTemplate>
144+
</StackLayout>
145+
</DataTemplate>
146+
</ContentView.Resources>
147+
{% endhighlight %}
148+
149+
Step 3: DataTemplate defined in the Series Tooltip Template.
150+
151+
{% tabs %}
152+
<chart:SfChart BackgroundColor="Transparent">
153+
. . .
154+
<chart:SfChart.Series>
155+
<chart:PieSeries . . .
156+
EnableTooltip="true"
157+
GroupMode="Value"
158+
GroupTo="50"
159+
TooltipTemplate="{StaticResource TooltipTemplate}"
160+
>
161+
. . .
162+
</chart:PieSeries>
163+
</chart:SfChart.Series>
164+
<chart:SfChart.ChartBehaviors>
165+
<chart:ChartTooltipBehavior BackgroundColor="LightBlue
166+
</chart:SfChart.ChartBehaviors>
167+
</chart:SfChart>
168+
{% endhighlight %}

0 commit comments

Comments
 (0)