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

本文关键字:折线图 何使用 调整 | 更新日期: 2023-09-27 18:34:11

我正在使用System.Web.UI.DataVisualization.Chartting来生成我在运行时填充的折线图。图形的 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);
    }

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

如果轴可缩放,请将比例视图设置为所需的范围

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