ASP.NET 折线图 X 轴间隔关闭
ASP.NET Line Chart X-Axis Intervals Off
我在 ASP.NET 网页中使用 MSChart (System.Web.UI.DataVisualization.Charting.Chart()) 折线图。它工作正常,只是 X 轴间隔偏离 1,我无法弄清楚如何使它们与正确的数字对齐。这是:
注意 X 轴 (MPH) 数字:-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");
请特别注意 Interval = 10
和 IsStartedFromZero = true
。
设置坐标轴最小值:
chartArea.AxisX.Minimum = 0;
我在 ASP.NET 网页中使用 MSChart (System.Web.UI.DataVisualization.Charting.Chart()) 折线图。它工作正常,只是 X 轴间隔偏离 1,我无法弄清楚如何使它们与正确的数字对齐。这是:
注意 X 轴 (MPH) 数字:-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");
请特别注意 Interval = 10
和 IsStartedFromZero = true
。
设置坐标轴最小值:
chartArea.AxisX.Minimum = 0;