C3 is very simple free JavaScript library that allows to create various types of chart literally in few lines of code. C3 is based on D3 so we must plugin it as well
<div id="chart"></div>
< script src = "https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js" > </script> < script src = "https://unpkg.com/c3@0.6.8/c3.js" > </script> < link href = "https://unpkg.com/c3@0.6.8/c3.css" rel = "stylesheet" > < div id = "chart" > < / div > < script > var chart = c3 . generate ( { bindto : '#chart' , data : { columns : [ [ 'data1' , 30 , 200 , 100 , 400 , 250 ] , [ 'data2' , 50 , 20 , 10 , 40 , 25 ] , [ 'data3' , 150 , 10 , 40 , 40 , 15 ] , [ 'data4' , 80 , 40 , 70 , 20 , 45 ] , [ 'data5' , 30 , 60 , 35 , 90 , 40 ] ] } } ) ; </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script src="https://unpkg.com/c3@0.6.8/c3.js"></script>
<link href="https://unpkg.com/c3@0.6.8/c3.css" rel="stylesheet">
<div id="chart"></div>
<script>
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 250],
['data2', 50, 20, 10, 40, 25],
['data3', 150, 10, 40, 40, 15],
['data4', 80, 40, 70, 20, 45],
['data5', 30, 60, 35, 90, 40]
]}
});
</script>
That small code snippet produces such a beautiful chart with a tooltip:
0 1 2 3 4 0 50 100 150 200 250 300 350 400 0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1 0 1 2 3 4 data1 data2 data3 data4 data5
But as you can see, the data in the tooltip are not sorted. The goal of this post is to show how to sort it.
Unfortunately, there is no a simple option to achieve this.
We have to override whole the tooltip code for that. The key property here is contents . It allows to use a custom template for tooltip appearance
< script > var chart = c3 . generate ( { bindto : '#chart' , data : { columns : [ [ 'data1' , 30 , 200 , 100 , 400 , 250 ] , [ 'data2' , 50 , 20 , 10 , 40 , 25 ] , [ 'data3' , 150 , 10 , 40 , 40 , 15 ] , [ 'data4' , 80 , 40 , 70 , 20 , 45 ] , [ 'data5' , 30 , 60 , 35 , 90 , 40 ] ] } , tooltip : { contents : function ( d , defaultTitleFormat , defaultValueFormat , color ) { 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 ; 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>" ; } } } ) ; </script> <script>
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 250],
['data2', 50, 20, 10, 40, 25],
['data3', 150, 10, 40, 40, 15],
['data4', 80, 40, 70, 20, 45],
['data5', 30, 60, 35, 90, 40]
]},
tooltip: {
contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
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;
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>";
}
}
});
</script>
0 1 2 3 4 0 50 100 150 200 250 300 350 400 0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1 0 1 2 3 4 data1 data2 data3 data4 data5
Awesome! Tooltip items are sorted now. But what if we want to sort the items values ascend? We have to add the following sort function:
d.sort(function(a, b){
return a.value - b.value;
});
d . sort ( function ( a , b ) { return a . value - b . value ; } ) ; d.sort(function(a, b){
return a.value - b.value;
});
The entire contents:
contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
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;
d.sort(function(a, b){
return a.value - b.value;
});
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;
text = "" + (title || title === 0 ? "" : "");
}
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 += "";
text += "";
text += "";
text += "";
}
return text + " ";
}
contents : function ( d , defaultTitleFormat , defaultValueFormat , color ) { 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 ; d . sort ( function ( a , b ) { return a . value - b . value ; } ) ; 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 ; text = "" + ( title | | title = = = 0 ? "" : "" ) ; } 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 + = "" ; text + = "" ; text + = "" ; text + = "" ; } return text + "<table class=" & quot ; + $$ . CLASS . tooltip + & quot ; " > < tbody > < tr > < th colspan = "2" > " + title + " < /th></ tr > < tr class = "" + $$.CLASS.tooltipName + "-" + d[i].id + "" > < td class = "name" > < span style = "background-color: " + bgcolor + ";" > < /span>" + name + "</ td > < td class = "value" > " + value + " < /td></ tr > < /tbody></ table > " ; } contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
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;
d.sort(function(a, b){
return a.value - b.value;
});
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;
text = "" + (title || title === 0 ? "" : "");
}
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 += "";
text += "";
text += "";
text += "";
}
return text + "<table class="" + $$.CLASS.tooltip + ""><tbody>
<tr><th colspan="2">" + title + "</th></tr>
<tr class="" + $$.CLASS.tooltipName + "-" + d[i].id + ""><td class="name"><span style="background-color: " + bgcolor + ";"></span>" + name + "</td><td class="value">" + value + "</td></tr>
</tbody></table>";
}
0 1 2 3 4 0 50 100 150 200 250 300 350 400 0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1 0 1 2 3 4 data1 data2 data3 data4 data5
You can try it on the JSFiddler:
https://jsfiddle.net/AndrewBuntsev/abqt8zgh
No comments:
Post a Comment