在Chart.js中更改饼图和折线图的标签字体族。

3
我想改变线图和饼图中图例和标签的字体,我该怎么做? 我几乎尝试了所有方法,但很遗憾没有起作用。 我尝试了以下方法:
            <Pie
                data={state}
                defaultValue={resources.length + other}
                plugins={{
                    //@ts-ignore
                    font: {
                        weight: 'bold',
                        size: 100,
                        family: 'IranYekanFa',
                    }
                }}
                options={{
                    title: {
                        display: true,

                        text: "",
                        fontColor: 'rgb(160,0,255)',
                        fontSize: 30,
                        fontFamily:"IranYekanFa",
                    },
                    legend: {
                        labels: {
                            fontFamily:"IranYekanFa !important" ,
                        }
                    },
                    animation: {
                        duration: 0, // general animation time
                    },

                }}
            />

甚至尝试使用 CSS:

.piechart-parent-div{
    font-family:"IranYekanFa" !important;
}

什么都没用!

1个回答

1

由于字体未更改,因此似乎您正在使用V3版本。 V3具有一些重大的变化,例如如何定义比例尺以及名称空间中的更改,因此您的标题和图例配置不正确。另外,您定义字体的方式也是错误的。有关所有更改,请阅读迁移指南

更改后的工作示例:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        labels: {
          font: {
            family: 'Comic Sans MS',
          }
        }
      },
    },
    scales: {
      x: {
        ticks: {
          font: {
            family: 'Comic Sans MS'
          },
          reverse: false
        }
      }
    }
  }
}

var 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.5.1/chart.js"></script>
</body>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接