Skip to content

DevExpress-Examples/winforms-dashboards-how-to-use-a-custom-format-for-axis-labels-in-the-chart-item

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dashboard for WinForms - How to Use a Custom Format for Axis Labels in the Chart Item

UPDATE: Starting with version 18.2, we introduced the capability to format any Numeric or Date-Time Value. So, you can set formats demonstrated in this example via the Dashboard Designer's UI using the following settings without writing additional code:
Numeric Format X-Axis Settings
Numeric Format Y-Axis Settings
However, you can still use approaches demonstrated in this example if you wish to implement your custom formatting not supported by the control.

Although our Dashboards do not provide a way to use custom formats and specify formats for axis labels in certain case, there is a way to format axis labels manually. This example illustrates how to format axis labels in a custom manner. For this access the underlying Chart Control in the DashboardItemControlCreated event handler and handle the CustomDrawAxisLabel event to modify the axis labels' content directly.

Note that printed or exported documents containing a dashboard/dashboard item do not reflect appearance settings applied using the events for accessing of underlying controls.

Files to Review

Example Overview

The following three most frequently asked scenarios are represented in this example:

1. Add the currency sign.

screenshot

private void dashboardDesigner1_DashboardItemControlCreated(object sender, DevExpress.DashboardWin.DashboardItemControlEventArgs e) {
    ...
    e.ChartControl.CustomDrawAxisLabel += ChartControl_CustomDrawAxisLabel1;
    ...
}
 private void ChartControl_CustomDrawAxisLabel1(object sender, DevExpress.XtraCharts.CustomDrawAxisLabelEventArgs e) {
    if (e.Item.Axis is SecondaryAxisY)
        e.Item.Text = "$" + e.Item.Text;
}

2. Use the ones unit format

screenshot

private void dashboardDesigner1_DashboardItemControlCreated(object sender, DevExpress.DashboardWin.DashboardItemControlEventArgs e) {
    ...
    e.ChartControl.CustomDrawAxisLabel += ChartControl_CustomDrawAxisLabel2;
    ...
}
 private void ChartControl_CustomDrawAxisLabel2(object sender, DevExpress.XtraCharts.CustomDrawAxisLabelEventArgs e) {
    if (e.Item.Axis is SecondaryAxisY)
        e.Item.Text = ((double)e.Item.AxisValue).ToString("n0");
}

3. Remove the decimal part of numbers.

screenshot

private void dashboardDesigner1_DashboardItemControlCreated(object sender, DevExpress.DashboardWin.DashboardItemControlEventArgs e) {
    ...
    e.ChartControl.CustomDrawAxisLabel += ChartControl_CustomDrawAxisLabel3;
    ...
}
private void ChartControl_CustomDrawAxisLabel3(object sender, DevExpress.XtraCharts.CustomDrawAxisLabelEventArgs e) {
    if (e.Item.Axis is AxisX)
        e.Item.Text = ((double)e.Item.AxisValue).ToString("n0");
}

Documentation