1

我有一个帕累托图(使用 Chart.js 版本:2.0.2),我需要在条形图内和线下显示值。

任何帮助将不胜感激:

这是代码(希望它对某人有用):

<canvas id="canvas"></canvas>

<script type="text/javascript">
    var data = {
        labels: ["8","7","9","11","10"],
        datasets: [{
            type: "line",
            label: "Acumulado",
            borderColor: "#BA1E14",
            backgroundColor: "#BA1E14",
            pointBorderWidth: 5,
            fill: false,
            data: [34.04,57.45,76.60,89.36,100.00],
            yAxisID: 'y-axis-2'
        },{
            type: "bar",
            label: "Asistencia",
            borderColor: "#56B513",
            backgroundColor: "#56B513",
            data: [16,11,9,6,5],
            yAxisID: 'y-axis-1'
        },{
            type: "bar",
            label: "Solución",
            borderColor: "#000FAA",
            backgroundColor: "#000FAA",
            data: [16,11,9,6,5],
            yAxisID: 'y-axis-1'
        }]
    };

    var options = {
        scales: {
            xAxes: [{
                stacked: true,
                scaleLabel: {
                    display: true,
                    labelString: "Estaciones"
                }
            }],

            yAxes: [{
                type: "linear",
                position: "left",
                id: "y-axis-1",
                stacked: true,
                ticks: {
                    suggestedMin: 0
                },
                scaleLabel: {
                    display: true,
                    labelString: "Minutos"
                }
            },{
                type: "linear",
                position: "right",
                id: "y-axis-2",
                ticks: {
                    suggestedMin: 0,
                    callback: function(value) {
                        return value + "%";
                    }
                },
                scaleLabel: {
                    display: true,
                    labelString: "Porcentaje"
                }
            }]
        }
    };

    window.onload = function() {
        var ctx = document.getElementById("canvas").getContext("2d");

        window.myBar = new Chart(ctx, {
            type: 'bar',
            data: data,
            options: options
        });
    };
</script>

现在图表看起来像这样: 在此处输入图像描述 我需要它看起来像这样: 在此处输入图像描述

4

2 回答 2

0

这是我开发的答案(仅在选项对象中):

var options = {
    animation: {
        onComplete: function() {
            var ctx = this.chart.ctx;
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";

            this.chart.config.data.datasets.forEach(function(dataset) {
                ctx.font = "20px Arial";
                switch (dataset.type) {
                    case "line":
                        ctx.fillStyle = "Black";
                        dataset.metaData.forEach(function(p) {
                            ctx.fillText(p._chart.config.data.datasets[p._datasetIndex].data[p._index], p._model.x, p._model.y - 20);
                        });
                        break;
                    case "bar":
                        ctx.fillStyle = "White";
                        dataset.metaData.forEach(function(p) {
                            ctx.fillText(p._chart.config.data.datasets[p._datasetIndex].data[p._index], p._model.x, p._model.y  + 20);
                        });
                        break;
                }
            });
        }
    }
...
};

这是结果:

在此处输入图像描述

于 2016-05-02T23:31:18.823 回答
0

我认为从 2.6.0 版开始可以混合 ChartTypes,比如 Bars 和 Lines

按照说明进行操作

一个简短的例子是 2 个 DataSet,一个是 bar 类型,另一个是 line 类型:

const data = {
  labels: [
    'January',
    'February',
    'March',
    'April'
  ],
  datasets: [{
    type: 'bar',
    label: 'Bar Dataset',
    data: [10, 20, 30, 40],
    borderColor: 'rgb(255, 99, 132)',
    backgroundColor: 'rgba(255, 99, 132, 0.2)'
  }, {
    type: 'line',
    label: 'Line Dataset',
    data: [50, 50, 50, 50],
    fill: false,
    borderColor: 'rgb(54, 162, 235)'
  }]
};

您只需要计算 Pareto 值并放入 Dataset Line Type

于 2021-06-25T13:22:40.190 回答