Top Right Legend Position for a C3 chart?
Asked Answered
F

4

11

Is there a way to position the legend in 'top right' for c3 charts?

The current options appear to only allow 'bottom' and 'right'. I've noticed there is a 'Custom Legend Option'. However, I wanted to check before proceeding down this path.

Thanks

Friday answered 11/11, 2014 at 23:44 Comment(1)
Just implemented the Custom Legend Option, and it allowed me to position (and style) the legend any way I wantFriday
U
29

I have a same issue. So I spent many time to find the solution. And Finally, I got the solution. Here is the solution.

Document

C3 Document is not bad, but some example is incomplete. Position of legend is one of them.

The example show position of legend can be located in only bottom, right and inset. That's all. But in the document, you can find more possibility.

Look at the c3.legend.position in the document. The document says again legend can be located in only bottom, right and inset. But don't be disappointed. Look at the below item, c3.legend.inset. And today, that item will save our time.

Code

First of all, look at the example.

legend: {
  inset: {
    anchor: 'top-right'
    x: 20,
    y: 10,
    step: 2
  }
}

You see it? That's a top-right you want. And code works wonderfully. If you change anchor, the position of legend'll be changed. And you can adjust coordinates with x and y. And step may be adjust length of legend. If you increase it, then legend'll be longer. But it's my guess. Please someone explain more about it.

Example

Here is my example.

c3.legend.inset example

var appActivityChart = c3.generate({
    data: {
        //...
    },
    legend: {
        show: true,
        position: 'inset',
        inset: {
            anchor: 'top-right',
            x: 250,
            y: 50,
            step: 5
        }
    }
});

I hope C3 add or refer about c3.legend.inset. Current example is too complicated. Until that time I hope this answer can save your time. Have great coding.

Unwearied answered 2/8, 2015 at 17:7 Comment(1)
Great answer. In the example it is not clear that position: 'inset' is required. Your answer makes that clear here. Thanks.Yellowbird
R
5

I have same question and found next solution to place legend above chart:

legend: {
    position: 'inset',
    inset: {
        anchor: 'top-left',
        x: 20,
        y: -40,
        step: 1
    }
},
padding: {
    top: 40
}

And legend will not cover chart.

Romanticize answered 21/12, 2015 at 17:50 Comment(0)
N
4
var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, 200, 100, 400, 150, 250],
            ['data2', 50, 20, 10, 40, 15, 25]
        ]
    },
     legend: {
        show: true,
        position: 'inset',
        inset: {
            anchor: 'top-right',
            x:undefined,
            y: undefined,
            step: undefined
        }
    }
});

Replaced undefined in x,y and step . The legend is display on top right.

Novia answered 6/4, 2016 at 5:42 Comment(0)
C
0
  inset: {
            anchor: 'top-right',
            x: 250,
            y: 50,
            step: 5
        }

Change to

  inset: {
            anchor: 'top-right'
        }
Cudweed answered 28/11, 2018 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.