Chartist-js: How to control the last axisX label style in css?

Created on 20 Jul 2015  路  7Comments  路  Source: gionkunz/chartist-js

here is what i want to make :
image

the first label in axisX is placed at the beginning, and the last label at the end , others center~
how can i make it by pure css?
( i found if axisX and axisY grouped in different , i can use last-child to control the last label in axisX)

enhancement

Most helpful comment

I've found a way to achieve this, although it's a bit of a hack:

chart.on('draw', function(event) {
    // If the draw event is for labels on the x-axis
    if (event.type === 'label' && event.axis.units.pos === 'x') {
        switch (event.index) {
            // First
            case 0:
                event.element._node.childNodes[0].style.marginLeft = '0px';
            break;

            // Last
            case SERIES_LENGTH:
                event.element._node.childNodes[0].style.marginLeft = '-51px';
            break;
        }
    }
});

Where SERIES_LENGTH is the number of data points - 1. Screenshot and JS Bin:
http://jsbin.com/zegotidisi/edit?js,output
JS Bin Screenshot

All 7 comments

This is somewhat a problem since all labels are in the same group. Maybe we can add a class first and last to the vertical and horizontal labels within chartist. For the moment you can use the draw events (check the documentation and examples) to add the classes yourself.

I found myself trying to do the same thing today. I made super crude plugin to deal with my problem with it.

https://github.com/mtgibbs/chartist-plugin-labelclasses

If that's of any use.

I've found a way to achieve this, although it's a bit of a hack:

chart.on('draw', function(event) {
    // If the draw event is for labels on the x-axis
    if (event.type === 'label' && event.axis.units.pos === 'x') {
        switch (event.index) {
            // First
            case 0:
                event.element._node.childNodes[0].style.marginLeft = '0px';
            break;

            // Last
            case SERIES_LENGTH:
                event.element._node.childNodes[0].style.marginLeft = '-51px';
            break;
        }
    }
});

Where SERIES_LENGTH is the number of data points - 1. Screenshot and JS Bin:
http://jsbin.com/zegotidisi/edit?js,output
JS Bin Screenshot

@kittsville

Thanks! That came in handy because the chart wasn't displaying the last value properly.

Without your code:
Without Code

With your code:
With Code

@kittsville - thanks!

For anyone else, his approach is also useful if you want to style other label attributes such as font-size, etc. For example, I'm rendering multiple graphs on the same page, but I want each graph to have different label styling.

Here's my code based on the structure outlined above:

//specify more options before the chart is displayed
chart.on('draw', function(data){

    //label size
    if (data.type === 'label') { data.element._node.childNodes[0].style.fontSize = '11px' }
});

@kittsville Awesome!! Thanks a lot!! 馃槜

have you tried to increase the chart padding:

chartPadding: { right: 30, bottom: 30 }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gsklee picture gsklee  路  4Comments

unhinged picture unhinged  路  3Comments

alberk8 picture alberk8  路  4Comments

alexcarpenter picture alexcarpenter  路  3Comments

adilbenmoussa picture adilbenmoussa  路  4Comments