2
votes

I have a couple of problems with a MSChart plotted in a Winform.

  1. I can't get axis Y2 values displayed on the right hand side of the chart, which should be the default - similar to this question

  2. the stripline won't plot, and

  3. is it possible to line up the zero values of axes Y1 and Y2?

    Thanks for any help.

        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

1

1 Answers

3
votes

You asked about the unwanted placement of the AxisY2 by setting its Crossing property to 0

Simply not setting it, thus leaving it at the default double.NaN for automatically placing it at the right will resolve the issue..:

Crossing = double.NaN

To make a stripline show, it needs to start within the visible range of its axis. Offsetting it by 10 is way overboard for your data.. Also making it black is probably not what you want, unless you only want a thin line, not a colored area..

The basic rules for StripLines are:

  • When Interval = 0 only one stripline is shown at IntervalOffset with a width/height of StripWidth
  • When Interval > 0 many striplines are shown, going over the whole axis; unless you have a semi-tranparent color you need to make sure that StripWidth < Interval or else there is no space between them!
  • All measures are in axis-values; the types can be set with StripWidthType and IntervalType; useful especially when using one of the DateTime units.

To make the 0 values of both y-axes align, you need to tweak the Minimum and/or Maximum values of one or both axes. This may be a bit tricky and you will probably need to look into your data and take full control of spacing and placing the axis labels..

enter image description here