1. <small id='UsfZC'></small><noframes id='UsfZC'>

      <i id='UsfZC'><tr id='UsfZC'><dt id='UsfZC'><q id='UsfZC'><span id='UsfZC'><b id='UsfZC'><form id='UsfZC'><ins id='UsfZC'></ins><ul id='UsfZC'></ul><sub id='UsfZC'></sub></form><legend id='UsfZC'></legend><bdo id='UsfZC'><pre id='UsfZC'><center id='UsfZC'></center></pre></bdo></b><th id='UsfZC'></th></span></q></dt></tr></i><div id='UsfZC'><tfoot id='UsfZC'></tfoot><dl id='UsfZC'><fieldset id='UsfZC'></fieldset></dl></div>
      <tfoot id='UsfZC'></tfoot><legend id='UsfZC'><style id='UsfZC'><dir id='UsfZC'><q id='UsfZC'></q></dir></style></legend>

        • <bdo id='UsfZC'></bdo><ul id='UsfZC'></ul>

        Chart.js 将标签变成链接

        时间:2023-11-02
        • <i id='FJu3g'><tr id='FJu3g'><dt id='FJu3g'><q id='FJu3g'><span id='FJu3g'><b id='FJu3g'><form id='FJu3g'><ins id='FJu3g'></ins><ul id='FJu3g'></ul><sub id='FJu3g'></sub></form><legend id='FJu3g'></legend><bdo id='FJu3g'><pre id='FJu3g'><center id='FJu3g'></center></pre></bdo></b><th id='FJu3g'></th></span></q></dt></tr></i><div id='FJu3g'><tfoot id='FJu3g'></tfoot><dl id='FJu3g'><fieldset id='FJu3g'></fieldset></dl></div>
            <legend id='FJu3g'><style id='FJu3g'><dir id='FJu3g'><q id='FJu3g'></q></dir></style></legend>
          • <small id='FJu3g'></small><noframes id='FJu3g'>

              • <bdo id='FJu3g'></bdo><ul id='FJu3g'></ul>
                <tfoot id='FJu3g'></tfoot>

                    <tbody id='FJu3g'></tbody>
                  本文介绍了Chart.js 将标签变成链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果不执行以下操作,我不确定这是否可行:在HTML 画布,但让我们确定一下.

                  I'm not sure this is possible without doing something along the line of: Create links in HTML canvas but let's make sure.

                  有没有办法(相对)简单地将 Chart.js 标签转换为链接?有问题的图表是雷达图:http://www.chartjs.org/docs/#雷达图(到目前为止,我一直在使用图例,只需稍加修改库即可正常工作,但现在我应该使用标签本身.)

                  Is there a way to (relatively) simply turn Chart.js labels into links? The chart in question is the radar chart: http://www.chartjs.org/docs/#radar-chart (So far I've been using the legend for that, works fine with a little library modification, but now I should use the labels themselves.)

                  推荐答案

                  你可以监听点击事件,然后遍历所有的pointLabels,检查点击是否在那个框内.如果是这种情况,您可以从包含所有标签的数组中获取相应的标签.

                  You can listen to the click event and then loop through all the pointLabels check if the click is in that box. If this is the case you get the corresponding label from the array containing all the labels.

                  然后你可以打开使用 window.location = linkwindow.open(link) 去你的链接.

                  Then you can open use window.location = link or window.open(link) to go to your link.

                  点击后在 google 上搜索颜色的示例:

                  Example that searches the color on google on click:

                  const options = {
                    type: 'radar',
                    data: {
                      labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                      datasets: [{
                          label: '# of Votes',
                          data: [12, 19, 3, 5, 2, 3],
                          borderColor: 'orange'
                        },
                        {
                          label: '# of Points',
                          data: [7, 11, 5, 8, 3, 7],
                          borderColor: 'pink'
                        }
                      ]
                    },
                    options: {
                      onClick: (evt, activeEls, chart) => {
                        const {
                          x,
                          y
                        } = evt;
                        let index = -1;
                  
                        for (let i = 0; i < chart.scales.r._pointLabelItems.length; i++) {
                          const {
                            bottom,
                            top,
                            left,
                            right
                          } = chart.scales.r._pointLabelItems[i];
                  
                          if (x >= left && x <= right && y >= top && y <= bottom) {
                            index = i;
                            break;
                          }
                        }
                  
                        if (index === -1) {
                          return;
                        }
                  
                        const clickedLabel = chart.scales.r._pointLabels[index];
                  
                        window.open(`https://www.google.com/search?q=color%20${clickedLabel}`); // Blocked in stack snipet. See fiddle
                        console.log(clickedLabel)
                      }
                    }
                  }
                  
                  const ctx = document.getElementById('chartJSContainer').getContext('2d');
                  new Chart(ctx, options);

                  <body>
                    <canvas id="chartJSContainer" width="600" height="400"></canvas>
                    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
                  </body>

                  小提琴:https://jsfiddle.net/Leelenaleee/fnqr4c7j/22/

                  这篇关于Chart.js 将标签变成链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:chart.js 图表上的叠加线 下一篇:如何根据标签更改 Chart.js 点的颜色

                  相关文章

                    <bdo id='80unx'></bdo><ul id='80unx'></ul>
                    <tfoot id='80unx'></tfoot>
                  1. <small id='80unx'></small><noframes id='80unx'>

                    <i id='80unx'><tr id='80unx'><dt id='80unx'><q id='80unx'><span id='80unx'><b id='80unx'><form id='80unx'><ins id='80unx'></ins><ul id='80unx'></ul><sub id='80unx'></sub></form><legend id='80unx'></legend><bdo id='80unx'><pre id='80unx'><center id='80unx'></center></pre></bdo></b><th id='80unx'></th></span></q></dt></tr></i><div id='80unx'><tfoot id='80unx'></tfoot><dl id='80unx'><fieldset id='80unx'></fieldset></dl></div>
                      <legend id='80unx'><style id='80unx'><dir id='80unx'><q id='80unx'></q></dir></style></legend>