In Chart.js set chart title, name of x axis and y axis?
Asked Answered
L

11

140

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.

Linearity answered 12/1, 2015 at 21:5 Comment(1)
For the reference - https://mcmap.net/q/167949/-chart-js-formatting-y-axisNotecase
P
279

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.

Penna answered 30/4, 2016 at 11:29 Comment(3)
yes the sytax is working fine even after converting it into the laravel chart library options.thanks a lot i have been looking for the correct syntx and i got itHeathcote
Is there are way to show the axis label but hide the scale ?Nordgren
Well i have found how to display axis label without showing scale itself.Nordgren
P
66

For Chart.js version 3

options = {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Your Title'
      }
    }
  }     
}
Poly answered 21/4, 2021 at 8:56 Comment(2)
My browser generates an error because y should be an object and not an array. However, if I remove the square brackets it works.Mammiemammiferous
@DaveF Yes, version 3 changes y from an array to an object.Buttonhole
E
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,
            }
        }
    }
}
Extrauterine answered 22/6, 2021 at 18:38 Comment(1)
And where in the chart object should this option be set... ?Rugger
D
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'
          }
        } ]
      }
    }
  };
Dollop answered 12/10, 2018 at 1:12 Comment(0)
A
9

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,
    },
  }
}
Automatic answered 30/1, 2023 at 9:13 Comment(0)
N
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

Nuncle answered 16/10, 2021 at 7:46 Comment(1)
Doesn't work for me. Can you provide a working fiddle?Sissified
B
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>
Biagio answered 12/5, 2017 at 0:54 Comment(1)
I'm using Chart_2.5.0.min.js you can have it here: cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.jsBiagio
R
2

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

Resent answered 29/3, 2023 at 20:11 Comment(0)
C
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',
          },
        },
      ],
    },
  };
Camfort answered 11/3, 2022 at 13:0 Comment(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.Sissified
B
0
          <Scatter
            data={data}
            // style={{ width: "50%", height: "50%" }}
            options={{
              scales: {
                yAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Probability",
                    },
                  },
                ],
                xAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Hours",
                    },
                  },
                ],
              },
            }}
          />
Bernicebernie answered 23/9, 2020 at 7:41 Comment(0)
A
0

Adding to Marimuthu's answer.

I had similar situation where I was using the V 4.4.0 and was missing the Title's for X and Y axes. The issue was importing the wrong CDN/file.

I used umd version and it all worked fine. If anyone using CDN, make sure you use (similar):

https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.0/chart.umd.min.js
Alluvium answered 26/10, 2023 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.