。
編集:日付の唯一の年の部分を取得するためにd[0].x.getFullYear()
を使用(C3は内部JavaScriptのDateオブジェクトとして指定された年保存するので、それは時系列です)
は、ここで私はこの議論から撮影したコードですhttps://github.com/c3js/c3/issues/444、および修正:
function tooltip_contents(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:
//console.log($$.data.targets);
for (i = 0; i < d.length; i++) {
if (! (d[i] && (d[i].value || d[i].value === 0))) { continue; }
// to exclude
//if (d[i].name === 'data2') { continue; }
if (! text) {
title = 'MY TOOLTIP @ ' + d[0].x.getFullYear(); // SHOW X-VALUE, year only (given it is a time series)
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 + "'></span>" + name + "</td>";
text += "<td class='value'>" + value + "</td>";
text += "</tr>";
}
return text + "</table>";
}
var chart = c3.generate({
data: {
x: 'year',
xFormat: '%Y',
columns: [
['year', '1970', '1975', '1980', '1985', '1990'],
['data1', 100, 200, 150, 300, 200],
['data2', 400, 500, 250, 700, 300],
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y'
}
},
},
tooltip: {
contents: tooltip_contents
}
});
私のフィドル、現在のx値を示す:コードのhttp://jsfiddle.net/w7h385h3/5/
感謝を。しかし、x-value(csvからロードされる)は年です。例えば、「1970」です。 'd [i] .x'の値は' Thur Jan 01 1970 00:00:00 GMT-0800(PST) 'のように書かれています。 – Mahir
ツールチップのプロパティをオーバーライドしないと、タイトルは' 1970'のように正しく表示されます – Mahir
'xFormat: '%Y''を指定すると、年だけが返されます。これはこれを内部的に適用します。私は、時系列とJavaScriptの日付オブジェクトでコードを更新し、ちょうど年を表示しました - http://jsfiddle.net/w7h385h3/4/ –