Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

legend alignment in bar graph using chartist

I am trying to create legends for a bar chart using chartist, i have used chartist plugin for legend, but i am not able to align it to the bottom of the chart, can anyone provide inputs on how to customise . (i have tried using it in css, but not able to reflect my changes.)

HTML :

<chartist class="ct-chart ct-major-twelfth"
    chartist-chart-type="Bar"
    chartist-data="chartData"
    chartist-chart-options="chartOptions">
</chartist>

JS:

chartData = {
    labels: [],
    series: [[]]
};

chartOptions= {
    plugins: [
        Chartist.plugins.legend()
    ]
};

legend.JS :

 $ct-series-colors: (#d70206, #F05B4F, #F4C63D, #453D3F) !default;

.ct-legend {
    position: relative;
    z-index: 10;

    li {
        position: relative;
        padding-left: 23px;
        margin-bottom: 3px;
    }

    li:before {
        width: 12px;
        height: 12px;
        position: absolute;
        left: 0;
        content: '';
        border: 3px solid transparent;
        border-radius: 2px;
    }

    li.inactive:before {
        background: transparent;
    }

    &.ct-legend-inside {
        position: absolute;
        top: 0;
        right: 0;
    }

    @for $i from 0 to length($ct-series-colors) {
        .ct-series-#{$i}:before {
            background-color: nth($ct-series-colors, $i + 1);
            border-color: nth($ct-series-colors, $i + 1);
        }
    }
}

also can anyone let me know how to add y-axis values on top of bars.i tried using below css file but still unable to get labels (similar to Chartist.plugins.ctPointLabels) but seems it is applicable only to line chart.

CSS:

.ct-chart .ct-bar {
  stroke-width: 60px;
}

.ct-chart .ct-label, .ct-chart .ct-label.ct-horizontal {
  text-align: center;
}

.ct-bar-label {
  text-anchor: middle;
  alignment-baseline: hanging;
  fill: white;
}

Thanks in advance.

like image 916
user1190615 Avatar asked Feb 22 '26 20:02

user1190615


1 Answers

It's really simple, but poorly documented.

Chartist.plugins.legend({
   position: 'bottom'
});

Keep in mind your chart SVG is positioned absolutely to it's parent. So you're legend CSS will need to set the position absolute as well.

For a more thorough approach, replace the chart.on('created', ...)

chart.on('created', function (data) {
    // Append the legend element to the DOM
    switch (options.position) {
        case 'top':
            chart.container.insertBefore(legendElement, chart.container.childNodes[0]);
            break;

        case 'bottom':
            chart.container.parentNode.insertBefore(legendElement, null);
            break;
    }
});

The key is the:

chart.container.parentNode.insertBefore(legendElement, null);

Which will insert the legend AFTER the element the chart is contained within.

Happy coding.

like image 139
pim Avatar answered Feb 24 '26 09:02

pim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!