Browse Source

update color input

andy 4 months ago
parent
commit
aeb8d457bb
1 changed files with 15 additions and 1 deletions
  1. 15 1
      js/demo/text-to-chart.js

+ 15 - 1
js/demo/text-to-chart.js

@@ -1629,6 +1629,21 @@ function createChart(chartType, data, labels) {
                     display: false,
                     fullSize: true,
                     position: 'bottom',
+                    onClick: function (e, legendItem, legend) {
+                        e.native.stopImmediatePropagation();
+                        // 獲取被點擊的數據集索引
+                        selectedDatasetIndex = legendItem.datasetIndex;
+
+
+                        const dataset_color = legend.chart.data.datasets[selectedDatasetIndex].borderColor;
+                        console.log(dataset_color)
+
+                        colorInput.value = dataset_color;
+
+                        // // 手動觸發 Chart.js 的默認圖例點擊事件
+                        // const defaultClickHandler = Chart.defaults.plugins.legend.onClick;
+                        // defaultClickHandler.call(this, e, legendItem, legend);
+                    }
                 },
 
             },
@@ -1692,7 +1707,6 @@ function createChart(chartType, data, labels) {
                 }
             },
             onClick: function (event, elements) {
-                console.log()
                 if (elements.length > 0) {
                     const element = elements[0];
                     selectedDatasetIndex = element.datasetIndex;