gpt4 book ai didi

c# - 将 X 轴标签与图表列对齐(ASP.Net 图表控件)

转载 作者:太空狗 更新时间:2023-10-29 21:39:41 24 4
gpt4 key购买 nike

我正在尝试使用 ASP.Net Chart 控件制作图表,该控件在 X 轴上具有特定数值,在 Y 轴上具有特定的频率计数。这是我要替换的图表框架的示例:

enter image description here

在上面的示例中,X 轴标签与列对齐。但是,使用 ASP.Net Chart 控件,而不是标记代表这些特定值的列(例如 1492、2984),控件以圆形间隔标记而不是像您可以的那样与列(例如 2000、4000)对齐见下文:

enter image description here

我发现其他类似的帖子建议将 ChartArea.AxisX.Interval 设置为 1。我试过了,但是 X 轴标签由于某种原因消失了,如下所示:

enter image description here

这是我用来创建和填充图表的代码(减去设置各种颜色属性):

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();

最佳答案

您应该将 IsXValueIndex 设置为 true

像这样:
chart.Series["Series1"].IsXValueIndexed = true;

示例:

// 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);

关于c# - 将 X 轴标签与图表列对齐(ASP.Net 图表控件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19771768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com