|
| 1 | +--- |
| 2 | +title: Appending Percentage and NameField to Pie Chart Items in RadHtmlChart for ASP.NET AJAX |
| 3 | +description: Learn how to append percentage values and name fields to each item in a RadHtmlChart Pie Chart dynamically using C# codebehind. |
| 4 | +type: how-to |
| 5 | +page_title: How to Dynamically Append Percentage and NameField to RadHtmlChart Pie Chart Items Using C# |
| 6 | +slug: how-to-append-percentage-and-namefield-radhtmlchart-pie-chart |
| 7 | +tags: radhtmlchart, asp.net ajax, pie chart, codebehind, c#, clienttemplate, data binding |
| 8 | +res_type: kb |
| 9 | +ticketid: 1677029 |
| 10 | +--- |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +When working with the [RadHtmlChart for ASP.NET AJAX](https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/overview), you might want to dynamically append percentage values and name fields to each item in a Pie Chart directly from the codebehind using C#. This knowledge base article also answers the following questions: |
| 15 | + |
| 16 | +- How to use ClientTemplate in RadHtmlChart PieSeries with C#? |
| 17 | +- How to dynamically set colors for Pie Chart items in RadHtmlChart? |
| 18 | +- How to bind data to RadHtmlChart PieSeries using codebehind in ASP.NET AJAX? |
| 19 | + |
| 20 | +## Environment |
| 21 | + |
| 22 | +<table> |
| 23 | +<tbody> |
| 24 | +<tr> |
| 25 | +<td>Product</td> |
| 26 | +<td>RadHtmlChart for ASP.NET AJAX</td> |
| 27 | +</tr> |
| 28 | +</tbody> |
| 29 | +</table> |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To append percentage values and name fields to each item in a RadHtmlChart Pie Chart, and optionally set item colors dynamically from the codebehind, you can follow these steps: |
| 34 | + |
| 35 | +### Approach 1: Use a Dictionary for Color Mapping |
| 36 | + |
| 37 | +This approach allows you to map `DAYPART` values to their corresponding colors without modifying the database. A calculated `COLOR` column is added dynamically to the `DataTable`, which is then used to set the slice colors. |
| 38 | + |
| 39 | +**ASPX.CS:** |
| 40 | + |
| 41 | +```ASPX |
| 42 | + <telerik:RadHtmlChart runat="server" ID="DayPartsPieChart" Height="400px" Width="500px" |
| 43 | + Legend-Appearance-Position="Right" |
| 44 | + BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" |
| 45 | + Transitions="true"> |
| 46 | + <Appearance FillStyle-BackgroundColor="White"></Appearance> |
| 47 | + <PlotArea> |
| 48 | + <Series> |
| 49 | + <telerik:PieSeries> |
| 50 | + </telerik:PieSeries> |
| 51 | + </Series> |
| 52 | + </PlotArea> |
| 53 | + <Legend> |
| 54 | + <Appearance Visible="false" /> |
| 55 | + </Legend> |
| 56 | + </telerik:RadHtmlChart> |
| 57 | +``` |
| 58 | + |
| 59 | +```csharp |
| 60 | + protected void Page_Init(object sender, EventArgs e) |
| 61 | + { |
| 62 | + if (!IsPostBack) |
| 63 | + { |
| 64 | + LoadDayPartsPieChart(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void LoadDayPartsPieChart() |
| 69 | + { |
| 70 | + // Define and populate the data source |
| 71 | + DataTable table = new DataTable(); |
| 72 | + table.Columns.Add("DAYPART", typeof(string)); |
| 73 | + table.Columns.Add("PCT", typeof(decimal)); |
| 74 | + |
| 75 | + table.Rows.Add("EM", 18.5m); |
| 76 | + table.Rows.Add("DT", 18.6m); |
| 77 | + table.Rows.Add("PT", 3.0m); |
| 78 | + table.Rows.Add("LN", 37.1m); |
| 79 | + table.Rows.Add("LE", 11.4m); |
| 80 | + table.Rows.Add("EF", 11.4m); |
| 81 | + |
| 82 | + // Define a dictionary for DAYPART-to-color mapping |
| 83 | + Dictionary<string, string> colorMapping = new Dictionary<string, string> |
| 84 | + { |
| 85 | + { "EM", "Purple" }, |
| 86 | + { "DT", "Orange" }, |
| 87 | + { "PT", "Green" }, |
| 88 | + { "LN", "Blue" }, |
| 89 | + { "LE", "Red" }, |
| 90 | + { "EF", "Yellow" } |
| 91 | + }; |
| 92 | + |
| 93 | + // Add a calculated COLOR column dynamically |
| 94 | + table.Columns.Add("COLOR", typeof(string)); |
| 95 | + foreach (DataRow row in table.Rows) |
| 96 | + { |
| 97 | + string dayPart = row["DAYPART"].ToString(); |
| 98 | + if (colorMapping.ContainsKey(dayPart)) |
| 99 | + { |
| 100 | + row["COLOR"] = colorMapping[dayPart]; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Clear existing series |
| 105 | + DayPartsPieChart.PlotArea.Series.Clear(); |
| 106 | + |
| 107 | + // Create a new PieSeries |
| 108 | + Telerik.Web.UI.PieSeries pieSeries = new Telerik.Web.UI.PieSeries |
| 109 | + { |
| 110 | + DataFieldY = "PCT", // Bind PCT values to the pie slices |
| 111 | + NameField = "DAYPART", // Bind DAYPART names to labels |
| 112 | + ColorField = "COLOR", // Bind dynamically calculated COLOR field |
| 113 | + LabelsAppearance = |
| 114 | + { |
| 115 | + Position = Telerik.Web.UI.HtmlChart.PieAndDonutLabelsPosition.OutsideEnd, |
| 116 | + Visible = true, |
| 117 | + ClientTemplate = "#= dataItem.PCT # % \\n #= dataItem.DAYPART #" |
| 118 | + }, |
| 119 | + TooltipsAppearance = |
| 120 | + { |
| 121 | + ClientTemplate = "#= dataItem.PCT # : \\n #= dataItem.DAYPART #" |
| 122 | + }, |
| 123 | + VisibleInLegend = false |
| 124 | + }; |
| 125 | + |
| 126 | + // Add the PieSeries to the chart |
| 127 | + DayPartsPieChart.PlotArea.Series.Add(pieSeries); |
| 128 | + |
| 129 | + // Bind the chart to the data source |
| 130 | + DayPartsPieChart.DataSource = table; |
| 131 | + DayPartsPieChart.DataBind(); |
| 132 | + |
| 133 | + // Optionally update the chart title |
| 134 | + DayPartsPieChart.ChartTitle.Text = "Airings by Daypart"; |
| 135 | + } |
| 136 | +``` |
| 137 | + |
| 138 | +### Approach 2: Use the ColorField Property |
| 139 | + |
| 140 | +If your data source already includes a `COLOR` column, you can directly bind it to the `ColorField` property of the `PieSeries` to dynamically set the item colors. |
| 141 | + |
| 142 | +**ASPX.CS:** |
| 143 | + |
| 144 | +```csharp |
| 145 | +// Similar to Approach 1, but ensure your DataTable includes a COLOR column |
| 146 | + protected void Page_Init(object sender, EventArgs e) |
| 147 | + { |
| 148 | + if (!IsPostBack) |
| 149 | + { |
| 150 | + LoadDayPartsPieChart(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public void LoadDayPartsPieChart() |
| 155 | + { |
| 156 | + // Define and populate the data source |
| 157 | + DataTable table = new DataTable(); |
| 158 | + table.Columns.Add("DAYPART", typeof(string)); |
| 159 | + table.Columns.Add("PCT", typeof(decimal)); |
| 160 | + table.Columns.Add("COLOR", typeof(string)); // Add a COLOR column to the data source |
| 161 | +
|
| 162 | + table.Rows.Add("EM", 18.5m, "Purple"); |
| 163 | + table.Rows.Add("DT", 18.6m, "Orange"); |
| 164 | + table.Rows.Add("PT", 3.0m, "Green"); |
| 165 | + table.Rows.Add("LN", 37.1m, "Blue"); |
| 166 | + table.Rows.Add("LE", 11.4m, "Red"); |
| 167 | + table.Rows.Add("EF", 11.4m, "Yellow"); |
| 168 | + |
| 169 | + // Clear existing series |
| 170 | + DayPartsPieChart.PlotArea.Series.Clear(); |
| 171 | + |
| 172 | + // Create a new PieSeries |
| 173 | + Telerik.Web.UI.PieSeries pieSeries = new Telerik.Web.UI.PieSeries |
| 174 | + { |
| 175 | + DataFieldY = "PCT", // Bind PCT values to the pie slices |
| 176 | + NameField = "DAYPART", // Bind DAYPART names to labels |
| 177 | + ColorField = "COLOR", // Bind COLOR column to dynamically set colors |
| 178 | + LabelsAppearance = |
| 179 | + { |
| 180 | + Position = Telerik.Web.UI.HtmlChart.PieAndDonutLabelsPosition.OutsideEnd, |
| 181 | + Visible = true, |
| 182 | + ClientTemplate = "#= dataItem.PCT # % \\n #= dataItem.DAYPART #" |
| 183 | + }, |
| 184 | + TooltipsAppearance = |
| 185 | + { |
| 186 | + ClientTemplate = "#= dataItem.PCT # : \\n #= dataItem.DAYPART #" |
| 187 | + }, |
| 188 | + VisibleInLegend = false |
| 189 | + }; |
| 190 | + |
| 191 | + // Add the PieSeries to the chart |
| 192 | + DayPartsPieChart.PlotArea.Series.Add(pieSeries); |
| 193 | + |
| 194 | + // Bind the chart to the data source |
| 195 | + DayPartsPieChart.DataSource = table; |
| 196 | + DayPartsPieChart.DataBind(); |
| 197 | + |
| 198 | + // Optionally update the chart title |
| 199 | + DayPartsPieChart.ChartTitle.Text = "Airings by Daypart"; |
| 200 | + } |
| 201 | +``` |
| 202 | + |
| 203 | +Both approaches allow for dynamically appending percentage values and name fields to each Pie Chart item and setting their colors from the codebehind. |
| 204 | + |
| 205 | +## See Also |
| 206 | + |
| 207 | +- [RadHtmlChart Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/overview) |
| 208 | +- [PieSeries - Telerik UI for ASP.NET AJAX](https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/chart-types/pie-chart) |
| 209 | +- [RadHtmlChart ClientTemplate](https://www.telerik.com/products/aspnet-ajax/documentation/controls/htmlchart/functionality/clienttemplate/overview) |
0 commit comments