при щелчке элемента легенды в chart.js мой бар скрыт, но значение бара не скрывается

#javascript #jquery #canvas #chart.js

#javascript #jquery #холст #chart.js

Вопрос:

это мой 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
                        }
                    }]
                }
            }
        });
 

введите описание изображения здесь

Комментарии:

1. проясните свой вопрос, добавьте скриншот вашего отказа

2. дорогой Али, спасибо за вашу помощь, снимок экрана отсюда введите i.stack.imgur.com/1W0Vj.jpg

Ответ №1:

вы переопределили метод onClick по умолчанию для легенды legend: { display: true, position: "bottom", onClick: null } , удалите onClick: null и повторите попытку. Тогда это должно сработать

РЕДАКТИРОВАТЬ: Уважаемый, в вашем послерисовке вы никогда не проверяете, скрыт ли набор данных, вы всегда рисуете текст. И поскольку данные все еще находятся в объекте данных, он просто нарисует их

Комментарии:

1. дорогой, я отключаю это свойство, потому что неправильно отображается, когда onClick имеет значение true. T вы можете показать результат на этой картинке, которую я прикрепил. i.stack.imgur.com/1W0Vj.jpg