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

eoxn13cs  于 2022-11-06  发布在  Chart.js
关注(0)|答案(8)|浏览(246)

Chart.js(documentation)是否可以为数据集设置图表的名称(标题)(例如我所在城市的温度)、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]
        }
    ]

}

真的谢谢你的帮助。

0qx6xfy6

0qx6xfy61#

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

options = {
  scales: {
    yAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'probability'
      }
    }]
  }     
}

更多详细信息,请参见标签文档。

42fyovps

42fyovps2#

适用于Chart.js版本3

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Your Title'
      }
    }
  }     
}
uurity8g

uurity8g3#

如果你已经像@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'
          }
        } ]
      }
    }
  };
31moq8wy

31moq8wy4#

对于2021年的读者:

版本

"chart.js": "^3.3.2",

真实的世界工作样本:

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,
            }
        }
    }
}
xienkqul

xienkqul5#

在图表JS 3.5.x中,我认为轴的标题应设置如下(例如,x轴,标题=“秒”):

options: {
  scales: {x: { title: { display: true, text: 'seconds' }}}
}

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

sh7euo9m

sh7euo9m6#

就用这个:

<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>
a2mppw5e

a2mppw5e7#

<Scatter
            data={data}
            // style={{ width: "50%", height: "50%" }}
            options={{
              scales: {
                yAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Probability",
                    },
                  },
                ],
                xAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Hours",
                    },
                  },
                ],
              },
            }}
          />
gwbalxhn

gwbalxhn8#

请参见example,其名称为x轴和y轴左右。

public barChartOptions: ChartOptions = {
    title: {
      display: true,
      text: 'Custom Chart Title',
    },
    responsive: true,
    legend: {
      position: 'right',
    },
    scales: {
      yAxes: [
        {
          position: 'right',
          scaleLabel: {
            display: true,
            labelString: 'Frequency Rate - right',
          },
        },
        {
          position: 'left',
          scaleLabel: {
            display: true,
            labelString: 'Frequency Rate - left',
          },
        },
      ],
      xAxes: [
        {
          display: true,
          position: 'bottom',
          scaleLabel: {
            display: true,
            labelString: 'Titulo do eixo X',
            fontStyle: 'italic',
            fontSize: 12,
            fontColor: '#030',
          },
        },
      ],
    },
  };

相关问题