Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing fontFamily on ChartJS bar chart

I've partially implemented ChartJS in a project, but I can't figure out how to change the font that's displayed on the X and Y axes of a bar chart.

I've read the ChartJS documentation, searched for examples on GitHub, etc. I'm not sure what I'm doing wrong, but I rest assured knowing the solution will involve a line of code and will be a startlingly stupid oversight on my part.

This code draws the chart I want, but with the default font:

var barChartLanguage = Chart.Bar(myCanvas, {
    data: dataLanguages,
    options: options,
    defaults: defaults
});

I tried changing the font in the defaults without success:

var defaults = {
    global: {
        // example font
        defaultFontFamily: "'Raleway'"
    }
};

And I tried changing it on the axis with options:

var options = {
    animation: {
        duration: 2000
    },
    scales: {
        yAxes: [{
            display: true,
            ticks: {
                suggestedMin: 0,    // minimum will be 0, unless there is a lower value.
                // OR //
                beginAtZero: true,   // minimum value will be 0.
                suggestedMax: 10
            },
            gridLines: {
                display: false
            },
            pointLabels: {
                fontFamily: "'Raleway'"
            }
        }],
        xAxes: [{
            gridLines: {
                display: false
            }
        }],
    },
};
like image 983
Adrian Avatar asked May 28 '16 21:05

Adrian


People also ask

How do I change the font on a chart?

It says "There are 4 special global settings that can change all of the fonts on the chart. These options are in Chart.defaults.global ". You'll need to change defaultFontFamily for the font. And defaultFontColor, defaultFontSize, and defaultFontStyle for color, size, etc. Show activity on this post.

How to add a font-family to a chart?

If you wanted to add the font-family to the chart object then you can add it in the options object. options: { legend: { labels: { fontFamily: 'YourFont' } }...}

Why are some fonts not showing up in my charts?

If a font is specified for a chart that does exist on the system, the browser will not apply the font when it is set. If you notice odd fonts appearing in your charts, check that the font you are applying exists on your system. See issue 3318 for more details.

How do I create a horizontal bar chart?

A horizontal bar chart is a variation on a vertical bar chart. It is sometimes used to show trend data, and the comparison of multiple data sets side by side. To achieve this you will have to set the indexAxis property in the options object to 'y'. The default for this property is 'x' and thus will show vertical bars.


2 Answers

Add ticks.fontFamily to xAxes so it will look like this:

xAxes: [{
  gridLines: {
    display: false
  },
  ticks: {
    fontFamily: "Verdana",
  }
}],

Documentation: http://www.chartjs.org/docs/#scales

Example on jsfiddle: http://jsfiddle.net/4asLpwd5/

like image 177
vegazz Avatar answered Sep 20 '22 18:09

vegazz


From version 3.x, onwards the syntax was changed.

Chart.js migration guide: 3.x Migration Guide

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'line',
    data: chartData,
    options: {
        responsive: true,
        plugins: {
            legend: {
                display: false,
            }
        },
        scales: {
            x: {
                grid: {
                    display: false
                },
                ticks: {
                    font: {
                        family: 'Raleway', // Your font family
                        size: 14,
                    },
                },
            },
            y: {
                beginAtZero: true,
                ticks: {
                    font: {
                        family: 'Raleway', // Your font family
                        size: 14,
                    },
                    // Include a dollar sign in the ticks
                    callback: function (value, index, values) {
                        return '$' + value;
                    },
                },
            }
        }
    }
});
like image 36
Karthikeyan P Avatar answered Sep 21 '22 18:09

Karthikeyan P