Override Font size

Created on 26 April 2023, over 1 year ago
Updated 23 February 2024, 10 months ago

Problem/Motivation

Hi Thank you for this module.

I am using chart.js library to display charts in my website, how can I override the canvas font size for legends and title in chart.js library.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Closed: works as designed

Version

5.0

Component

Chart.js integration

Created by

πŸ‡¨πŸ‡¦Canada mhentry Toronto

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mhentry
  • πŸ‡¨πŸ‡¦Canada mhentry Toronto

    Just an update to my question; I used following option to override the font size in my theme .js file.
    Chart.defaults.font.size = 18;
    Chart.defaults.font.weight = 500;
    Chart.defaults.font.family = "Open Sans";
    Chart.defaults.color = "#000000";

    Still trying to figure out how to reduce the width of the each legend.

  • πŸ‡ΊπŸ‡ΈUnited States andileco

    Sorry for the delay. You could do something like this in the .module file of a custom module named my_module:

    /**
     * Implements hook_chart_alter().
     */
    function my_module_chart_alter(array &$element, $chart_id) {
      $element['#raw_options'] = [
        'options' => [
          'plugins' => [
            'title' => [
              'font' => [
                'size' => 20,
                'weight' => 500,
                'family' => 'Open Sans',
              ],
              'color' => '#000000',
            ],
            'legend' => [
              'labels' => [
                'font' => [
                  'size' => 20,
                  'weight' => 500,
                  'family' => 'Open Sans',
                ],
                'color' => '#000000',
              ],
            ],
          ],
        ],
      ];
    }
    
  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡¨πŸ‡¦Canada nikathone Ontario

    @mhentry please let us know if the example above helped you. In the meantime I will go ahead and close this issue.

  • πŸ‡¨πŸ‡¦Canada ciesinsg

    Hi there, I just wanted to reply to this issue as the original user never replied to confirm whether or not this works.

    I tried the hook alter with the raw options and this unfortunately did not work for me. What did work for me is:

    function my_module_chart_alter(array &$element, $chart_id) {
      $element['#title_font_size'] = 20;
      $element['#title_font_weight'] = 'bold'; // Adjust font weight as desired
      $element['#title_color'] = '#004B78';
    }
    

    I am not sure if this is the right way to do it, but it did work for me. I am also not sure what would need to change for the #raw_options method in order for it to work.

Production build 0.71.5 2024