【发布时间】:2019-07-15 07:46:00
【问题描述】:
我需要显示两年的产品销售数据。但数据多于应有的数据。正如你所看到的,这是我想要的,现在它是静态的。
这是fiddle,我一直尝试到现在。我尝试过嵌套值,但它什么也没显示。在 2018 年,我只给出了单个值作为 json 对象来测试它是否有效。虽然我也尝试过给出所有嵌套值
var comaprechart = c3.generate({
bindto: '#' + chartid,
data: {
x: "x",
xSort: false,
columns: [
["x", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
["2018", {
"Total Revenue" : "$1,500.00",
"Units Sold": "100",
"Units Purchased" : "150"
}, 20, 30, 20, 10, 20, 30, 20, 10, 20, 30, 20],
// ["20118", 30, 10, 20, 30, 30, 10, 20, 30, 20, 10, 25, 15],
["2019", 20, 30, 10, 10, 20, 40, 10, 24, 34, 14, 24, 10]
],
type: 'spline',
regions: cregions
}, color: {
pattern: ["#9edefd", "#7C9EE5", "#2e97ff"]
},
axis: {
x: {
type: "category",
tick: {
outer: false,
fit: true,
centered: true,
},
lines: {
show: true
},
label: {
text: 'Month',
position: 'outer-center',
}
},
y: {
tick: {
outer: false,
format: function (d) { return d; }
},
label: {
text: 'Units Sold',
position: 'outer-middle'
}
}
},
size: { width: 400, height: 250 },
tooltip: {
format: {
},
contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
// console.log(d);
var $$ = this, config = $$.config,
titleFormat = config.tooltip_format_title || defaultTitleFormat,
nameFormat = config.tooltip_format_name || function (name) { return name; },
valueFormat = config.tooltip_format_value || defaultValueFormat,
text, i, title, value, name, bgcolor;
for (i = 0; i < d.length; i++) {
if (! (d[i] && (d[i].value || d[i].value === 0))) { continue; }
if (! text) {
title = titleFormat ? titleFormat(d[i].x) : d[i].x;
console.log(title);
text = "<table class='" + $$.CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2' class='hide'>" + title + "</th></tr>" : "");
}
name = nameFormat(d[i].name);
value = valueFormat(d[i].value, d[i].ratio, d[i].id, d[i].index);
bgcolor = $$.levelColor ? $$.levelColor(d[i].value) : color(d[i].id);
text += "<tr class='" + $$.CLASS.tooltipName + "-" + d[i].id + "'>";
text += "<td class='name'><span style='background-color:" + bgcolor + "'></span>" + name + "</td>";
text += "<td class='value'><tr><td>Revenue</td><td>$1,50" + [i] + ".00</td><tr><td>Units Sold</td><td>" + value + "</td><tr><tr><td>Units Purchased: </td><td>150</td></tr>";
text += "</tr>";
}
return text + "</table>";
}
},
legend: {
show: false
}
});
}
我想要的预期输出是显示
"Total Revenue" : "$1,500.00",
"Units Sold": "100",
"Units Purchased" : "150"
2018 年以下和 2019 年相同,如下图所示。目前它只显示单个值。
这是我想要的输出
【问题讨论】: