如何使用c#调整折线图的Y轴刻度

时间:2016-02-11 17:45:39

标签: c# mschart

我正在使用System.Web.UI.DataVisualization.Charting生成我在运行时填充的折线图。图表的Y轴是当前从-150%到+ 150%的百分比值。我想调整比例,以便Y轴标签始终从-100%缩放到+ 100%。任何帮助表示赞赏。谢谢。

这是我对图表的示例代码:

        Chart c = new Chart();
        c.AntiAliasing = AntiAliasingStyles.All;
        c.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
        c.Width = 800; 
        c.Height = 450;

        ChartArea ca = new ChartArea();
        ca.Position.X = 0;
        ca.Position.Y = 10;
        ca.Position.Width = 100;
        ca.Position.Height = 80;
        ca.BackColor = Color.FromArgb(248, 248, 248);
        ca.BackSecondaryColor = Color.FromArgb(255, 255, 255);
        ca.BackGradientStyle = GradientStyle.TopBottom;

        ca.AxisY.IsMarksNextToAxis = true;
        ca.AxisY.LineColor = Color.FromArgb(157, 157, 157);
        ca.AxisY.MajorTickMark.Enabled = true;
        ca.AxisY.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
        ca.AxisY.LabelStyle.ForeColor = Color.FromArgb(89, 89, 89);
        ca.AxisY.LabelStyle.Format = "{0:p}";
        ca.AxisY.LabelStyle.IsEndLabelVisible = true;
        ca.AxisY.Interval = 1;
        ca.AxisY.LabelStyle.Font = new Font("Calibri", 4, FontStyle.Regular);
        ca.AxisY.MajorGrid.LineColor = Color.FromArgb(234, 234, 234);          

        ca.AxisX.IsMarksNextToAxis = true;
        ca.AxisX.LineColor = Color.FromArgb(157, 157, 157);
        ca.AxisX.MajorTickMark.Enabled = true;
        ca.AxisX.MajorTickMark.LineColor = Color.FromArgb(157, 157, 157);
        ca.AxisX.LabelStyle.IsEndLabelVisible = true;            
        ca.AxisX.LabelStyle.ForeColor = Color.FromArgb(89, 89, 89);
        ca.AxisX.MajorGrid.LineWidth = 0;
        ca.AxisX.IsMarginVisible = false;
        ca.AxisX.Interval = 0;
        c.ChartAreas.Add(ca);

        List<RawData> rawData = (List<RawData>)rawDataDao.GetDataByDateRange(start.Value, end.Value);

    if(rawData.Count > 0)
    {
         Series s = new Series();
                 s.Font = new Font("Lucida Sans Unicode", 6f);
                 s.Color = System.Drawing.ColorTranslator.FromHtml(GetCombinedColorByIndex(possibleSensors.IndexOf(sensor))); 
                 s.BorderColor = Color.FromArgb(159, 27, 13);
                 s.BackSecondaryColor = Color.FromArgb(173, 32, 11);
                 s.BackGradientStyle = GradientStyle.LeftRight;
                 s.ChartType = SeriesChartType.Line;
                 s.XValueType = ChartValueType.DateTime;
                 s.Name = sensor.SensorName;

                 foreach (RawData d in workingRawData)
                 {
                     System.DateTime x = DateTime.Parse(d.Dt.Value.ToString("", CultureInfo.CreateSpecificCulture("en-US")));
                     DataPoint p = new DataPoint(x.ToOADate(), Convert.ToDouble(d.Reading));                       
                     p.AxisLabel = d.Dt.Value.ToString();
                     s.Points.Add(p);
                 }
                 c.Series.Add(s);
    }

1 个答案:

答案 0 :(得分:0)

如果轴是可缩放的,请将缩放视图设置为您需要的范围

ca.AxisY.ScaleView.Zoom(-100, 100);