【问题标题】:Show center tick value radar chart- Chart.js显示中心刻度值雷达图 - Chart.js
【发布时间】:2022-01-22 01:37:27
【问题描述】:

我有一张雷达图,我想在图表中心显示刻度的值。我的r规模设置如下,我的数据是1-5的整数。

    scales: {
      r: {
        min: 1,
        max: 5,
        ticks: {
          stepSize: 1,
          z: 10,
        },
      },
    },

在雷达图的中心,我希望显示刻度值,就像显示所有其他值一样。将我的min 设置为 0(比我的实际最小值小一个步长)会显示我的刻度,但我希望标记图表的中心,而不是在中心和第一个刻度之间设置最小尺寸。

【问题讨论】:

    标签: chart.js


    【解决方案1】:

    您需要使用自定义插件自己在画布上绘制它,如下所示:

    const options = {
      type: 'radar',
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            borderColor: 'pink'
          },
          {
            label: '# of Points',
            data: [7, 11, 5, 8, 3, 7],
            borderColor: 'orange'
          }
        ]
      },
      options: {
        plugins: {
          centerTick: {
            //color: 'grey' // Set custom color for y
          }
        }
      },
      plugins: [{
        id: 'centerTick',
        afterDraw: (chart, args, opts) => {
          const {
            ctx
          } = chart;
          ctx.fillStyle = opts.color || Chart.defaults.color;
          ctx.fillText(chart.scales.r.min, chart.scales.r.xCenter - (ctx.measureText(chart.scales.r.min).width / 2), chart.scales.r.yCenter)
        }
      }]
    }
    
    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.7.0/chart.js"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2017-03-07
      • 2023-03-06
      相关资源
      最近更新 更多