我试图在工具提示中显示的表格上添加页脚。在这一行中,我想显示从json文件加载的特定数据。
图表:
<script>
var scene;
$.getJSON('assets/json/chartc3.json', function (data) {
scene = data;
var chart = c3.generate({
bindto: '#chartc3',
data:
{
json: scene,
keys:
{
x: 'round',
value: ['Marketable', 'Total Requested Capacity', 'Your Bid'],
},
types: {
Marketable: 'area'
},
colors: {
Marketable: '#A09FA2',
'Total Requested Capacity': '#272E80',
'Your Bid': '#8EBF60'
}
},
axis:
{
x: {
tick:
{
culling:
{
max: 10
}
},
type: 'category'
},
y:
{
min: 0,
padding: {
bottom: 0
},
tick:
{
values: [[0], [d3.max(d3.values(scene))]],
format: function (d) { return d3.format(',f')(d) + ' kWh/h' }
//or format: function (d) { return '$' + d; }
}
}
},
tooltip: {
contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
var $$ = this, config = $$.config, CLASS = $$.CLASS,
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;
// You can access all of data like this:
for (i = 0; i < d.length; i++) {
if (!(d[i] && (d[i].value || d[i].value === 0))) { continue; }
// ADD
if (d[i].name === 'data2') { continue; }
if (!text) {
title = 'MY TOOLTIP'
text = "<table class='" + CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2'>" + 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 + "; border-radius: 5px;'></span>" + name + "</td>";
text += "<td class='value'>" + value + "</td>";
text += "</tr>";
}
**var surchargedata;
$.getJSON('assets/json/surcharge.json', function(data)
{
console.log("Prima"+text);
surchargedata=data;
text += "<tr class='" + CLASS.tooltipName + "-Surcharge" + "'>";
text += "<td class='name'>" + surchargedata[i].Surcharge+ "</td>";
text += "<td class='value'>" + surchargedata[i].Surcharge+ "</td>";
text += "</tr></table>";
console.log(text);
})**
return text;
}
}
});
});
</script>
我打印了日志,表格似乎生成得很好但是如果你看一下工具提示:
我看不到最后一行。
此处生成的表的日志:
<table class='c3-tooltip'>
<tr>
<th colspan='2'>MY TOOLTIP</th>
</tr>
<tr class='c3-tooltip-name-Marketable'>
<td class='name'><span style='background-color:#A09FA2; border-radius: 5px;'>
</span>Marketable
</td>
<td class='value'>5,220,593 kWh/h</td>
</tr>
<tr class='c3-tooltip-name-Total Requested Capacity'>
<td class='name'><span style='background-color:#272E80; border-radius: 5px;'>
</span>Total Requested Capacity
</td>
<td class='value'>16,449,051 kWh/h</td>
</tr>
<tr class='c3-tooltip-name-Your Bid'>
<td class='name'>
<span style='background-color:#8EBF60; border-radius: 5px;'>
</span>Your Bid
</td>
<td class='value'>100,000 kWh/h
</td>
</tr>
<tr class='c3-tooltip-name-Surcharge'>
<td class='name'>50.38</td>
<td class='value'>50.38</td>
</tr>
</table>
答案 0 :(得分:1)
生成工具提示时,使用$ .getJSON加载附加费数据。并且您的附加费行将添加到您为回调中的工具提示生成的文本中。到目前为止,实际的工具提示函数已经返回了$ .getJSON调用之前生成的任何文本。
解决此问题的最简单方法是首先加载附加费数据,方法是移动附加费$ .getJSON以包裹原始脚本。
这是更新的脚本。
<script>
var surchargedata;
$.getJSON('assets/json/surcharge.json', function (data) {
surchargedata = data;
var scene;
$.getJSON('assets/json/chartc3.json', function (data) {
scene = data;
var chart = c3.generate({
bindto: '#chartc3',
data:
{
json: scene,
keys:
{
x: 'round',
value: ['Marketable', 'Total Requested Capacity', 'Your Bid'],
},
types: {
Marketable: 'area'
},
colors: {
Marketable: '#A09FA2',
'Total Requested Capacity': '#272E80',
'Your Bid': '#8EBF60'
}
},
axis:
{
x: {
tick:
{
culling:
{
max: 10
}
},
type: 'category'
},
y:
{
min: 0,
padding: {
bottom: 0
},
tick:
{
values: [[0], [d3.max(d3.values(scene))]],
format: function (d) { return d3.format(',f')(d) + ' kWh/h' }
//or format: function (d) { return '$' + d; }
}
}
},
tooltip: {
contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
var $$ = this, config = $$.config, CLASS = $$.CLASS,
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;
console.log(JSON.stringify(d))
// You can access all of data like this:
for (i = 0; i < d.length; i++) {
if (!(d[i] && (d[i].value || d[i].value === 0))) { continue; }
// ADD
if (d[i].name === 'data2') { continue; }
if (!text) {
title = 'MY TOOLTIP'
text = "<table class='" + CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2'>" + 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 + "; border-radius: 5px;'></span>" + name + "</td>";
text += "<td class='value'>" + value + "</td>";
text += "</tr>";
}
console.log("Prima" + text);
text += "<tr class='" + CLASS.tooltipName + "-Surcharge" + "'>";
text += "<td class='name'>" + surchargedata[i].Surcharge + "</td>";
text += "<td class='value'>" + surchargedata[i].Surcharge + "</td>";
text += "</tr></table>";
console.log(text);
return text;
}
}
});
});
})
</script>