将X轴标签与图表列对齐(ASP.Net图表控件) [英] Line up X axis labels with chart columns (ASP.Net Chart control)

查看:288
本文介绍了将X轴标签与图表列对齐(ASP.Net图表控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ASP.Net图表控件,在X轴上具有特定的数值和在Y轴上的频率计数制作图表。下面是一个我想要替换的图表框架的示例:





在上面的例子中,X轴标签与列对齐。但是,使用ASP.Net图表控件,而不是标记表示这些特定值(例如1492,2984)的列,控件是以舍入的间隔标记,并且不排列列(例如2000,4000),因为您可以请参阅下文:



p>

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





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

  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;



示例:

  //创建系列
系列series1 =新系列(Series1);

//设置图表类型
series1.ChartType = SeriesChartType.Column;

//添加一些点
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;

//系列视觉效果
series1.YValueMembers =Frequency;
series1.XValueMember =RoundedValue;
series1.BorderWidth = 1;
series1.ShadowOffset = 0;
series1.Color = Drawing.Color.Red;
series1.IsXValueIndexed = true;

//设置X轴
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);

//设置Y轴
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);


I'm trying to make a chart using the ASP.Net Chart control that has specific numerical values on the X axis and their frequency count on the Y axis. Here is an example of what I want from the charting framework I am replacing:

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

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

Here is the code I'm using 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();

解决方案

You should 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);

这篇关于将X轴标签与图表列对齐(ASP.Net图表控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆