BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
I have been working a scatter chart. It is nearly completed. However I need to sort out my xAxis and its labels.
The xAxis is a datatime. I need to show the date only on it . However, I get strange dates that are not even in the data. So obviously I have missed something. This is the image of the output as it sites today:
This is my code setup. The xAxis is Closed Date which is a DateTime type
When I change the xAxis name to "Date" I see this.
Here is my code (VB.net) . Somethings are commented out, other comments tell what I am trying to do.
Any guidance is appreciated, its a mess right now.
John W.
--------------------------------------------------
'Working Chart Code
Dim xAxisLableModel As ChartDataBindAxisLabelModel = Nothing
Dim modChart As ChartDataBindModel = Nothing
Dim dsChartData As New DataSet
'Add datatable to dataset or chart
dsChartData.Tables.Add(dt)
'*****Chart Setup******
'Count Rows in dataset for testing
Dim x As Integer = dsChartData.Tables("Table1").Rows.Count
'load data into model
modChart = New ChartDataBindModel(dsChartData, "Table1")
'Set x and y
modChart.XName = "CloseDate"
modChart.YNames = New String() {"Close Price"}
'Create Trend Line
Dim trend As New TrendlineCollection
Dim trndLne As New Trendline
Dim polyTrend As New Trendline
'TrendLine 1 create and add
trndLne.Type = TrendlineType.Linear
trndLne.Name = "Trend1"
trndLne.Visible = True
trndLne.Style = Drawing2D.DashStyle.Solid
trndLne.Width = 2.0F
trndLne.Color = Color.Red
trndLne.ForwardForecast = False
'Trend Line Poly 2 to create and add
polyTrend.Type = TrendlineType.Polynomial
polyTrend.PolynomialOrder = 3
polyTrend.Name = "Trend2"
polyTrend.Visible = True
polyTrend.Style = Drawing2D.DashStyle.Solid
polyTrend.Width = 2.0F
polyTrend.Color = Color.Blue
polyTrend.ForwardForecast = False
'Create series and chart type
Dim series As New ChartSeries("Import Data")
series.Type = ChartSeriesType.Scatter
series.Text = series.Name
series.SeriesModel = modChart
series.Style.TextColor = Color.Black
series.Style.Font.Bold = True
series.Trendlines.Add(trndLne)
series.Trendlines.Add(polyTrend)
' series.Style.DisplayText = True
ChartControl1.Series.Add(series)
ChartControl1.PrimaryXAxis.ValueType = ChartValueType.DateTime
ChartControl1.PrimaryXAxis.DateTimeFormat = "MMM dd"
ChartControl1.PrimaryXAxis.LabelRotate = True
ChartControl1.PrimaryXAxis.LabelRotateAngle = 30
ChartControl1.PrimaryYAxis.ValueType = ChartValueType.Double
ChartControl1.PrimaryYAxis.Format = "c"
'Dim i As UInteger = ChartControl1.Series.VisibleCount
'xAxisLableModel = New ChartDataBindAxisLabelModel(dsChartData, "Table1")
'xAxisLableModel.LabelName = "Date"
ChartControl1.PrimaryXAxis.RangePaddingType = ChartAxisRangePaddingType.Calculate
'ChartControl1.PrimaryXAxis.LabelsImpl = xAxisLableModel
I tried to edit the first post but when I change the xAxis.name to "Date" the column header in datatable I see this.
A real mess of things.
John W
Hello John Willems,
We have analyzed your query and would like to inform you that if you use ChartValueType as 'Category', all the axis labels will display on the PrimaryXAxis by setting the interval to 1. Based on your requirements, since you are using linear data, the axis label should display based on its range base. Therefore, we recommend displaying all the axis labels by setting custom labels.
Please refer to the following documentation, which will be helpful to you:
https://www.syncfusion.com/kb/12395/how-to-customize-the-axis-label-in-windows-forms-charts.
Please let us know if you need further assistance.
Regards,
Nanthini Mahalingam.
Thank you, that sorted the xAxis labeling.
My hopefully last question has to do with the trendline. When sort the data by date or cost it changes the chart/graph. The trendline will go up or down depending on the order. The x(date) and y(cost) remained paired but the graph changes. (See Images)
I need to correct this behavior. Is there a way to fix to this?
John W
We are pleased to learn that our solution meets your requirements. According to Trendline behaviors, trend lines should be drawn based on PrimaryXAxis values. Therefore, we recommend sorting the date values and displaying the trend lines.
Additionally, we suggest using Trendline forecasting if you wish to display previous or future predictions.
Please refer to the following document, which will be helpful to you:
https://help.syncfusion.com/windowsforms/chart/chart-series?cs-save-lang=1&cs-lang=vb#forecasting
Please let us know if you need further assistance.