Align X-axis labels using chart columns (ASP.Net chart management)

I am trying to create a chart using an ASP.Net Chart control that has specific numeric values ​​on the X axis and their frequency on the Y axis. Here is an example of what I want from the chart frame I am replacing:

enter image description here

In the above example, the X-axis labels line up with columns. But using the ASP.Net Chart control instead of labeling the columns that represent these specific values ​​(e.g. 1492, 2984), the control is marked at rounded intervals and does not align with the columns (e.g. 2000, 4000), as you can see below:

enter image description here

I found other similar publications that recommended setting ChartArea.AxisX.Interval to 1. I tried this, but then for some reason the X axis label disappears, as you can see below:

enter image description here

Here is the code I use to create and populate the chart (minus setting various color attributes):

 DataTable newDt = GetChartDataTable(); chart.DataSource = newDt; chart.Series.Add("Series1"); chart.Series["Series1"].YValueMembers = "Frequency"; chart.Series["Series1"].XValueMember = "RoundedValue"; chart.ChartAreas["ChartArea1"].AxisX.Title = "kbps"; chart.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Sans Serif", 10, FontStyle.Bold); chart.ChartAreas["ChartArea1"].AxisY.Title = "Frequency"; chart.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("Sans Serif", 10, FontStyle.Bold); chart.Titles["Title1"].Text = chartTitle; chart.Titles["Title1"].Font = new Font("Sans Serif", 10, FontStyle.Bold); chart.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column; chart.Series["Series1"]["ShowMarkerLines"] = "True"; chart.DataBind(); 
+6
source share
1 answer

You must set IsXValueIndex to true .

Like this:
chart.Series["Series1"].IsXValueIndexed = true;

Example:

 // Creating the series Series series1 = new Series("Series1"); // Setting the Chart Types series1.ChartType = SeriesChartType.Column; // Adding some points series1.Points.AddXY(1492, 12); series1.Points.AddXY(2984, 0); series1.Points.AddXY(4476, 1); series1.Points.AddXY(5968, 2); series1.Points.AddXY(7460, 2); series1.Points.AddXY(8952, 12); series1.Points.AddXY(10444, 4); series1.Points.AddXY(11936, 3); series1.Points.AddXY(13428, 3); series1.Points.AddXY(14920, 5); series1.Points.AddXY(16412, 1); Chart1.Series.Add(series1); Chart1.Width = 600; Chart1.Height = 600; // Series visual series1.YValueMembers = "Frequency"; series1.XValueMember = "RoundedValue"; series1.BorderWidth = 1; series1.ShadowOffset = 0; series1.Color = Drawing.Color.Red; series1.IsXValueIndexed = true; // Setting the X Axis Chart1.ChartAreas("ChartArea1").AxisX.IsMarginVisible = true; Chart1.ChartAreas("ChartArea1").AxisX.Interval = 1; Chart1.ChartAreas("ChartArea1").AxisX.Maximum = Double.NaN; Chart1.ChartAreas("ChartArea1").AxisX.Title = "kbps"; Chart1.ChartAreas("ChartArea1").AxisX.TitleFont = new Font("Sans Serif", 10, FontStyle.Bold); // Setting the Y Axis Chart1.ChartAreas("ChartArea1").AxisY.Interval = 2; Chart1.ChartAreas("ChartArea1").AxisY.Maximum = Double.NaN; Chart1.ChartAreas("ChartArea1").AxisY.Title = "Frequency"; Chart1.ChartAreas("ChartArea1").AxisY.TitleFont = new Font("Sans Serif", 10, FontStyle.Bold); 
+5
source

Source: https://habr.com/ru/post/957354/


All Articles