javascript - legend alignment in bar graph using chartist - Stack Overflow

I am trying to create legends for a bar chart using chartist, i have used chartist plugin for legend, b

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.

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.

Share Improve this question edited Mar 2, 2016 at 19:58 manzapanza 6,2554 gold badges42 silver badges50 bronze badges asked Mar 2, 2016 at 19:19 user1190615user1190615 1111 gold badge4 silver badges17 bronze badges 2
  • 1 Did you found what you're looking for, I'm having the same need. – wilfriedroset Commented Apr 4, 2016 at 14:51
  • I am also looking for this – Hakan Fıstık Commented Sep 2, 2016 at 11:23
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250322a4618639.html

相关推荐

  • javascript - legend alignment in bar graph using chartist - Stack Overflow

    I am trying to create legends for a bar chart using chartist, i have used chartist plugin for legend, b

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信