71

In chart.js how can I set the set the font size for just the x axis labels without touching global config?

I've already tried setting the 'scaleFontSize' option my options object. I've also tried setting:

{
  ...
  scales: {
    xAxes: [{
      scaleFontSize: 40
      ...
    }]
   }
}
dingdingding
  • 1,411
  • 1
  • 15
  • 23
  • 1
    https://stackoverflow.com/a/48580585/7186739 – Billu Feb 02 '18 at 10:24
  • 2
    The title should be changed, since the question and the answers focus on the axis tick labels and not the actual axis label, which is confusing if you are looking to change the axis label font size. – Little geek Jun 14 '19 at 07:58

8 Answers8

141

The fontSize attribute is actually in scales.xAxes.ticks and not in scales.xAxes as you thought.

So you just have to edit the attribute like this :

var options = {
    scales: {
        yAxes: [{
            ticks: {
                fontSize: 40
            }
        }]
    }
}


You can see a fully working example in this jsFiddle and here is its result :

enter image description here

tektiv
  • 14,010
  • 5
  • 61
  • 70
65

Configuration options and properties for chartjs 3.0 has changed. Currently I'm using Chartjs 3.1.1. Fonts are used as objects now. In order to change font size of x axis ticks you have to use following configuration.

var options = {
    scales: {
        x: {
            ticks: {
                font: {
                    size: 12,
                }
            }
        }
    }
};

Checkout this jsfiddle sample.

rumman0786
  • 1,150
  • 10
  • 20
9
options: {
                  locale: 'fa-IR',
                  responsive: true, // Instruct chart js to respond nicely.
                  maintainAspectRatio: false, // Add to prevent default behaviour of full-width/height 
                        plugins: {
                          legend: {
                              position: 'top',
                              labels: {
                                  font: {
                                    size: 9,
                                    family:'vazir'
                                  }
                             }
                          },
                          title: {
                            display: true,
                            text: chart_info.chartTitle,
                            font: {
                                size: 16,
                                family:'vazir'
                            }
                          },
                          tooltip: {
                              bodyFont: {
                                size: 13,
                                family:'vazir'
                              }
                         }
                    },
                    scales: {
                        x: {
                            ticks: {
                                font: {
                                    size: 10,
                                    family:'vazir'
                                }
                            }
                        },
                        y: {
                            ticks: {
                                font: {
                                    size: 10,
                                    family:'vazir'
                                }
                            }
                        }                       
                    }
                }
M-O-H-S-E-N
  • 346
  • 4
  • 7
8

Try this is working

     options: {
        scales: {
           xAxes: [{
                   ticks: {
                    fontSize: 10
                   }
                  }]
                }
             }
NAVEEN KUMAR
  • 81
  • 1
  • 2
4

Try to see if this will work

{
  ...
  scales: {
    xAxes: [{
      fontSize: 40
      ...
    }]
   }
}

It doesn't look like scaleFontSize is a valid property.

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
1

Try this

Chart.defaults.global.defaultFontSize = 8;
Usama Fayyaz
  • 95
  • 2
  • 12
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Jun 17 '20 at 00:54
  • 1
    As of April 2023, this does not work. However the following text works for the global configuration: `Chart.defaults.font.size = 16;` Source: https://www.chartjs.org/docs/latest/general/fonts.html – Mr-IDE Apr 27 '23 at 15:41
0

Try this simple solution:

myChart.options.scales.yAxes[0].ticks.fontSize = 40 ;

myChart.update();
Abdul Hameed
  • 1,025
  • 13
  • 17
0

Currently, I'm using ^2.9.4 chart.js. I've tried other solutions posted here and did some tweak.

    options: {
    scales: {
      yAxes: [{
        ticks: {
          minor: {
             fontSize: 16
          }
        }
      }],
      xAxes: [{
        ticks: {
          minor: {
             fontSize: 16
          }
        }
      }]
     }
   }