Search code examples
canvasprimefacesjsf-2chart.js

How to change axis font in PrimeFaces and Chart.js


I am using PrimeFaces 7, and I am trying to use barchart from Chart.js as follows:

enter image description here

Code:

<p:barChart  widgetVar="cfg" model="#{dashboardBean.barModel}" style="width:500px;height:348px;"/>

and I create my model as follows:

private void createBarModel(Map<String, Double> delegatesMap) {
    barModel = new BarChartModel();
    barModel.setExtender("skinBar");
    // ...
}

I am trying to change the axis font family using extender function as follows:

function skinBar() {
        var options = $.extend(true, {}, this.cfg.config);

    options = {
            scales: {
                yAxes: [{
                    ticks: {
                        fontFamily: 'Tahoma',
                        fontSize:40
                    }
                }],
                xAxes: [{
                    ticks: {
                        fontFamily: 'Tahoma',
                        fontSize:40
                    }
                }]
            }
        }

       
       $.extend(true, this.cfg.config, options);
    }

but it's not working. How can I fix it?


Solution

  • It worked by changing my extender function as follows.

    Model:

    barModel = new BarChartModel();
    barModel.setExtender("skinBar");
    

    Extender function:

    function skinBar() {
        Chart.defaults.global.defaultFontFamily = 'Tahoma';
    }
    

    reference: https://www.chartjs.org/docs/latest/general/fonts.html