gpt4 book ai didi

c# - Winform MsChart 副轴和带状线

转载 作者:行者123 更新时间:2023-11-30 23:16:22 24 4
gpt4 key购买 nike

我在 Winform 中绘制的 MSChart 有几个问题。

  1. 我无法在图表右侧显示轴 Y2 值,这应该是默认值 - 类似于此 question

  2. 带状线不会绘制,并且

  3. 是否可以将轴 Y1 和 Y2 的零值对齐?

    感谢您的帮助。

        ChartArea TestChartArea = new ChartArea();

    public void CreateChartArea()
    {
    TestChartArea.Name = "TestChartArea";
    TestChartArea.BackColor = Color.LightGreen;
    TestChartArea.Position = new ElementPosition { Height = 100, Width = 80, X = 2, Y = 5 };
    //TestChartArea.Position = new ElementPosition { Auto = true };
    TestChartArea.AxisY = new Axis
    {
    Enabled = AxisEnabled.True,
    IsLabelAutoFit = true,
    IsMarginVisible = true,
    LabelStyle = new LabelStyle { Format = "P2", ForeColor = Color.DarkBlue, Font = new Font("Arial", 10, FontStyle.Regular) },
    LineColor = Color.Black,
    MajorGrid = new Grid { LineColor = Color.White, LineDashStyle = ChartDashStyle.Solid },
    MajorTickMark = new TickMark { LineColor = Color.Black }
    };

    TestChartArea.AxisY2 = new Axis
    {
    Enabled = AxisEnabled.True,
    IsLabelAutoFit = true,
    IsMarginVisible = true,
    LabelStyle = new LabelStyle { Format = "P2", ForeColor = Color.DarkBlue, Font = new Font("Arial", 10, FontStyle.Regular) },
    LineColor = Color.Transparent,
    MajorGrid = new Grid { LineColor = Color.Yellow, LineDashStyle = ChartDashStyle.Solid },
    MajorTickMark = new TickMark { LineColor = Color.Blue }

    };

    TestChartArea.AxisX = new Axis
    {
    Enabled = AxisEnabled.True,
    Crossing = 0,
    LineWidth = 1,
    IsLabelAutoFit = true,
    IsMarginVisible = false,
    LabelStyle = new LabelStyle { Angle=-45,Format = "N0", ForeColor = Color.Black, Font = new Font("Arial", 8, FontStyle.Regular) },
    LineColor = Color.Black,
    MajorGrid = new Grid { LineColor = Color.White, LineDashStyle = ChartDashStyle.Solid },
    MajorTickMark = new TickMark { LineColor = Color.LightGray, Size = 4.0f },
    Name="Spot"

    };
    }

    public void PlotChart()
    {
    int[] Xseries = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    double[] AXJO = { 0.0025, 0.0015, -0.001, 0.002, 0.0045, -0.002, -0.003, 0.0001, -0.004, -0.0075 };
    double[] ES = { 0.0020, 0.0010, -0.0005, 0.003, 0.0025, -0.001, -0.0015, 0.0005, -0.0032, -0.006 };
    double[] Diff = new double[10];

    // pair return
    for (int i = 0; i < 10; i++)
    {
    Diff[i] = AXJO[i] - ES[i];
    }

    TestChart.BackColor = Color.LightGoldenrodYellow;
    TestChart.BackSecondaryColor = Color.LightBlue;

    if (TestChart.Series.IsUniqueName("AXJO"))
    {
    TestChart.Series.Add("AXJO");
    TestChart.Series["AXJO"].YAxisType = AxisType.Primary;
    TestChart.Series["AXJO"].Color = Color.Green;
    TestChart.Series["AXJO"].ChartType = SeriesChartType.Line;
    TestChart.Series["AXJO"].Points.DataBindXY(Xseries, AXJO);
    TestChart.Series["AXJO"].ChartArea = "TestChartArea";
    }

    if (TestChart.Series.IsUniqueName("ES"))
    {
    TestChart.Series.Add("ES");
    TestChart.Series["ES"].YAxisType = AxisType.Primary;
    TestChart.Series["ES"].Color = Color.Red;
    TestChart.Series["ES"].ChartType = SeriesChartType.Line;
    TestChart.Series["ES"].Points.DataBindXY(Xseries, ES);
    TestChart.Series["ES"].ChartArea = "TestChartArea";
    }

    if (TestChart.Series.IsUniqueName("Diff"))
    {
    TestChart.Series.Add("Diff");
    TestChart.Series["Diff"].YAxisType = AxisType.Secondary;
    TestChart.Series["Diff"].Color = Color.Blue;
    TestChart.Series["Diff"].ChartType = SeriesChartType.Line;
    TestChart.Series["Diff"].Points.DataBindXY(Xseries, Diff);
    TestChart.Series["Diff"].ChartArea = "TestChartArea";
    }
    }

    public void AddStripLine()
    {
    // add stripline at Diff=zero
    StripLine ZeroDiff = new StripLine();
    ZeroDiff.ForeColor = Color.Black;
    ZeroDiff.BackColor = Color.Black;
    ZeroDiff.StripWidth = 1;
    ZeroDiff.BorderWidth = 2;
    ZeroDiff.Interval = 0;
    ZeroDiff.IntervalOffset = 10;
    TestChart.ChartAreas["TestChartArea"].AxisY2.StripLines.Add(ZeroDiff);
    }

    private void button1_Click(object sender, EventArgs e)
    {
    PlotChart();
    AddStripLine();
    }
    }

enter image description here

最佳答案

您询问了 AxisY2 的不想要的位置通过设置其 Crossing属性(property)0

只是不设置它,因此保留默认值 double.NaN自动将其放在右侧将解决问题..:

Crossing = double.NaN

要使带状线显示,它需要在其轴的可见范围内开始。将它偏移 10 对你的数据来说太过分了。将它变成黑色可能不是你想要的,除非你只想要一条细线,而不是彩色区域。

StripLines的基本规则是:

  • 何时Interval = 0IntervalOffset 处仅显示一条 带状线宽度/高度为 StripWidth
  • 何时Interval > 0显示了许多 带状线,遍及整个轴;除非你有半透明的颜色,否则你需要确保 StripWidth < Interval否则它们之间就没有空间了!
  • 所有度量均以轴值表示;可以使用 StripWidthType 设置类型和 IntervalType ;在使用 DateTime 之一时尤其有用单位。

要使两个 y 轴的 0 值对齐,您需要调整 Minimum和/或 Maximum一个或两个轴的值。这可能有点棘手,您可能需要查看数据并完全控制间距和放置轴标签。

enter image description here

关于c# - Winform MsChart 副轴和带状线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42049169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com