c3 icon indicating copy to clipboard operation
c3 copied to clipboard

Labels cut off in chart

Open AbeHandler opened this issue 11 years ago • 29 comments

I have a bar chart with rotated axes. The labels are a little long and are getting cut off. Here is a live demo:

https://dl.dropboxusercontent.com/spa/juxddtvfvlf51yv/Sample/test.html

Here is the code:

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, 200, 100, 400, 150, 250, 30, 200, 100, 400, 150, 250, 50, 50, 50, 50]
        ],
        type: 'bar'
    },
    bar: {
        width: {
            ratio: 0.25 // this makes bar width 50% of length between ticks
        }
        // or
        //width: 100 // this makes bar width 100px
    },
    axis: {
        x: {
            type: 'categorized',
            categories: ['City: Police and Fire', 'City: General Fund', 'City: Public Library', 'Board of Liquidation', 'Sewerage and Water Board', 'Audubon Zoo', 'Audubon Aquarium', 'Board of Assessors', 'School Board', 'Levee Board', 'Law Enforcement', 'Economic Development','Parkway and Recreation Department', 'Capital Improvement', 'Street and Traffic Control', 'Police and Fire']
        },
        rotated: true
    }
});

AbeHandler avatar May 07 '14 21:05 AbeHandler

I had the same problem:

Simply add

padding: {
  left: 500
},

to your chart config. To set a dynamic value, I'm using a little jQuery Plugin that gets the length of a text-string:


$.fn.textWidth = function(text, font) {
  if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  $.fn.textWidth.fakeEl.text(text || this.val() || this.text()).css('font', font || this.css('font'));
  return $.fn.textWidth.fakeEl.width();
};

var longest_text_width = 0;
chartData[0].forEach(function(data) {
  var w = $.fn.textWidth(data, '13px Clear Sans');
    if (w > longest_text_width) {
      longest_text_width = w;
    }
});

var chart = c3.generate({

  ...

  padding: {
    left: longest_text_width + 10 // add 10px for some spacing
  },

  ...

});

gopeter avatar May 08 '14 07:05 gopeter

Thanks. That solved my problem. This library is great -- but you should probably add some kind of auto calculation of the padding in future releases. This is exactly the kind of step you should automate away so that users don't have to think about it anymore.

AbeHandler avatar May 08 '14 16:05 AbeHandler

Hi, I agree. This issue is related to #129 and I'll fix this later. Thanks!

masayuki0812 avatar May 09 '14 04:05 masayuki0812

Hi @AbeHandler , I think the issue for automatic padding has been fixed in the latest version 0.1.38. Please try this version.

masayuki0812 avatar May 10 '14 13:05 masayuki0812

I think there is something fizzy going on with the padding calculation on mobile devices.

If you go to http://c3js.org/ and in Google Chrome Developer Tools -> Toggle Device Mode -> iPhone 3GS (other will do) and press F5. Y-axis values are cut off.

Any ideas how to fix it?

mobile_yaxis_cutoff

ttamminen avatar Feb 10 '15 12:02 ttamminen

Sorry, c3js.org is not working correctly. I'm planing the fixed version 0.4.10 shortly, then it should work.

masayuki0812 avatar Mar 10 '15 12:03 masayuki0812

in demo page this problem still exist

nhducit avatar Jul 02 '15 16:07 nhducit

@nhduc296 I just checked — looks fine to me? screen shot 2015-07-03 at 15 07 30

aendra-rininsland avatar Jul 03 '15 14:07 aendra-rininsland

@aendrew

in demo page http://c3js.org/samples/axes_x_tick_format.html


var chart = c3.generate({
    data: {
        x: 'x',
        columns: [
            ['x', '2010-01-01', '2011-01-01', '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01'],
            ['sample', 30, 200, 100, 400, 150, 250]
        ]
    },
    axis : {
        x : {
            type : 'timeseries',
            tick: {
                //format: function (x) { return x.getFullYear(); }
              format: '%d-%m-%Y' // format string is also available for timeseries data
            }
        }
    }
});


c3 label cut off

nhducit avatar Jul 04 '15 01:07 nhducit

Ah. Yes indeed, still seems to be an issue with longer titles.

aendra-rininsland avatar Jul 04 '15 09:07 aendra-rininsland

This issue seems still not solved. This is annoying :/ . Anyone knows how it should be done in a proper way ? Computing padding from total length of labels is still the better solution or is there any better methods ?

eregnier avatar Jul 26 '17 21:07 eregnier

Any update here?

skrzepij avatar Jan 26 '18 10:01 skrzepij

I'm still seeing this issue when setting a custom tick count. The last tick is being cut off.

abmultimedia avatar Feb 05 '18 20:02 abmultimedia

Having the same issue here. clipped

lepolt avatar Feb 14 '18 19:02 lepolt

Year 2018.

Can anyone help me, please? With this 'declarative' approach I simply don't know what do :(

(All labels are either overlapped or cropped)

screen shot 2018-02-23 at 10 54 30

inlinecoder avatar Feb 23 '18 08:02 inlinecoder

@inlinecoder maybe rotating your labels may solve your issue: http://c3js.org/samples/axes_x_tick_rotate.html

eregnier avatar Feb 23 '18 09:02 eregnier

Of course, we can find some kind of a workaround, but that doesn't solve the global issue.

Atm, I've managed to overcome overlapping and cropping, at least for now.

What I did is just moved nested styles and made them global. I guess, C3 tries to find matching styles and perform some calculations with it; and if some specific style is nested, C3 can't find it, and basically can't perform a proper calculation.

screen shot 2018-02-23 at 11 34 33 screen shot 2018-02-23 at 11 35 24

inlinecoder avatar Feb 23 '18 09:02 inlinecoder

Any update in this issue ? I am still getting this.

mervinva avatar Mar 13 '18 06:03 mervinva

@mervinva can you attach an exact issue you're having, so it's easier to think how to handle it?

inlinecoder avatar Mar 13 '18 11:03 inlinecoder

Same issue like the image commented by lepolt above.

mervinva avatar Mar 13 '18 11:03 mervinva

Same issue for me... Date/Year labels get truncated. Same issue as lepolt above.

garthgoodson avatar Aug 30 '18 17:08 garthgoodson

Still having this issue. Any fix?

victorpinheiro avatar Feb 25 '19 18:02 victorpinheiro

It woks for me

.c3 .c3-axis-x .tick:last-of-type text {
  transform: translate(-8px, 0)!important;
}

roremeol avatar Jun 25 '19 13:06 roremeol

@roremeol, that's not a good solution, even if it works.

inlinecoder avatar Jun 25 '19 13:06 inlinecoder

@inlinecoder, for now until they fix it, it's the best that i have

roremeol avatar Jun 25 '19 13:06 roremeol

Another way. However it probably may cause side effects

svg {
    overflow: visible !important;
}

luchnikovs avatar Sep 23 '19 12:09 luchnikovs

Having the same problem, anything new?

Lampent avatar Aug 01 '22 21:08 Lampent