0

I am looking a friends code to implement google charts into my mvc project.

I have a model that I am pulling data from:

public partial class HoursPerSite
{
    public string SiteName { get; set; }
    public Nullable<decimal> SiteHours { get; set; }
}

The chart has rendered and it looks good but the legend is showing SiteHours and not SiteName as I'd like it to.

Here is the controller part:

// HOLIDAY PIE START
ViewBag.msg = db.HoursPerSites.Count().ToString();

var query = from r in db.HoursPerSites
            select new { Count = r.SiteHours, Value = r.SiteHours };

var result2 = query.ToList();

var datachart2 = new object[result2.Count];
int l = 0;
foreach (var i in result2)
{
    datachart2[l] = new object[] { i.Value.ToString(), i.Count };
    l++;
}
string datastr2 = JsonConvert.SerializeObject(datachart2, Formatting.None);
ViewBag.dataj2 = new HtmlString(datastr2);

// HOLIDAY PIE END

I'd like it so the pie chart legend/key shows the site not the hours.

1 Answer 1

1

Not sure but is this:

select new { Count = r.SiteHours, Value = r.SiteHours };

Not supposed to be this: (SiteName):

select new { Count = r.SiteHours, Value = r.SiteName };

Also, your naming is very bad if I may say. That will not make your future work easier. Try naming your variables and obejects more specifically.

EDIT:

  • You can make use of Regions instead of code comments for example which will make seperation/ordering/viewing of code parts/sections much easier
  • Naming your variables better will make your life but also other people working on/with your code easier

I would change your current code to this:

Please note that I don't have any editor and that there could be syntax errors

#region Holiday Pie Chart

ViewBag.msg = db.HoursPerSites.Count().ToString();

var queryHoursPerSites = from r in db.HoursPerSites
        select new { Count = r.SiteHours, Value = r.SiteHours };

var resultsQueryHoursPerSites = queryHoursPerSites.ToList(); // or HoursPerSitesCollection

var holidayPieChart = new object[resultsQueryHoursPerSites.Count];
int counter = 0; // 
foreach (var record in resultsQueryHoursPerSites)
{
    holidayPieChart[counter] = new object[] { record.Value.ToString(), record.Count };
counter++;
}

string deserialisedResults = JsonConvert.SerializeObject(holidayPieChart, Formatting.None);

// no idea what dataj2 is here ...
ViewBag.dataj2 = new HtmlString(deserialisedResults);


#endregion

I am sure there is even more to "improve" or "change" but that would in my opinion already be an improvement.

I am sure others can elaborate even better on what I am suggesting here :)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that worked! Its my friends code. The only naming of mine is SiteHours, SiteName. If that's what you are referring to, could you explain why? I'd like to get better!
You are welcome, i have edited my original answer. SiteHours and SiteName are good names (i don't know much about the context of the project you are working on).
"You can make use of Regions" - regions are unpopular with some people - including myself. A better way of "seperation/ordering/viewing of code parts/sections" is to use a method.
What I am referring to is a visual not logical structure. If you have tons of methods, personally, i prefer them to be (in a region that you can collapse/uncollapse if needed) this will make "navigation" in a large scaled project much easier and faster instead of having them seperated (partial classes and so on) but that is a personal preference. I am using the seperation of concerns and encapsulation (SOLID, OOP, Design Patters ...) principles for my code, but as explained above, i do use regions. I do love the concept of the Anti-Region-Legion, pretty neat "concept" :)
@Dimitri Thanks for the tip Dimitri, that makes alot of sense!! I'll definitely use regions now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.