在Chart.js中设置图表标题,x轴名称和y轴?

Dan*_*iKR 51 charts title chart.js

是否chart.js之(文件)有数据集设置图表的姓名(名称)(在我的城市如温度),X轴(例如天)的名称和Y轴(如温度)的名称选项.或者我应该用css解决这个问题?

var lineChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.2)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            pointHighlightFill : "#fff",
            pointHighlightStroke : "rgba(220,220,220,1)",
            data : [data]
        }
    ]

}
Run Code Online (Sandbox Code Playgroud)

真的感谢你的帮助.

and*_*sit 147

在Chart.js 2.0版中,可以为轴设置标签:

options = {
  scales: {
    yAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'probability'
      }
    }]
  }     
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅标签文档.


Aye*_*une 64

对于 Chart.js 版本 3

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Your Title'
      }
    }
  }     
}
Run Code Online (Sandbox Code Playgroud)

  • 我的浏览器生成错误,因为“y”应该是一个对象而不是数组。但是,如果我删除方括号,它就可以工作。 (10认同)

s̮̦*_*̥̳̼ 17

致 2021 年的读者:

版本

"chart.js": "^3.3.2",
Run Code Online (Sandbox Code Playgroud)

真实世界工作示例:

const optionsTemplate = (yAxisTitle, xAxisTitle) => {
    return {
        scales: {
            yAxes: {
                title: {
                    display: true,
                    text: yAxisTitle,
                    font: {
                        size: 15
                    }
                },
                ticks: {
                    precision: 0
                }
            },
            xAxes: {
                title: {
                    display: true,
                    text: xAxisTitle,
                    font: {
                        size: 15
                    }
                }
            }
        },
        plugins: {
            legend: {
                display: false,
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 13

如果您已经像@andyhasit 和@Marcus 提到的那样为轴设置了标签,并且想稍后更改它,那么您可以尝试以下操作:

chart.options.scales.yAxes[ 0 ].scaleLabel.labelString = "New Label";

完整配置供参考:

var chartConfig = {
    type: 'line',
    data: {
      datasets: [ {
        label: 'DefaultLabel',
        backgroundColor: '#ff0000',
        borderColor: '#ff0000',
        fill: false,
        data: [],
      } ]
    },
    options: {
      responsive: true,
      scales: {
        xAxes: [ {
          type: 'time',
          display: true,
          scaleLabel: {
            display: true,
            labelString: 'Date'
          },
          ticks: {
            major: {
              fontStyle: 'bold',
              fontColor: '#FF0000'
            }
          }
        } ],
        yAxes: [ {
          display: true,
          scaleLabel: {
            display: true,
            labelString: 'value'
          }
        } ]
      }
    }
  };
Run Code Online (Sandbox Code Playgroud)


小智 9

如果您使用的是 Chart JS v4,请尝试此操作。

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Test'
      }
    },
    x: {
      title: {
        display: true,
        text: 'Test'
      }
    }
  },
  plugins: {
    legend: {
      display: false,
    },
  }
}
Run Code Online (Sandbox Code Playgroud)


Gio*_*esi 8

在图表 JS 3.5.x 中,在我看来,轴的标题应设置如下(例如 x 轴,标题 = '秒'):

options: {
  scales: {x: { title: { display: true, text: 'seconds' }}}
}
Run Code Online (Sandbox Code Playgroud)

请参阅: https: //www.chartjs.org/docs/latest/axes/labelling.html


小智 7

只需使用这个:

<script>
  var ctx = document.getElementById("myChart").getContext('2d');
  var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
      labels: ["1","2","3","4","5","6","7","8","9","10","11",],
      datasets: [{
        label: 'YOUR LABEL',
        backgroundColor: [
          "#566573",
          "#99a3a4",
          "#dc7633",
          "#f5b041",
          "#f7dc6f",
          "#82e0aa",
          "#73c6b6",
          "#5dade2",
          "#a569bd",
          "#ec7063",
          "#a5754a"
        ],
        data: [12, 19, 3, 17, 28, 24, 7, 2,4,14,6],            
      },]
    },
    //HERE COMES THE AXIS Y LABEL
    options : {
      scales: {
        yAxes: [{
          scaleLabel: {
            display: true,
            labelString: 'probability'
          }
        }]
      }
    }
  });
</script>
Run Code Online (Sandbox Code Playgroud)