Articles in this section
Category / Section

How to Change the Color of Axis Elements in a Blazor Chart?

5 mins read

This article explains how to change the color of axis element in Blazor Chart Component.

Customizing axis label color using the OnAxisLabelRender event in Blazor Chart

Blazor Charts provides support to customize the appearance of the ChartAxis elements such as grid lines, tick lines, axis label and axis title. Chart axis label color can be customized by using OnAxisLabelRender event.

The following properties are available in the AxisLabelRenderEventArgs.

LabelStyle – Specifies the font information of the axis label.

Text – Specifies the text to be displayed in the axis label.

Value – Specifies the value of the axis label.

We can also customize the Width and Color of gridlines and ticklines of the chart axis using ChartAxisMajorGridLines, ChartAxisMinorGridLines, ChartAxisMajorTickLines and ChartAxisMinorTickLines property of the corresponding axis.

The following code example illustrates how to customize the color of axis elements.

Index.razor


@using Syncfusion.Blazor.Charts

<SfChart>

    <ChartEvents OnAxisLabelRender="AxisRender"></ChartEvents>

    <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category" MinorTicksPerInterval="2">
        <ChartAxisMajorGridLines Width="1" Color="blue"/>
        <ChartAxisMinorGridLines Width="0.5" Color="red"/>
        <ChartAxisMajorTickLines Width="1" Color="blue"/>
        <ChartAxisMinorTickLines Width="1" Color="red"/>
    </ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries DataSource="@MedalDetails" XName="X" YName="YValue" Type="ChartSeriesType.Column" />
    </ChartSeriesCollection>
    
</SfChart>

@code {

    public class ChartData
    {
        public string X { get; set; }
        public double YValue { get; set; }
    }

    public List<ChartData> MedalDetails = new List<ChartData>
    {
        new ChartData { X= "USA", YValue= 46 },
        new ChartData { X= "GBR", YValue= 27 },
        new ChartData { X= "CHN", YValue= 26 },
        new ChartData { X= "UK", YValue= 23 },
        new ChartData { X= "AUS", YValue= 16 },
        new ChartData { X= "IND", YValue= 36 },
        new ChartData { X= "DEN", YValue= 12 },
        new ChartData { X= "MEX", YValue= 20 },
    };

    public void AxisRender(AxisLabelRenderEventArgs args)
    {
        if(args.Axis.Name == "PrimaryXAxis")
        {
            args.LabelStyle.Color = "blue";
        }
    }

}

The following screenshot illustrates the output of the above code snippet.

Output:

axis_element_color.png

View the sample in GitHub

Conclusion

I hope you enjoyed learning how to change color of axis element in Blazor Chart Component.

You can refer to our Blazor Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied