【问题标题】:How to show/hide animation when legend clicked chart js图例单击图表js时如何显示/隐藏动画
【发布时间】:2019-04-11 10:23:51
【问题描述】:

我添加动画以在折线图 js 中的值点上显示标签值数据。但是当我单击图例隐藏一行时,标签不会消失。老实说,我真的没有任何线索来解决它。

the chart now

the chart after hide some line with legend

var ctxTotal = $("#grap_trajec_divisi");
                var chartOptions = {
                    responsive: true,
                    maintainAspectRatio: false,
                    legend: {
                            position: 'bottom',
                        },
                    tooltips: {
                        enabled: true,
                        mode: 'single',
                        callbacks: {
                            label: function (tooltipItems, data) {
                                var dataM = tooltipItems.yLabel;
                                formatM(dataM);
                                var multistringText = [data_M];
                                multistringText.push(tooltipItems.yLabel);
                                multistringText.push(data.datasets[tooltipItems.datasetIndex].label);
                                return multistringText;
                            }
                        }
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            gridLines: {
                                color: "#f3f3f3",
                                drawTicks: true,
                            },
                            scaleLabel: {
                                display: false,
                                labelString: 'Month'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            gridLines: {
                                color: "#f3f3f3",
                                drawTicks: true,
                            },
                            scaleLabel: {
                                display: false,
                                labelString: 'Value'
                            },
                            ticks: {
                                callback: function (value, index, values) {
                                    var dataYaxis = value;
                                    formatM(dataYaxis);
                                    return data_M;
                                }
                            },
                        }]
                    },
                    animation: {
                        duration: 1,
                        onComplete: function () {
                            var chartInstance = this.chart,
                                ctx = chartInstance.ctx;
                            ctx.font = '.7rem "Calibri",sans-serif';
                            ctx.fillStyle = '#555';
                            ctx.textAlign = "center";

                        this.data.datasets.forEach(function (dataset, i) {
                            var meta = chartInstance.controller.getDatasetMeta(i);
                            meta.data.forEach(function (bar, index) {
                                var data = dataset.data[index];
                                formatM(data);
                                ctx.fillText(data_M, bar._model.x, bar._model.y - 5);
                            });
                        });
                    }
                }
                //title: {
                //    display: true,
                //    text: 'Chart.js Line Chart - proyeksi'
                //}
            };
            var chartData = {
                labels: arr,
                datasets: [{
                    label: "RKAP",
                    data: value_LT,
                    fill: false,
                    borderColor: "rgb(89,159,240)",
                    pointBorderColor: "rgb(89,159,240)",
                    pointBackgroundColor: "#FFFFFF",
                    pointBorderWidth: 1,
                    pointHoverBorderWidth: 1,
                    pointRadius: 3,
                    spanGaps: true,
                }, {
                    label: "Target",
                    data: value_LT2,
                    fill: false,
                    borderColor: "rgb(186,179,61)",
                    pointBorderColor: "rgb(186,179,61)",
                    pointBackgroundColor: "#FFFFFF",
                    pointBorderWidth: 1,
                    pointHoverBorderWidth: 1,
                    pointRadius: 3,
                    spanGaps: true,
                }, {
                    label: "Actual",
                    data: value_LO,
                    fill: false,
                    borderColor: "rgb(78,199,138)",
                    pointBorderColor: "rgb(78,199,138)",
                    pointBackgroundColor: "#FFFFFF",
                    pointBorderWidth: 1,
                    pointHoverBorderWidth: 1,
                    pointRadius: 3,
                    spanGaps: true,
                }, {
                    label: "Proyeksi",
                    data: value_LP,
                    fill: false,
                    borderColor: "rgb(241,151,89)",
                    pointBorderColor: "rgb(241,151,89)",
                    pointBackgroundColor: "#FFFFFF",
                    pointBorderWidth: 1,
                    pointHoverBorderWidth: 1,
                    pointRadius: 3,
                    spanGaps: true,
                }],
            };
            var config = {
                type: 'line',
                options: chartOptions,
                data: chartData
            };
            if (window.chartTrajecDivisi != undefined) {
                window.chartTrajecDivisi.destroy();
            }
            window.chartTrajecDivisi = new Chart(ctxTotal, config);

我想通过单击标签来隐藏行隐藏时的标签,因此标签和行一起隐藏/显示。

【问题讨论】:

  • 你好@Yudha Adtia 你有一些小提琴的例子吗?
  • 这是小提琴jsfiddle.net/3qda8p1o/1
  • 你的小提琴不起作用...
  • 图表js不够用?我认为我的问题仅在图表中(动画/回调/图例)
  • 我希望看到“运行”图表以更好地帮助您

标签: javascript chart.js


【解决方案1】:

我找到了答案。只需删除动画,并使用插件数据标签图表js。

【讨论】:

  • 另一个问题,很多人会来寻求帮助,只会发现“我解决了我的问题”。
  • 同意@RomuloPBenedetti -- 请添加解决方案的详细说明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
相关资源
最近更新 更多