【问题标题】:when click legend item in chart.js my bar is hiden but value of bar not hide当单击chart.js中的图例项时,我的栏被隐藏但栏的值不隐藏
【发布时间】:2021-02-02 12:52:46
【问题描述】:

这是我的 html 代码:

<div class="chart col-md-12">
 <canvas id="bar-chart-grouped2" style="min-height: 250px; height: 250px; max-height: 400px; max-width: 100%;margin-top:1%;"></canvas>
</div>

和chartjs代码

new Chart(document.getElementById("bar-chart-grouped"), {
               type: 'bar',
               data: {
                   labels:  <%=Newtonsoft.Json.JsonConvert.SerializeObject(Date_Solar_Index)%>,
                datasets: [
                    {
                        label: "Total_Recived",
                        backgroundColor: "#17a2b8",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data:<%=Newtonsoft.Json.JsonConvert.SerializeObject(POID_MMS_Index)%>,
                    }, {
                        label: "valideted",
                        backgroundColor: "#28a745",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data:  <%=Newtonsoft.Json.JsonConvert.SerializeObject(COW_POID_MMS_Index)%>,
                    }, {
                        label: "Total_rejected",
                        backgroundColor: "#ff0000",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data: <%=Newtonsoft.Json.JsonConvert.SerializeObject(MNP_POID_MMS_Index)%>,
                    }, {
                        label: "Total_Back log",
                        backgroundColor: "#ffc107",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data:  <%=Newtonsoft.Json.JsonConvert.SerializeObject(Supervisions_Index)%>,
                    }, {
                        label: "Total_Back log",
                        backgroundColor: "#B60A3B",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data:  <%=Newtonsoft.Json.JsonConvert.SerializeObject(INDEXED_Index)%>,
                    }, {
                        label: "Total_Back log",
                        backgroundColor: "#72098E",
                        fillColor: "rgba(151,187,205,0.5)",
                        borderColor: '#e2e2e2',
                        borderWidth: '2',
                        data:  <%=Newtonsoft.Json.JsonConvert.SerializeObject(Reject_Index)%>,
                    }
                ]
            },
            options: {
                maintainAspectRatio: false,
                responsive: true,
                title: {
                    display: true,
                    text: ''
                },
                legend: { display: true, position: "bottom", onClick: null },
                hover: {
                    animationDuration: 0
                },


                animation: {
                    onComplete: function () {
                        var chartInstance = this.chart;
                        var ctx = chartInstance.ctx;
                        ctx.response = true;
                        ctx.textAlign = "center";
                        ctx.font = "bold  Arial";
                        //ctx.fillStyle = "black";
                        Chart.helpers.each(this.data.datasets.forEach(function (dataset, i) {
                            var meta = chartInstance.controller.getDatasetMeta(i);
                            Chart.helpers.each(meta.data.forEach(function (bar, index) {
                                ctx.save();
                                // Translate 0,0 to the point you want the text
                                ctx.translate(bar._model.x, bar._model.y - 20);

                                // Rotate context by -90 degrees
                                ctx.rotate(-0.5 * Math.PI);

                                // Draw text
                                ctx.fillText(dataset.data[index], 0, 0);
                                ctx.restore();
                            }), this)
                        }), this);
                    }
                },
                scales: {
                    xAxes: [{
                        gridLines: {
                            display: false
                        }
                    }],
                    yAxes: [{
                        gridLines: {
                            color: "rgba(0, 0, 0, 0)",

                        },
                        id: 'B',
                        type: 'linear',
                        display: false,
                        position: 'left',

                    }, {
                        gridLines: {
                            color: "rgba(0, 0, 0, 0)",
                        },
                        id: 'A',
                        type: 'linear',
                        position: 'right',
                        display: false,
                        ticks: {

                            max: 101,
                            min: 0
                        }
                    }]
                }
            }
        });

enter image description here

【问题讨论】:

标签: javascript jquery canvas chart.js


【解决方案1】:

您已覆盖图例 legend: { display: true, position: "bottom", onClick: null } 的默认 onClick 方法,删除 onClick: null 并重试。它应该可以工作

编辑:亲爱的,在您的 afterDraw 中,您从不检查数据集是否隐藏,您总是绘制文本。而且由于数据仍在数据对象中,它只会绘制它

【讨论】:

  • 亲爱的我禁用了这个属性,因为当 onClick 为真时显示不正确 T 你可以在我附加的这张图片中显示结果。 i.stack.imgur.com/1W0Vj.jpg
猜你喜欢
  • 2014-06-02
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2012-01-12
相关资源
最近更新 更多