|
1 | 1 | --- |
2 | | -title: Using Chart in column of a Grid |
3 | | -description: Learn how to use a Telerik UI for {{ site.framework }} Chart in a column of a Grid. |
| 2 | +title: Displaying Chart in a Column of a Grid |
| 3 | +description: Learn how to display a Telerik UI for {{ site.framework }} Chart in a column cell of a Telerik UI for {{ site.framework }} Grid. |
4 | 4 | type: how-to |
5 | | -page_title: Using a Chart in a column of a Grid |
6 | | -previous_url: /helpers/charts/how-to/use-chart-in-ajax-grid-column, html-helpers/charts/how-to/howto_createchartinajaxgridcolumn_chartaspnetmvc |
| 5 | +page_title: Displaying Chart in a Column of a Grid |
| 6 | +previous_url: /helpers/charts/how-to/use-chart-in-ajax-grid-column, /html-helpers/charts/how-to/use-chart-in-ajax-grid-column |
7 | 7 | slug: chart-in-grid-column |
8 | 8 | tags: chart, grid, column, template |
9 | 9 | res_type: kb |
10 | 10 | --- |
11 | 11 |
|
12 | | -# Description |
13 | | -How can I use a Telerik UI for {{ site.framework }} Chart in a column of a Grid? |
| 12 | +## Environment |
| 13 | + |
| 14 | + <table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>{{ site.product }} Chart</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Product Version</td> |
| 21 | + <td>Created with version 2024.4.1112</td> |
| 22 | + </tr> |
| 23 | + </table> |
14 | 24 |
|
15 | | -# Solution |
| 25 | +## Description |
| 26 | +How can I initialize a Chart into a specified [Grid]({% slug htmlhelpers_grid_aspnetcore_overview %}) column? |
16 | 27 |
|
17 | | -1. Declare the Telerik UI for {{ site.framework }} Grid. |
18 | | -1. Use column [`Templates`](https://docs.telerik.com/aspnet-core/html-helpers/data-management/grid/templates/overview) for the columns of the Grid. |
19 | | -1. Add the declarations for the Charts in the Templates. |
20 | | -1. Use the [`DataBinding`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/databinding) and [`DataBound`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/databound) Events of the Grid. |
21 | | -1. In the Event handlers, use the [`destroy`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/destroy) and [`eval`](https://www.w3schools.com/jsref/jsref_eval.asp#:~:text=The%20eval()%20method%20evaluates,eval()%20executes%20the%20statements.) methods to render the Charts. |
| 28 | +## Solution |
| 29 | + |
| 30 | +1. Declare the Grid component. |
| 31 | +1. Use the [`Template()`](/api/kendo.mvc.ui.fluent/gridcolumnfactory#templatesystemstring) option of the Grid column to define the Charts. |
| 32 | +1. Handle the [`DataBinding`](/api/kendo.mvc.ui.fluent/grideventbuilder#databindingsystemstring) and [`DataBound`](/api/kendo.mvc.ui.fluent/grideventbuilder#databoundsystemstring) events of the Grid. |
| 33 | +1. Within the event handlers, call the [`destroy`](https://docs.telerik.com/kendo-ui/api/javascript/kendo/methods/destroy) method and the jQuery [`eval`](https://www.w3schools.com/jsref/jsref_eval.asp#:~:text=The%20eval()%20method%20evaluates,eval()%20executes%20the%20statements.) method to render the Charts. By design, the script tags are not automatically evaluated inside the Grid column template, so the scripts must be evaluated manually in the `DataBound` event of the Grid.. |
22 | 34 |
|
23 | 35 |
|
24 | 36 | ```HtmlHelper |
@@ -66,42 +78,53 @@ How can I use a Telerik UI for {{ site.framework }} Chart in a column of a Grid? |
66 | 78 | {% if site.core %} |
67 | 79 | ```TagHelper |
68 | 80 | @addTagHelper *, Kendo.Mvc |
69 | | - @{ |
70 | | - var categories = new string[] { "2000", "2001", "2002", "2003" }; |
71 | | - } |
72 | 81 |
|
73 | | - <kendo-grid name="grid2" data-binding="onDataBinding" data-bound="onDataBound" > |
74 | | - <datasource type="DataSourceTagHelperType.Ajax" page-size="20" server-operation="false" data="@Model"> |
75 | | - <schema> |
76 | | - <model id="ID"> |
77 | | - </model> |
78 | | - </schema> |
79 | | - </datasource> |
| 82 | + @model IEnumerable<TelerikAspNetCoreApp26.Models.ViewModel> |
80 | 83 | |
| 84 | + <kendo-grid name="grid" on-data-binding="onDataBinding" on-data-bound="onDataBound"> |
81 | 85 | <columns> |
82 | 86 | <column field="ID" title="ID"></column> |
83 | 87 | |
84 | 88 | <column title="Chart Remote Data"> |
85 | 89 | <column-template> |
86 | | - <kendo-chart name="ID"> |
87 | | - <category-axis> |
88 | | - <category-axis-item categories="categories"> |
89 | | - </category-axis-item> |
90 | | - </category-axis> |
| 90 | + <kendo-chart name="remote${data.ID}"> |
| 91 | + <series-defaults type="ChartSeriesType.Column"> |
| 92 | + <stack enabled="true" /> |
| 93 | + </series-defaults> |
| 94 | + <datasource> |
| 95 | + <transport> |
| 96 | + <read type="post" url="@Url.Action("ReadChartData", "Home", new {id = "${data.ID}"})" /> |
| 97 | + </transport> |
| 98 | + </datasource> |
91 | 99 | <series> |
92 | | - <series-item type="ChartSeriesType.Bar" |
93 | | - name="Example Series" |
94 | | - data="new double[] { 200, 450, 300, 125 }"> |
95 | | - </series-item> |
| 100 | + <series-item type="ChartSeriesType.Column" field="Value" category-field="Category" name="Value Series"></series-item> |
| 101 | + <series-item type="ChartSeriesType.Column" field="Value1" category-field="Category" name="Value1 Series"></series-item> |
| 102 | + </series> |
| 103 | + <tooltip template="#:category# - #: value #" visible="true"></tooltip> |
| 104 | + </kendo-chart> |
| 105 | + </column-template> |
| 106 | + </column> |
| 107 | + |
| 108 | + <column title="Chart Local Data"> |
| 109 | + <column-template> |
| 110 | + <kendo-chart name="local${data.ID}" class="chart-local"> |
| 111 | + <series-defaults type="ChartSeriesType.Column"> |
| 112 | + <stack enabled="true" /> |
| 113 | + </series-defaults> |
| 114 | + <series> |
| 115 | + <series-item type="ChartSeriesType.Column" field="Value" category-field="Category" name="Value Series"></series-item> |
| 116 | + <series-item type="ChartSeriesType.Column" field="Value1" category-field="Category" name="Value1 Series"></series-item> |
96 | 117 | </series> |
97 | | - <chart-legend position="ChartLegendPosition.Bottom"> |
98 | | - </chart-legend> |
99 | | - <chart-title text="Kendo Chart Example"> |
100 | | - </chart-title> |
101 | 118 | </kendo-chart> |
102 | 119 | </column-template> |
103 | 120 | </column> |
104 | 121 | </columns> |
| 122 | + <datasource type="DataSourceTagHelperType.Ajax" page-size="20" server-operation="false" data="@Model"> |
| 123 | + <schema> |
| 124 | + <model id="ID"> |
| 125 | + </model> |
| 126 | + </schema> |
| 127 | + </datasource> |
105 | 128 | </kendo-grid> |
106 | 129 | ``` |
107 | 130 | {% endif %} |
@@ -133,7 +156,7 @@ How can I use a Telerik UI for {{ site.framework }} Chart in a column of a Grid? |
133 | 156 | </style> |
134 | 157 | ``` |
135 | 158 |
|
136 | | -For the complete implementation of the suggested approach [refer to the project on how to use a Chart in the ClientTemplate of a Grid column and bind the Chart based on the row data](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/ChartInGrid). |
| 159 | +For the complete implementation of the suggested approach, refer to [the project on how to use a Chart in the ClientTemplate of a Grid column and bind the Chart based on the row data](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/ChartInGrid). {% if site.core %}You can use this as a starting point to configure the same setup in an ASP.NET Core project.{% endif %} |
137 | 160 |
|
138 | 161 | ## More {{ site.framework }} Chart Resources |
139 | 162 |
|
|
0 commit comments