138

Does Chart.js (documentation) have option for datasets to set name (title) of chart (e.g. Temperature in my City), name of x axis (e.g. Days) and name of y axis (e.g. Temperature). Or I should solve this with 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]
        }
    ]

}

Realy thanks for help.

DaniKR
  • 2,418
  • 10
  • 39
  • 48

10 Answers10

278

In Chart.js version 2.0, it is possible to set labels for axes:

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

See Labelling documentation for more details.

andyhasit
  • 14,137
  • 7
  • 49
  • 51
55

For Chart.js version 3

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Your Title'
      }
    }
  }     
}
Avatar
  • 14,622
  • 9
  • 119
  • 198
Ayenew Yihune
  • 1,059
  • 13
  • 19
17

For Readers in 2021:

Version

"chart.js": "^3.3.2",

Real World Working Sample:

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,
            }
        }
    }
}
NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
16

If you have already set labels for your axis like how @andyhasit and @Marcus mentioned, and would like to change it at a later time, then you can try this:

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

Full config for reference:

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'
          }
        } ]
      }
    }
  };
Sahil Gandhi
  • 201
  • 2
  • 7
8

In chart JS 3.5.x, it seems to me the title of axes shall be set as follows (example for x axis, title = 'seconds'):

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

see: https://www.chartjs.org/docs/latest/axes/labelling.html

Giorgio Barchiesi
  • 6,109
  • 3
  • 32
  • 36
7

just use this:

<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>
WasiF
  • 26,101
  • 16
  • 120
  • 128
no.name.added
  • 359
  • 4
  • 4
6

If you are using chart JS v4 try this.

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Test'
      }
    },
    x: {
      title: {
        display: true,
        text: 'Test'
      }
    }
  },
  plugins: {
    legend: {
      display: false,
    },
  }
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Marimuthu
  • 61
  • 1
  • 4
1

See example with name of x axis and y axis left and right.

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',
          },
        },
      ],
    },
  };
FXLima
  • 337
  • 2
  • 5
  • 1
    Thank you for posting this (as it contains the answer I used) but please don't post link only answers. The links can rot and it is better if you can include the relevant information here. – rob Mar 25 '22 at 10:58
1

To name the axis:

  1. inside new Chart()
  2. Go to options (or create it):

options: {

    scales: {
      x: {
        title: { display: true, text: 'X axis label' }
      },
      y: {
        title: { display: true, text: 'Y axis label'}
      }
    },   }

Remember: new Chart() -> options -> scales -> x or y -> title

SomeOne
  • 51
  • 4
0
          <Scatter
            data={data}
            // style={{ width: "50%", height: "50%" }}
            options={{
              scales: {
                yAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Probability",
                    },
                  },
                ],
                xAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Hours",
                    },
                  },
                ],
              },
            }}
          />
Fakabbir Amin
  • 989
  • 7
  • 16