Is it possible to build a Chart in the MVC Control and pass it to the View in the Model? I want to do this so that I can dynamically display a chart based on the desired dashboard layout. So I want the View to dictate where the charts are displayed but I want the View to be ignorant of the reports content. Let me know if I haven't explained what I want to do well enough or if there is a better way. I plan to have the dashboard layouts and which reports those contain stored in a database.
This is a "mark up"
View
ChartCollection(Model)
Controller
This is a "mark up"
View
@model ChartCollection
@{
ViewBag.Title =
"Dashboard"
;
}
<h2>
Dashboard
</h2>
@Html.Partial(
"_Report1"
, Model[
"ValidationResults"
])
@Html.Partial(
"_Report2"
, Model[
"ClientComponents"
])
ChartCollection(Model)
public
class
ChartCollection : Dictionary<
string
, IChart>
{
}
01.
public
class
HomeController : Controller
02.
{
03.
public
ActionResult Index()
04.
{
05.
ViewBag.Message =
"Welcome to ASP.NET MVC!"
;
06.
07.
return
View();
08.
}
09.
10.
public
ActionResult About()
11.
{
12.
ChartCollection charts =
new
ChartCollection();
13.
14.
IChart chart;
15.
16.
/// How do I duplicate this logic in the Controller?
17.
@(Html.Kendo().Chart()
18.
.Name(
"ClientComponents"
)
19.
.Title(
"Components"
)
20.
.DataSource(ds =>
21.
{
22.
ds.Model(model =>
23.
{
24.
foreach
(DataColumn col
in
Model.QueryResults.Columns)
25.
{
26.
model.Field(col.ColumnName, col.DataType);
27.
}
28.
});
29.
}
30.
)
31.
.Legend(legend => legend.Visible(
true
).Position(ChartLegendPosition.Bottom))
32.
.Series(series => series.Bar(
new
int
[] {2,4,6}).Name(
"MyValues"
))
33.
.CategoryAxis(yaxis => yaxis.Categories(
new
string
[] {
"Hello"
,
"World"
}))
34.
.Tooltip(tip => tip.Visible(
true
)) )
35.
36.
/// Add the IChart to the ChartCollection
37.
charts.Add(chart.Name, chart)
38.
return
View(charts);
39.
}
40.
}