Brian Ritchie's Blog

My ramblings on .NET & other development topics

News



Twitter

Blog Roll

Connect with me

Adding sparklines to your MVC site using the Google Chart API

Sparklines present trends and variations associated with some measurement in a very compact form.  The term & concept was proposed by Ed Tufte.  This form of information graphic has become very popular for dashboards.  In this post, I'll show how add sparklines to your site using a custom HTML Helper method to generate code accessing the Google Chart API. In my next post, I'll show you how to add sparklines using the .NET charting API.

The folks over at Data Driven wrote a post about using the Google Chart API to render a sparkline.  The following shows how to take this concept & make it reusable by your MVC application.

Code for the extension method:

        public static MvcHtmlString DrawSparkline(this HtmlHelper helper, int width, int height, IEnumerable<int> plotData,
            string bgColor, string lineColor, string labelColor)
        {
            int? max = plotData.Max();
            int? min = plotData.Min();
            string list = String.Join(",", plotData);
            string sparkUrl = "<img src='http://chart.apis.google.com/" +
                 "chart?cht=lc&chf=bg,s,{6}&cgh=0,50,1,0&chds={0},{1}&chs={2}x{3}&chd=t:{4}&chco={8}" +
                 "&chls=1,1,0&chm=o,{7},0,20,4&chxt=r,x,y&chxs=0,{7},11,0,_|1,{7},1,0,_|2,{7},1,0,_&chxl=0:|{5}|1:||2:||'/>";
            string sparkLine = String.Format(sparkUrl, min, max, width, height, list, plotData.Last(), bgColor, labelColor, lineColor);
            return MvcHtmlString.Create(sparkLine);
        }

And here is an example of calling the HTML helper from a MVC Razor template:

@Html.DrawSparkline(70, 20, new int[14] { 1, 4, 2, 6, 20, 30, 1, 23, 14, 2, 41, 2, 33, 21 }, "ffffff", "999999", "990000")

Which produces the following sparkline:

Comments

Brian Ritchie's Blog said:

In my last post , I introduced sparklines and showed you how to generate them using the Google Chart

# August 30, 2011 10:37 PM

Using the .NET Chart API to add sparklines to your MVC site said:

Pingback from  Using the .NET Chart API to add sparklines to your MVC site

# August 31, 2011 3:21 AM

Using the .NET Chart API to add sparklines to your MVC site said:

Pingback from  Using the .NET Chart API to add sparklines to your MVC site

# August 31, 2011 3:21 AM

sebastian said:

error on String.Join(",", plotData);

Cannot string join an int array.

# April 24, 2012 1:03 PM

design icons said:

P.S. Please review our <a href="http://shock.setpixel.org">design portfolio</a> for Doors2012.

In Names, Tag Lines, and Company Personas - Why Bold Branding Wins

Yesterday I read an article about initiatives on the part of some airlines to facilitate passengers sitting next to someone they'd find interesting, while other airlines are making it easier to increase the odds of having a quiet flight by having no one sit in the next seat.Most people probably have a preference on this. Either they would like to talk to someone interesting on a long flight or would prefer to zone out in solitude. The feelingsw of those who don't care one way or the other undoubtedly have a lower intensity than the feelings of those who like or hate one of these options.Indifference isn't motivating. Attraction and repulsion are.And that's why blandness and similarity to competitors have such little power when it comes to publicity, word of mouth and customer loyalty. By not standing out, by trying to appeal to everyone and their uncle, companies have little or no energy charge. They earn little notice and ade into the background.Bolder branding works, however, because it doesn't try to please everyone. It aims to please those it has defined as ideal customers. When it's intelligently implemented, those who like the bolder branding really, really like it. Those who dislike it don't count. They aren't a loss because the likers are more likely to stick around, tell their like-minded friends and colleagues about the compant and promote the company and what it sells through articles, tweets, blog posts amd media coverage.You can certainly take bold branding too far - for example, by making it offensive in ways that cast shame on the company and its fans. Short of that, however, bold branding aligned with the desired customer base is very smart.Branding elements include the company name, its tag line and the personality a company takes on, as well as dozens of other items.Get started on bold branding by becoming clear about who the name or other branding element needs to appeal to, along with whose opinions don't matter at all. Warm up creatively by identifying other companies and advertising campaigns that you see being aimed at the same target population. Also identify their polar opposites - companies and campaigns that would make your target market shudder or turn away.Then throughout your brainstorming for new ideas, post those desirable and undesirable images on the wall to remind you that you're not trying to please the world at large or yourselves in coming up with ideas. You're trying to reach a certain set of people who have particular knowledge, attitudes, values and preferences. Above all, do not take a vote among the general public on branding elements or let the opinions of random people count in any way. Instead, create a set of criteria you can use to distinguish ideas that match the right profile from those that do not.If you can keep your eye on the goal, you'll reject boring, me-too branding elements and select bold names, tag lines, personas and more with exactly the right kind of magnetic charge.

# December 13, 2012 10:19 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)