Search code examples
c#excelchartsmschart

C# Chart axisY2 adjust (how to make it like Excel )


I've use chart to draw 2 of data value use axisY & axisY2, But C#'s chart didn't match what I want. I want it like Excel draws.

Excel chart: enter image description here

C# chart: enter image description here

I don't know how to do it , let series axisY & axisY2 auto draw line like Excel .
I think problem is how to adjust axisY2 let it match axisY. Does anyone have experience to do it?

chart1.Series.Clear();
        List<int> val_1 = new List<int>() {100,110,113,122,132,120,111,132,125,114,117,130 };
        List<int> val_2 = new List<int>() { 63, 70, 75, 79, 83, 74, 68, 79, 78, 72, 73, 80 };

        var se = new Series("val_1");

        se.ChartType = SeriesChartType.Line;
        se.MarkerSize = 5;
        for (int i = 0; i < val_1.Count;i++ )
        {
            se.Points.AddXY(i + 1, val_1[i]);
        }
        se.YAxisType = AxisType.Primary;
        chart1.Series.Add(se);

        se = new Series("val_2");
        se.ChartType = SeriesChartType.Line;
        se.MarkerSize = 5;
        for (int i = 0; i < val_2.Count; i++)
        {
            se.Points.AddXY(i + 1, val_2[i]);
        }

        se.YAxisType = AxisType.Secondary;

        chart1.Series.Add(se);

Solution

  • For your secondary Y axis, set the same minimum and maximum values as the ones set in the excel chart, like below:

            chart1.ChartAreas[0].AxisY2.Minimum = 0;
            chart1.ChartAreas[0].AxisY2.Maximum = 90;
    

    enter image description here