3

我正在尝试使用 chartist 为条形图创建图例,我使用 chartist 插件作为图例,但我无法将它与图表底部对齐,任何人都可以提供有关如何自定义的输入。(我曾尝试在 css 中使用它,但无法反映我的更改。)

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()
    ]
};

传奇.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);
        }
    }
}

也有人可以让我知道如何在条形顶部添加 y 轴值。我尝试使用下面的 css 文件但仍然无法获取标签(类似于 Chartist.plugins.ctPointLabels)但似乎它仅适用于折线图。

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;
}

提前致谢。

4

1 回答 1

5

这真的很简单,但文档记录很差。

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

请记住,您的图表 SVG 绝对定位于它的父级。所以你是传说中的 CSS 也需要设置绝对位置。

要获得更彻底的方法,请更换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;
    }
});

关键是:

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

这将在包含图表的元素之后插入图例。

快乐编码。

于 2016-09-16T20:28:04.937 回答