programad
programad

Reputation: 1311

MVC Ajax Chart Controller

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:

Facing this two problems, how to solve them?

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

I appreciate your time spent here!

Upvotes: 0

Views: 495

Answers (1)

Adrian K
Adrian K

Reputation: 10227

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).

Upvotes: 2

Related Questions