繁体   English   中英

如何创建纵轴(Y 轴)为字符串的图表?

[英]How to create a graph where the vertical axis (Y-axis) is a string?

我要制作的图表是使用 Chart.js 制作的折线图,纵轴为字符串,横轴为数字。 例如,横轴是时间,纵轴是帽子的颜色。 见下图

var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21],//Number of frames
    datasets: [{
      label: 'The colour of the hat of the person in the frame',
      data: ["red","blue","red","blue","yeallow","red","blue","red","blue","yeallow",.....],
      borderColor: '#f88',
    }],
  }
});

图形图像

是的,您必须将您的 y 比例设置为类别,还为其提供一个标签数组,然后在您的数据数组中您可以提及这些标签:

 const options = { type: 'line', data: { labels: [1, 2, 3, 4, 5, 6], datasets: [{ label: '# of Points', data: ["Blue", "Red", "Red", "Orange", "Green", "Orange"], borderColor: 'pink' }] }, options: { scales: { y: { type: 'category', labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"] }, } } } 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM