ASP.NET折线图X轴间隔关闭

时间:2017-01-31 21:54:34

标签: c# asp.net .net charts mschart

我在ASP.NET网页中使用了一个MSChart(System.Web.UI.DataVisualization.Charting.Chart())折线图。它的工作正常,只是X轴间隔偏离1,我无法弄清楚如何使它们与正确的数字对齐。这是:

enter image description here

注意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 = 10IsStartedFromZero = true

1 个答案:

答案 0 :(得分:1)

设置轴最小值:

chef-solo

enter image description here