Reputation: 601
I am using jquery Sparklines to make cute little bar graphs however I want to only colour the last bar a different colour. The code below works, but it would be nice if I could colour the bars by the tooltip names rather than by value.
Is there a way to do that?
var values = [500, 400, 700, 900, 1200, 300, 550];
var dates = {};
var now = new Date();
var counter = 0;
for (var i = values.length - 1; i >= 0; i--) {
var d = moment(now).subtract(1 * i, "day").format("MMM DD");
dates[counter] = d;
counter++;
}
$("#bargraph1").sparkline(values, {
type: "bar",
barWidth: 20,
barSpacing: 3,
height: 100,
tooltipFormat: "<span style=\"color: {{color}}\">●</span> {{offset:names}} ({{value}})",
tooltipValueLookups: {
names: dates
},
colorMap: ["blue", "blue", "blue", "blue", "blue", "blue", "red"]
});
.jqstooltip {
border: none !important;
box-sizing: content-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<span id="bargraph1"></span>
EDIT: can never get the code snippets to work. Heres a fiddle.js : http://jsfiddle.net/wm98qfdb/
Upvotes: 0
Views: 4121
Reputation: 8513
Try this snippet, I am bad at pasting the code as well, so... here goes nothing. I didn't implement it, but you could use a case statement in a for-loop to populate an array of names, so that 'squirrel' is always green, etc...
https://jsfiddle.net/omikey/y83dmusn/
.jqstooltip {
border:none !important;
box-sizing:content-box;
}
var values = [500, 400, 700, 900, 1200, 300, 550];
var dates = {};
var now = new Date();
var counter = 0;
for (var i = values.length - 1; i >= 0; i--) {
var d = moment(now).subtract(1 * i, "day").format("MMM DD");
dates[counter] = d;
counter++;
}
$("#bargraph1").sparkline(values, {
type: "bar",
barWidth: 20,
barSpacing: 3,
height: 100,
tooltipFormat: "<span style=\"color: {{color}}\">●</span> {{offset:names}} ({{value}})",
tooltipValueLookups: {
names: {
0: 'Squirrel',
1: 'Kitty',
2: 'Bird',
3: 'Three',
4: 'Four',
5: 'Five',
6: 'Six',
7: 'Seven'
// Add more here
}},
colorMap: ["green", "blue", "blue", "blue", "blue", "blue", "red"]
});
<span id="bargraph1"></span>
Upvotes: 2