How to implement datetime values in a WinForms Chart?
To implement DateTime values in a WinForms Charts, you must set the ChartValueType to DateTime and set the interval type to days, months, or years using the ChartDateTimeIntervalType class.
//Configure the chart series
ChartSeries chartSeries = new ChartSeries("Sales");
chartSeries.Type = ChartSeriesType.Line;
chartSeries.Style.DisplayText = true;
chartSeries.Style.TextOrientation = ChartTextOrientation.Up;
chartSeries.Style.Symbol.Shape = ChartSymbolShape.Circle;
chartSeries.Style.Symbol.Color = Color.Blue;
chartSeries.Style.Interior = new Syncfusion.Drawing.BrushInfo(Color.Blue);
//Assign the X and Y Axes
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.DateTime;
this.chartControl1.PrimaryXAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Shift;
this.chartControl1.PrimaryXAxis.IntervalType= ChartDateTimeIntervalType.Months;
this.chartControl1.PrimaryXAxis.DateTimeFormat = "MM/yyyy";
this.chartControl1.PrimaryYAxis.ValueType = ChartValueType.Double;
//Update the data points to the axis
foreach (var data in dataSource)
{
chartSeries.Points.Add(data.Date, data.YValue);
}
this.chartControl1.Series.Add(chartSeries);
'Configure the chart series
ChartSeries1.Type = ChartSeriesType.Line
ChartSeries1.Style.DisplayText = True
ChartSeries1.Style.TextOrientation = ChartTextOrientation.Up
ChartSeries1.Style.Symbol.Shape = ChartSymbolShape.Circle
ChartSeries1.Style.Symbol.Color = Color.Blue
ChartSeries1.Style.Interior = New Syncfusion.Drawing.BrushInfo(Color.Blue)
'Assign the X and Y axes
columnChart.PrimaryXAxis.ValueType = ChartValueType.DateTime
columnChart.PrimaryXAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Shift
columnChart.PrimaryXAxis.IntervalType = ChartDateTimeIntervalType.Months
columnChart.PrimaryXAxis.DateTimeFormat = "MM/yyyy"
columnChart.PrimaryYAxis.ValueType = ChartValueType.Double
'Update the data points to the series
For Each item In viewModel.PlantDetails
ChartSeries1.Points.Add(item.Date1, item.YValue)
Next
columnChart.Series.Add(ChartSeries1)
Conclusion
I hope you enjoyed learning about how to implement datetime values in a WinForms Chart.
You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples 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, Direct-Trac, or feedback portal. We are always happy to assist you!