I'm using System.Windows.Forms.DataVisualization.Charting
in my app and I want to add different label types on my X axis which is representing a timeline. For example, I want to use "HH:mm"
format for labels but when it's 00:00
I'd like to show "dd.MM"
format instead. I tried to add cutom labels but it has no effect at all.
var area = new ChartArea();
area.AxisX.LabelStyle.Format = "HH:mm";
area.AxisX.Interval = 1 / 24.0;
area.AxisX.CustomLabels.Add(1.0, DateTimeIntervalType.Days, "dd.MM");
Adding
CustomLabels
will help; however if you want them to show an individual format you will have to add them individually to eachDataPoint
!Doing so is not quite as simple as one could wish for; there are several overloads but none is really easy to use. The simplest way, imo, is to use one with a
FromPosition
and aToPosition
; these should then to be set in a way that they hit right between theDataPoints
; this way they will be centered nicely..Note that as the X-Values are really
DateTimes
, but as always in a chart, converted todoubles
we need to convert them back toDateTime
for correct formatting and also use their values to calculate the middle or rather half an interval..Here is an example: