Axis Labels

The only missing element of our basic chart structure are Axis Labels. These are labels you would like to display on each interval tick along the axis.

To display interval labels along the axes, we will use the axisLabels property on any Cartesian Chart.

AxisLabels

The property axisLabels requires a AxisLabelConfig for each individual axis. If the config is not provided, then interval labels won't be rendered for those axes.

...
BarChart(
  height: 400,
  chartStructureData: const CartesianChartStructureData(
    xUnitValue: 1,
    yUnitValue: 2,
    maxYValue: 10,
  ),
  chartStylingData: CartesianChartStylingData(
    axisStyle: CartesianAxisStyle(
      axisWidth: 3.0,
      axisColor: Colors.white,
      tickConfig: AxisTickConfig.forAllAxis(
        showTicks: true,
        tickLength: 10.0,
        tickColor: Colors.white,
      ),
    ),
    gridStyle: CartesianGridStyle(
      show: true,
      gridLineWidth: 1.0,
      gridLineColor: Colors.white,
    ),
  ),
  axisLabels: AxisLabels(
    left: AxisLabelConfig(
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(right: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
    bottom: AxisLabelConfig(
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(top: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
  ),
  data: BarSeries(
    barData: [
      SimpleBar(
        xValue: 1,
        yValue: const BarData(yValue: 2.3),
      ),
      SimpleBar(
        xValue: 2,
        yValue: const BarData(yValue: 7.2),
      ),
      SimpleBar(
        xValue: 3,
        yValue: const BarData(yValue: -4.8),
      ),
    ],
  ),
),

Let's dive deeper into the AxisLabelConfig that's been provided to left and bottom edges.

AxisLabelConfig

Each config must implement the mandatory builder method. This method provides the index of that iteration, and the value of the interval that has been processed with the Unit Values. Finally, it must return a widget that will be displayed as the label.

Because the label consumes a widget, you have the complete freedom to provide even images or icons if you choose.

Additionally, there are two more properties that can help you position your labels better.

If you wish to have your labels positioned in the center of the unit interval, you can set the value of centerLabels property to true.

...
BarChart(
  height: 400,
  chartStructureData: const CartesianChartStructureData(
    xUnitValue: 1,
    yUnitValue: 3,
  ),
  chartStylingData: CartesianChartStylingData(
    axisStyle: CartesianAxisStyle(
      axisWidth: 3.0,
      axisColor: Colors.white,
      tickConfig: AxisTickConfig.forAllAxis(
        showTicks: true,
        tickLength: 10.0,
        tickColor: Colors.white,
      ),
    ),
    gridStyle: CartesianGridStyle(
      show: true,
      gridLineWidth: 1.0,
      gridLineColor: Colors.white,
    ),
  ),
  axisLabels: AxisLabels(
    left: AxisLabelConfig(
      centerLabels: true,
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(right: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
    bottom: AxisLabelConfig(
      centerLabels: true,
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(top: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
  ),
  data: BarSeries(
    barData: [
      SimpleBar(
        xValue: 1,
        yValue: const BarData(yValue: 2.3),
      ),
      SimpleBar(
        xValue: 2,
        yValue: const BarData(yValue: 7.2),
      ),
      SimpleBar(
        xValue: 3,
        yValue: const BarData(yValue: 4.8),
      ),
    ],
  ),
),

And you can constrain the labels at the edges of each axis to fit within the axis range.

BarChart(
  height: 400,
  chartStructureData: const CartesianChartStructureData(
    xUnitValue: 1,
    yUnitValue: 3,
  ),
  chartStylingData: CartesianChartStylingData(
    axisStyle: CartesianAxisStyle(
      axisWidth: 3.0,
      axisColor: Colors.white,
      tickConfig: AxisTickConfig.forAllAxis(
        showTicks: true,
        tickLength: 10.0,
        tickColor: Colors.white,
      ),
    ),
    gridStyle: CartesianGridStyle(
      show: true,
      gridLineWidth: 1.0,
      gridLineColor: Colors.white,
    ),
  ),
  axisLabels: AxisLabels(
    left: AxisLabelConfig(
      centerLabels: true,
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(right: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
    bottom: AxisLabelConfig(
      centerLabels: true,
      builder: (index, value) => Padding(
        padding: const EdgeInsets.only(top: 8.0),
        child: Text(
          '$index',
          style: const TextStyle(color: Colors.white),
        ),
      ),
    ),
  ),
  data: BarSeries(
    barData: [
      SimpleBar(
        xValue: 1,
        yValue: const BarData(yValue: 2.3),
      ),
      SimpleBar(
        xValue: 2,
        yValue: const BarData(yValue: 7.2),
      ),
      SimpleBar(
        xValue: 3,
        yValue: const BarData(yValue: 4.8),
      ),
    ],
  ),
),

Class Properties

Name
Type
Required
Default Value

builder

Widget Function(int, double)

true

--

centerLabels

bool

false

false

constraintEdgeLabels

bool

false

false

This concludes our section to structure and customize the Cartesian Grid and Axes elements.

Last updated