开发者

MVC Ajax Chart Controller

开发者 https://www.devze.com 2023-04-04 18:41 出处:网络
Well, I\'m creating an \"stats\" website. Already have a template, little data and structure. I like Google Charts API, specially the charts appearance, so I\'ve created a HtmlHelper as an extension

Well, I'm creating an "stats" website. Already have a template, little data and structure.

I like Google Charts API, specially the charts appearance, so I've created a HtmlHelper as an extension for it. You can uset just like this:

@Html.GoogleChart("Types", 300, 300, GChartType.Pie, (List<KeyValuePair<string, int>>)ViewBag.MyData)

It has several overloads and support multiple chart types and single/multidimension data. It returns the full html code for the chart image.

I will release the helper as Open Source in a few weeks, I'm just fixing some eventual bugs.

As you can see, I pass a List of KeyValuePair to the View through the ViewBag, so the chart can be generated using that data.

This works OK, but I'm facing multiple Controllers using the same data to generate charts and I want to code DRY. Can some of you, Jedi Masters help this young Padawan?

I think I have two problems:

  • How to not repeate chart in the Views;
  • How to not retrieve the stats/filtered data from the database in multiple controllers;

Facing this two problems, how to solve them?

  • Create partial Views with the Char开发者_Python百科t Helper already set?
  • Create a ChartController with Actions of each specific chart?
  • Create a controller to retrieve data and organize it?
  • Make a simple class to get data and return the organized data to multiple controllers?
  • Make a class to return only the url of the charts insted of using the Helper?
  • Both controller for charts and for data?
  • Make Ajax calls to theese guys?

Can someone help me understand the possibilities and maybe show a path?

I appreciate your time spent here!


How to not repeate chart in the Views

I'm not sure exactly what you mean, but if the amount of code being repeated is manageable (small - like a one-liner) then it's probably not a big deal to repeat. If it takes more effort to not repeat than it would to repeat then it's necessarily really worth worrying about.

How to not retrieve the stats/filtered data from the database in multiple controllers

Do you mean how to avoid getting the same data more than once? If so you need to pull the data into an intermediary and have your specific charting requests hit that instead. It will save hitting the original data source more than once - but there's a lot of work required to get this set-up, so you'll have to do some analysis to see if it's worth it (pros and cons, etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号