Hide Legend
For instance, if you have a bar chart with multiple values: data1, data2, data3.
But you only show in the chart data1 and data2. The tooltip will show the data1, data2, and data3. It is ok. But in legend it does not make sense to appear.
Is it possible to dynamically hide the legend too? When you resize a chart to be smaller with many legend items, the chart is pushed too far up.

chart.resize(...)

@bastiao I'll add withLegend param to show/hide API and I think unload API can be used also. @chris-Opsimize I'll add this feature later. Thank you.
Nice.
You will add a method to show hide a particular legend? For instance, only to show one of them? It will be nice!
@CHR15-
I added legend.show and legend.hide API, so now you can show/hide legend dynamically by calling these APIs. Could you try these?
@bastiao
Now you can show/hide legends by calling legend.show and legend.hide APIs like this:
chart.legend.show() // show all legends
chart.legend.show('data1') // show legend for data1
chart.legend.show(['data1', 'data2']) // show legends for data1 and data2
legend.hide can be used by same way.
Addition to this, you can show/hide legend when show and hide called like this:
chart.show('data1', {withLegend: true}) // show chart and legend of 'data1'
chart.hide('data1', {withLegend: true}) //hide chart and legend of 'data1'
Please use this if you want to show/hide chart and legend at the same time.
@masayuki0812 I went to test this, but has caused this before I could test it:

that should be chart.resize({width: 750, height: 352});
@CHR15- Could you tell me the detail and how to reproduce this?
Apologies, it seems I was setting the size object in generate incorrectly without noticing, I will test legend now.
Looks pretty sweet @masayuki0812, nice work.
OK, thank you for checking. I close this issue.
@masayuki0812 Sorry for being a little dense on this one... after reading through this I'm still unsure how to only show specific legends.
For example, I have this chart and would like to hide the legend for 'date':

The chart code is as follows and I'm not sure where exactly to insert chart.legend.show('date') to hide the date legend:
var chart = c3.generate({
size: {
height: 320,
width: 960
},
data: {
url: 'urlgoeshere',
unload: ['date'],
type: 'spline',
labels: true,
colors: {
2011: '#E3E3F0',
2012: '#B4B2CB',
2013: '#746F95',
2014: '#4A475F'
},
},
legend: {
show: true,
position: 'right'
},
axis: {
x: {
type: 'categorized',
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
y: {
label: 'Number of Outings'
}
}
});
Hi @mickdee , Please call chart.legend.hide('date') just after var chart = c3.generate(..) called like this:
var chart = c3.generate({ ... });
chart.legend.hide('date');
I think this would work.
Boo yeah - works like a charm. Thanks @masayuki0812 !
Legends overlap on x axis ticks. Any solution?
Up
Hi @masayuki0812, I need a small help from you, pretty sure that you must be having a solution for this one.
I want to disable (it should be visible but in disabled state) few legend elements from c3js chart on page load. Post load, user can enable it by clicking on those legend elements. Also, I want to enable those legend elements on the basis of a flag value which I can setup through a checkbox besides the graph.
$scope.cpuChartArea = {
data: {
x: 'x',
columns: [
xLabels,
data1,
data2,
data3
],
xFormat: '%m-%d-%Y %H:%M:%S',
types: {
'data 1': 'area-spline',
'data 2': 'area-spline',
'data 3': 'area-spline',
}
},
point: {
show: false
},
axis: {
y: {
tick: {
format: function (d) { return d + "%"; }
}
},
x: {
type: 'timeseries',
tick: {
//format: function (x) { return x.getFullYear(); }
format: '%H:%M' // format string is also available for timeseries data
}
}
},
tooltip:{
format:{
title:function (x) { return x.getDate() + "/" + x.getMonth() + "/" + x.getFullYear() + " " + x.getHours()+ ":" + x.getMinutes() },
}
}
}