Microsoft Charting - 使Y轴始终可见

时间:2012-10-12 20:41:26

标签: charts data-visualization mschart

我正在用C#创建一个Microsoft Chart。它工作正常,但是当没有数据时,Y轴消失。我希望Y轴始终可见。我试图找到解决问题的方法,但我无法解决。

这是我的代码:

Chart chart = new Chart {
  AntiAliasing = AntiAliasingStyles.All,
  TextAntiAliasingQuality = TextAntiAliasingQuality.High,
  BackColor = Color.FromArgb(250, 250, 250),
  Height = size.Height,
  Width = size.Width
};

chart.Legends.Clear();

ChartArea area = new ChartArea {
  BackColor = Color.Transparent,
  BorderColor = Color.FromArgb(240, 240, 240),
  BorderWidth = 1,
  BorderDashStyle = ChartDashStyle.Solid,
  AxisX = new Axis {
    Enabled = AxisEnabled.True,
    IntervalAutoMode = IntervalAutoMode.VariableCount,
    IsLabelAutoFit = true,
    IsMarginVisible = true,
    LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) },
    LineColor = Color.FromArgb(220, 220, 220),
    MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid },
    MajorTickMark = new TickMark { LineColor = Color.FromArgb(220, 220, 220), Size = 4.0f },
  },
  AxisY = new Axis {
    Enabled = AxisEnabled.True,
    IntervalAutoMode = IntervalAutoMode.VariableCount,
    IsLabelAutoFit = true,
    IsMarginVisible = true,
    LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) },
    LineColor = Color.Transparent,
    MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid },
    MajorTickMark = new TickMark { LineColor = Color.FromArgb(240, 240, 240), Size = 2.0f }
  },
  Position = new ElementPosition { Height = 100, Width = 100, X = 0, Y = 0 }
};

chart.ChartAreas.Add(area);

area.AxisX.LabelStyle.Format = "H:mm";
area.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours;

Series series = new Series {
  CustomProperties = "PointWidth = 1",
  IsXValueIndexed = true,
  XValueType = (ChartValueType)Enum.Parse(typeof(ChartValueType), x.Data.GetType().GetGenericArguments()[0].Name)
};

series.BorderWidth = 2;
series.BorderColor = Color.FromArgb(84, 164, 232);
series.ChartType = SeriesChartType.Area;
series.Color = Color.FromArgb(222, 234, 244);

series.Points.DataBindXY(x.Data, s.Data);

chart.Series.Add(series);

1 个答案:

答案 0 :(得分:1)

据我所知,这是不可能的。

作为解决方法,您可以添加另一个带有单个数据点的虚拟“线”系列。对于单个数据点,不会绘制线 - 但将显示轴。

相关问题