0

我正在使用jquery Sparklines制作可爱的小条形图,但是我只想将最后一个条形图着色为不同的颜色。下面的代码有效,但如果我可以按工具提示名称而不是按值对条形进行着色,那就太好了。

有没有办法做到这一点?

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}}\">&#9679;</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>

编辑:永远无法让代码片段工作。这是一个 fiddle.js:http: //jsfiddle.net/wm98qfdb/

4

1 回答 1

2

试试这个片段,我也不擅长粘贴代码,所以......这里什么都没有。我没有实现它,但你可以在 for 循环中使用 case 语句来填充名称数组,这样“松鼠”总是绿色的,等等......

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}}\">&#9679;</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>
于 2015-05-20T01:25:33.963 回答