"invert" a Sparkline?

1,684 views
Skip to first unread message

Ian

unread,
Sep 11, 2012, 2:33:08 PM9/11/12
to jquery-s...@googlegroups.com

First of all, thanks for sharing Sparkline Gareth, it's great!

I was wondering if there is a way to "invert" a Sparkline like the attached.

Why would I want to do it?

I'm showing trends for search engine rankings, so in the attached Sparkline the left point would represent a high ranking (e.g. position #1) while the right point would be a low ranking (e.g. #100).

 Any suggestions are appreciated!

Ian


marcmaxson

unread,
Sep 16, 2012, 10:05:32 PM9/16/12
to jquery-s...@googlegroups.com
I regularly present inverted sparklines in parallel to the regular bar charts for direct comparison. I do it by weaving the data in python/cherrypy prior to feeding it to jquery. The inverted data set has negative values and the normal parallel trace has positive ones. See this example, here the inverted set is orange. Having data sets with two signs allows me to color them differently:

That last trace - male vs female - would be better done using the stacked bar options, but I cannot seem to get jquery to read the nested list string correctly / or at all.

Gareth

unread,
Oct 11, 2012, 9:20:31 PM10/11/12
to jquery-s...@googlegroups.com
Thanks for the suggestion - I opened issue #40 to track this

Dave Young

unread,
Nov 26, 2014, 11:50:34 AM11/26/14
to jquery-s...@googlegroups.com
Hi Gareth. I know this thread is pretty old but was wondering if you've had a chance to implement this? I cannot find any mention of issue 40 other than on the github issue tracker.

Thanks!

Dave Young

unread,
Nov 26, 2014, 2:08:23 PM11/26/14
to jquery-s...@googlegroups.com
I've added a small bit of code to accomplish this. It's working for my situation and perhaps it'll work for others as well.

First I added a new option called tootipInvert that defaults to false.

Then, in the getcurrentRegionTooltip function just above the line that assigns the text variable....

if (!fields[j].isNull || !options.get('tooltipSkipNull')) {
$.extend(fields[j], {
prefix: options.get('tooltipPrefix'),
suffix: options.get('tooltipSuffix')
});
// New code here
if (options.get('tooltipInvert')) {
fields[j].y *= -1;
}
// End of new code
text = format.render(fields[j], options.get('tooltipValueLookups'), options);
entries.push('<div class="' + fclass + '">' + text + '</div>');
}

This allows me to send inverted data to the plugin but have the tooltips show the non-inverted values.

Cheers!
Reply all
Reply to author
Forward
0 new messages