C# ASP.NET折线图X轴间隔关闭
我正在ASP.NET网页中使用MSChart(System.Web.UI.DataVisualization.Charting.Chart())折线图。它工作得很好,只是X轴间隔被关闭了1,我不知道如何使它们与正确的数字对齐。这是:C# ASP.NET折线图X轴间隔关闭,c#,asp.net,.net,charts,mschart,C#,Asp.net,.net,Charts,Mschart,我正在ASP.NET网页中使用MSChart(System.Web.UI.DataVisualization.Charting.Chart())折线图。它工作得很好,只是X轴间隔被关闭了1,我不知道如何使它们与正确的数字对齐。这是: private void BuildLineChart(string reportName, List < DataPoint > points, string xTitle, string yTitle) { var chart = new C
private void BuildLineChart(string reportName, List < DataPoint > points, string xTitle, string yTitle) {
var chart = new Chart();
// Build a column series
Series series = new Series(reportName);
series.ChartType = SeriesChartType.Line;
chart.Series.Add(series);
// Define the chart area
Grid grid = new Grid();
grid.LineWidth = 0;
ChartArea chartArea = new ChartArea();
chartArea.AxisX.MajorGrid = grid;
chartArea.AxisX.Crossing = 0;
chartArea.AxisX.Interval = 10;
chartArea.AxisX.IsStartedFromZero = true;
if (xTitle != string.Empty) {
chartArea.AxisX.Title = xTitle;
chartArea.AxisX.TitleAlignment = StringAlignment.Center;
chartArea.AxisX.TextOrientation = TextOrientation.Horizontal;
chartArea.AxisX.TitleFont = new Font("Verdana", 12);
}
if (yTitle != string.Empty) {
chartArea.AxisY.Title = yTitle;
chartArea.AxisY.TitleAlignment = StringAlignment.Center;
chartArea.AxisY.TextOrientation = TextOrientation.Rotated270;
chartArea.AxisY.TitleFont = new Font("Verdana", 12);
}
ChartArea3DStyle areaStyle = new ChartArea3DStyle(chartArea);
areaStyle.Rotation = 0;
chart.ChartAreas.Add(chartArea);
Axis xAxis = new Axis(chartArea, AxisName.X);
Axis yAxis = new Axis(chartArea, AxisName.Y);
// Set chart width and height (Note: increasing the width and height of the chart doesn't seem to improve the fidelity in the generated pdf (downstream))
chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
chart.Height = new System.Web.UI.WebControls.Unit(300, System.Web.UI.WebControls.UnitType.Pixel);
// Bind the data to the chart
foreach(DataPoint point in points) {
chart.Series[reportName].Points.Add(point);
}
chart.Series[reportName].BorderWidth = 2;
//chart.Series[reportName].IsValueShownAsLabel = true;
string filename = Server.MapPath("./ChartImages") + "/" + reportName + ".png";
chart.SaveImage(filename, ChartImageFormat.Png);
}
var points = new List<DataPoint>();
points.Add(new DataPoint(0, 0));
points.Add(new DataPoint(8, 25));
points.Add(new DataPoint(9, 15));
points.Add(new DataPoint(14, 25));
points.Add(new DataPoint(15, 15));
points.Add(new DataPoint(22, 26));
points.Add(new DataPoint(23, 16));
points.Add(new DataPoint(36, 26));
points.Add(new DataPoint(36, 17));
points.Add(new DataPoint(53, 26));
points.Add(new DataPoint(53, 19));
points.Add(new DataPoint(73, 26));
BuildLineChart("GearSplit", points, "MPH", "RPM X 100");
请注意X轴(英里/小时)编号:-1、9、19、29….
那些应该是0,10,20,30。我在代码中采取了一些措施,我认为应该按照我想要的方式来做,但没有任何效果。我的图表100%内置于c#,ASPX中没有任何内容。这是:
private void BuildLineChart(string reportName, List < DataPoint > points, string xTitle, string yTitle) {
var chart = new Chart();
// Build a column series
Series series = new Series(reportName);
series.ChartType = SeriesChartType.Line;
chart.Series.Add(series);
// Define the chart area
Grid grid = new Grid();
grid.LineWidth = 0;
ChartArea chartArea = new ChartArea();
chartArea.AxisX.MajorGrid = grid;
chartArea.AxisX.Crossing = 0;
chartArea.AxisX.Interval = 10;
chartArea.AxisX.IsStartedFromZero = true;
if (xTitle != string.Empty) {
chartArea.AxisX.Title = xTitle;
chartArea.AxisX.TitleAlignment = StringAlignment.Center;
chartArea.AxisX.TextOrientation = TextOrientation.Horizontal;
chartArea.AxisX.TitleFont = new Font("Verdana", 12);
}
if (yTitle != string.Empty) {
chartArea.AxisY.Title = yTitle;
chartArea.AxisY.TitleAlignment = StringAlignment.Center;
chartArea.AxisY.TextOrientation = TextOrientation.Rotated270;
chartArea.AxisY.TitleFont = new Font("Verdana", 12);
}
ChartArea3DStyle areaStyle = new ChartArea3DStyle(chartArea);
areaStyle.Rotation = 0;
chart.ChartAreas.Add(chartArea);
Axis xAxis = new Axis(chartArea, AxisName.X);
Axis yAxis = new Axis(chartArea, AxisName.Y);
// Set chart width and height (Note: increasing the width and height of the chart doesn't seem to improve the fidelity in the generated pdf (downstream))
chart.Width = new System.Web.UI.WebControls.Unit(800, System.Web.UI.WebControls.UnitType.Pixel);
chart.Height = new System.Web.UI.WebControls.Unit(300, System.Web.UI.WebControls.UnitType.Pixel);
// Bind the data to the chart
foreach(DataPoint point in points) {
chart.Series[reportName].Points.Add(point);
}
chart.Series[reportName].BorderWidth = 2;
//chart.Series[reportName].IsValueShownAsLabel = true;
string filename = Server.MapPath("./ChartImages") + "/" + reportName + ".png";
chart.SaveImage(filename, ChartImageFormat.Png);
}
var points = new List<DataPoint>();
points.Add(new DataPoint(0, 0));
points.Add(new DataPoint(8, 25));
points.Add(new DataPoint(9, 15));
points.Add(new DataPoint(14, 25));
points.Add(new DataPoint(15, 15));
points.Add(new DataPoint(22, 26));
points.Add(new DataPoint(23, 16));
points.Add(new DataPoint(36, 26));
points.Add(new DataPoint(36, 17));
points.Add(new DataPoint(53, 26));
points.Add(new DataPoint(53, 19));
points.Add(new DataPoint(73, 26));
BuildLineChart("GearSplit", points, "MPH", "RPM X 100");
private void BuildLineChart(string reportName,Listpoints,string xTitle,string yTitle){
var chart=新图表();
//建立一个专栏系列
系列=新系列(报告名称);
series.ChartType=serieChartType.Line;
图表.系列.添加(系列);
//定义图表区域
网格=新网格();
grid.LineWidth=0;
ChartArea ChartArea=新的ChartArea();
chartArea.AxisX.MajorGrid=网格;
chartArea.AxisX.Crossing=0;
chartArea.AxisX.Interval=10;
chartArea.AxisX.IsStartedFromZero=true;
if(xTitle!=string.Empty){
chartArea.axix.Title=xTitle;
chartArea.AxisX.TitleAlignment=StringAlignment.Center;
chartArea.AxisX.TextOrientation=TextOrientation.Horizontal;
chartArea.AxisX.TitleFont=新字体(“Verdana”,12);
}
if(yTitle!=string.Empty){
chartArea.AxisY.Title=ytile;
chartArea.AxisY.TitleAlignment=StringAlignment.Center;
chartArea.AxisY.TextOrientation=TextOrientation.Rotated270;
chartArea.AxisY.TitleFont=新字体(“Verdana”,12);
}
ChartArea3DStyle areaStyle=新的ChartArea3DStyle(chartArea);
areaStyle.旋转=0;
chart.ChartAreas.Add(chartArea);
Axis xAxis=新轴(chartArea,AxisName.X);
轴yAxis=新轴(图表区域,AxisName.Y);
//设置图表宽度和高度(注意:增加图表的宽度和高度似乎不会提高生成的pdf(下游)中的保真度)
chart.Width=new System.Web.UI.WebControls.Unit(800,System.Web.UI.WebControls.UnitType.Pixel);
chart.Height=newsystem.Web.UI.WebControls.Unit(300,System.Web.UI.WebControls.UnitType.Pixel);
//将数据绑定到图表
foreach(点对点数据点){
chart.Series[reportName].Points.Add(point);
}
chart.Series[reportName].BorderWidth=2;
//chart.Series[reportName].IsValueShownAsLabel=true;
字符串文件名=Server.MapPath(“./ChartImages”)+“/”+reportName+“.png”;
SaveImage(文件名,chartmageformat.Png);
}
var points=新列表();
添加(新数据点(0,0));
增加(新数据点(8,25));
增加(新数据点(9,15));
增加(新数据点(14,25));
增加(新数据点(15,15));
增加(新数据点(22,26));
增加(新数据点(23,16));
增加(新数据点(36,26));
增加(新数据点(36,17));
增加(新数据点(53,26));
增加(新数据点(53,19));
增加(新数据点(73,26));
构建线形图(“齿轮分离”,点,“英里/小时”,“转速X 100”);
请特别注意
间隔=10
和从零开始=真
设置轴最小值:
chartArea.AxisX.Minimum = 0;