c3 icon indicating copy to clipboard operation
c3 copied to clipboard

Hide Legend

Open bastiao opened this issue 12 years ago • 15 comments

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.

bastiao avatar Apr 03 '14 09:04 bastiao

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.

screen shot 2014-04-03 at 12 34 03

chart.resize(...)

screen shot 2014-04-03 at 12 33 48

CHR15- avatar Apr 03 '14 11:04 CHR15-

@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.

masayuki0812 avatar Apr 04 '14 03:04 masayuki0812

Nice.

You will add a method to show hide a particular legend? For instance, only to show one of them? It will be nice!

bastiao avatar Apr 04 '14 09:04 bastiao

@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 avatar Apr 07 '14 07:04 masayuki0812

@masayuki0812 I went to test this, but has caused this before I could test it:

screen shot 2014-04-07 at 11 43 45

that should be chart.resize({width: 750, height: 352});

CHR15- avatar Apr 07 '14 10:04 CHR15-

@CHR15- Could you tell me the detail and how to reproduce this?

masayuki0812 avatar Apr 07 '14 11:04 masayuki0812

Apologies, it seems I was setting the size object in generate incorrectly without noticing, I will test legend now.

CHR15- avatar Apr 07 '14 11:04 CHR15-

Looks pretty sweet @masayuki0812, nice work.

CHR15- avatar Apr 07 '14 15:04 CHR15-

OK, thank you for checking. I close this issue.

masayuki0812 avatar Apr 08 '14 01:04 masayuki0812

@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': 2014-05-06_231934

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'
    }
}

});

mickdee avatar May 07 '14 03:05 mickdee

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.

masayuki0812 avatar May 07 '14 14:05 masayuki0812

Boo yeah - works like a charm. Thanks @masayuki0812 !

mickdee avatar May 07 '14 18:05 mickdee

Legends overlap on x axis ticks. Any solution?

screen shot 2016-05-17 at 1 09 02 pm screen shot 2016-05-17 at 12 54 57 pm

pkpatels avatar May 17 '16 07:05 pkpatels

Up

Soufraz avatar May 10 '17 14:05 Soufraz

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() },
            }
        }
    }

Raghav-Kathuria avatar Oct 15 '18 09:10 Raghav-Kathuria