Search code examples
datetimedevexpresscustomizationaxis-labelsxtrachart

How to display the label as two lines for DateTime on XtraChart?


I have a DevExpress XtraChart LineChart control, and its argument is DateTime. I use the AxisX.DateTimeOptions.Format = DateTimeFormat.General, it will display year, month, day, hour, minute, but the labels will overlap each other. I know in DevExpress 15.1 version, we have new property to resolve this. But unfortunately I am on version 10.1 and I cannot upgrade for now.

I want to display the label as two lines: the first line just displays yyyy-MM-dd, and the second line just displays h:mm tt, how could I achieve this? How should I set the FormatString? Or shall I use the AxisX.DateTimeOptions.Format = DateTimeFormat.Custom or something else?

Thanks!!


Solution

  • I figure this out: We can use

    LineChart_CustomDrawAxisLabel

        private void LineChart_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            AxisBase axis = e.Item.Axis;
            if (axis is AxisX)
            {
                string dateTimeString = e.Item.Text;
                var result = dateTimeString.Split(' ');
    
                e.Item.Text = result[0] + System.Environment.NewLine + result[1] + result[2];
            }
        }