reactjsantd

How do we change the font color of ant design charts X and Y Axis Labels


I have spent way to much time trying to find out how to do this, I have tried the only example ive found to no avail

  yAxis: {
    label: {
      style: {
        fill: 'blue', // Change the color of y-axis labels to blue
      },
    },
  },

Solution

  • You can access an object with the key axis inside your config object. This object has many properties that can be applied to it. It's structured the following way:

    const config = {
      axis: {
        x: {},
        y: {},
      },
    }
    

    The property you are looking for is called labelFill. You can view all the available properties here.


    The following example creates a red font color for your X-axis and a blue color for your Y-axis:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Line } from '@ant-design/plots';
    
    const DemoLine = () => {
      const config = {
        data: {
          type: 'fetch',
          value: 'https://assets.antv.antgroup.com/g2/indices.json',
        },
        xField: (d) => new Date(d.Date),
        yField: 'Close',
        colorField: 'Symbol',
        normalize: { basis: 'first', groupBy: 'color' },
        axis: {
          x: { title: 'X Title', labelFill: 'red' },
          y: { title: 'Y Title', labelFill: 'blue' },
        },
      };
      return <Line {...config} />;
    };
    
    ReactDOM.render(<DemoLine />, document.getElementById('container'));
    

    Graph Image

    Code-Sandbox


    You can view more examples here.