0

我有一个水平条形图,显示如下:

在此处输入图像描述

由于条形图上的第二个数据值(1.0、0.8 等)被部分遮挡,我想按优先顺序执行以下操作之一:

  1. 将它们向左移动,使它们完全可见
  2. 将它们的字体从白色改为背面,使它们完全可见
  3. 完全删除它们,使它们完全不可见

导致它们被写在第一个(第二个?)位置的代码是这样的:

    Chart.pluginService.register({
        afterDraw: function (chartInstance) {
            if (chartInstance.id !== 1) return; // affect this one only
            var ctx = chartInstance.chart.ctx;
            // render the value of the chart above the bar
            ctx.font = Chart.helpers.fontString(14, 'bold', 
Chart.defaults.global.defaultFontFamily);
            ctx.textAlign = 'center';
            ctx.textBaseline = 'bottom';

            chartInstance.data.datasets.forEach(function (dataset) {
                for (var i = 0; i < dataset.data.length; i++) {
                    var model = dataset._meta[Object.keys(dataset._me
[0]].data[i]._model;
                    ctx.fillText(dataset.data[i]
(Number.isInteger(dataset.data[i]) ? ".0" : "") + "%", ((model.x
model.base) / 2), model.y + (model.height / 3));
                }
            });
        }
    });

...但我没有看到我可以在哪里根据需要操纵这些值。

对于上下文和全面披露,这里是图表的所有代码:

        Chart.pluginService.register({
            afterDraw: function (chartInstance) {
                if (chartInstance.id !== 1) return; // affect this one only
                var ctx = chartInstance.chart.ctx;
                // render the value of the chart above the bar
                ctx.font = Chart.helpers.fontString(14, 'bold'
Chart.defaults.global.defaultFontFamily);
                ctx.textAlign = 'center';
                ctx.textBaseline = 'bottom';

                chartInstance.data.datasets.forEach(function (dataset) {
                    for (var i = 0; i < dataset.data.length; i++) {
                        var model = dataset._meta[Object.keys(dataset._met
[0]].data[i]._model;
                        ctx.fillText(dataset.data[i] 
(Number.isInteger(dataset.data[i]) ? ".0" : "") + "%", ((model.x 
model.base) / 2), model.y + (model.height / 3));
                    }
                });
            }
        });

        var ctxBarChart 
$("#priceComplianceBarChart").get(0).getContext("2d");
        var priceComplianceData = {
            labels: [
                "Bix Produce", "Capitol City", "Charlies Portland", "Cost
Fruit and Produce",
                "Get Fresh Sales",
                "Loffredo East", "Loffredo West", "Paragon", "Piazz
Produce"
            ],
            datasets: [
            {
                label: "Price Compliant",
                backgroundColor: "rgba(34,139,34,0.5)",
                hoverBackgroundColor: "rgba(34,139,34,1)",
                data: [99.0, 99.2, 99.4, 98.9, 99.1, 99.5, 99.6, 99.2, 99.7]
            },
            {
                label: "Non-Compliant",
                backgroundColor: "rgba(255, 0, 0, 0.5)",
                hoverBackgroundColor: "rgba(255, 0, 0, 1)",
                data: [1.0, 0.8, 0.6, 1.1, 0.9, 0.5, 0.4, 0.8, 0.3]
            }
            ]
        }

        var priceComplianceOptions = {
            scales: {
                xAxes: [
                {
                    stacked: true
                }
                ],
                yAxes: [
                {
                    stacked: true
                }
                ]
            },
            tooltips: {
                enabled: false
            }
        };

        var priceBarChart = new Chart(ctxBarChart,
        {
            type: 'horizontalBar',
            data: priceComplianceData,
            options: priceComplianceOptions
        });

我正在使用 Chart.js 版本 2.2.2

4

1 回答 1

1

第一种解决方案:向左移动

在您的插件中,将 contexttextAlign属性设置为right如果它是第二个数据集:

chartInstance.data.datasets.forEach(function(dataset) {
    for (var i = 0; i < dataset.data.length; i++) {
        var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model;

        // If it is the second dataset (red color) ..
        if (dataset._meta[0].controller.index== 1) {
            // .. align to the right
            ctx.textAlign ="right";
            // .. and write it at the right bound of the chart
            ctx.fillText(parseFloat(dataset.data[i]).toFixed(2) + "%", (model.x - 2), (model.y + model.height / 3));

           // This looks like it has been moved a bit to the left
        }

        // Else ..
        else {
            // .. write as usual
            ctx.fillText(parseFloat(dataset.data[i]).toFixed(2) + "%", ((model.base + model.x) / 2), (model.y + model.height / 3));
        }
    }
});

检查这个 jsFiddle 上的结果。


第二种解决方案:将文本置于黑色

在您的插件中,将 contextfillStyle属性设置为您想要的颜色(#000 例如):

afterDraw: function(chartInstance) {
    var ctx = chartInstance.chart.ctx;
    // render the value of the chart above the bar
    ctx.font = Chart.helpers.fontString(14, 'bold',
        Chart.defaults.global.defaultFontFamily);
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';

    // Here:
    ctx.fillStyle = "#000";

    chartInstance.data.datasets.forEach(function(dataset) {
        // ...
    });
});

检查这个 jsFiddle 上的结果。


第三种解决方案:删除它,简单明了

在您的插件中添加一个条件以检查您当前正在处理的数据集:

chartInstance.data.datasets.forEach(function(dataset) {
    for (var i = 0; i < dataset.data.length; i++) {

        // If it is the second dataset (red color), we break out of the loop
        if (dataset._meta[0].controller.index == 1) break;

        var model = dataset._meta[Object.keys(dataset._meta)[0]].data[i]._model;

        ctx.fillText(parseFloat(dataset.data[i]).toFixed(2) + "%", ((model.base + model.x) / 2), (model.y + model.height / 3));
    }
});

检查这个 jsFiddle 上的结果。

于 2016-09-30T08:23:35.340 回答