Chart Control Y2 axis labels visible only when area position is set to auto RRS feed

  • Question

  • Hi There,

    I have an interesting problem where I am using a 3 series [the number of series is not too relevant] line chart with two Y axes. Using the samples and forums it seems only able to accomplish this by layering chart areas. 1 for the regular line chart in which the first two series appear. 2 for the third series. 3 for the second Y axis and a ghost series which was copied from the third series.

    I can get the three series to layer happily and look very nice. But I can either get the Y2 axis to layer correctly OR have it with labels and not layer correctly. You read that correctly.

    If I use the Position and InnerPosition of the other two areas then the labels don't show up despite there being more than enough visual space. Conversely, if I use Auto positioning then the labels do show up but the axis appears down and to the right of the rest of the chart.

    I have been banging my head against the wall on this for a while now and I am sure it is simple but am just missing the key ingredient.

    Can anyone provide some advice please?


    Wednesday, July 11, 2012 7:32 PM

All replies

  • I realise I have not put a code sample up. Please see below the important bits. It will no doubt look remarkably like the code sample in MultipleYAxes:

    // Create new chart area for original series
    ChartArea areaSeries = _chart.ChartAreas.Add("SecondaryArea");
    areaSeries.BackColor = Color.Transparent;
    areaSeries.BorderColor = Color.Transparent;
    areaSeries.AxisX.MajorGrid.Enabled = false;
    areaSeries.AxisX.MajorTickMark.Enabled = false;
    areaSeries.AxisX.LabelStyle.Enabled = false;
    areaSeries.AxisY.MajorGrid.Enabled = false;
    areaSeries.AxisY.MajorTickMark.Enabled = false;
    areaSeries.AxisY.LabelStyle.Enabled = false;
    areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
    series.ChartArea = areaSeries.Name;
    // Create new chart area for axis
    ChartArea areaAxis = _chart.ChartAreas.Add("SecondaryAreaAxis");
    areaAxis.BackColor = Color.Transparent;
    areaAxis.BorderColor = Color.Transparent;
    // Create a copy of specified series
    Series seriesCopy = _chart.Series.Add("SecondaryAreaAxis_Copy");
    seriesCopy.ChartType = series.ChartType;
    foreach (DataPoint point in series.Points)
        seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
    // Hide copied series
    seriesCopy.IsVisibleInLegend = false;
    seriesCopy.Color = Color.Transparent;
    seriesCopy.BorderColor = Color.Transparent;
    seriesCopy.ChartArea = areaAxis.Name;
    // Disable grid lines & tickmarks
    areaAxis.AxisX.LineWidth = 0;
    areaAxis.AxisX.MajorGrid.Enabled = false;
    areaAxis.AxisX.MajorTickMark.Enabled = false;
    areaAxis.AxisX.LabelStyle.Enabled = false;
    areaAxis.AxisY.Enabled = AxisEnabled.False;
    areaAxis.AxisY2.Enabled = AxisEnabled.True;
    areaAxis.AxisY2.MajorGrid.Enabled = false;
    areaAxis.AxisY2.MajorTickMark.Enabled = false;
    areaAxis.AxisY2.LabelStyle.Enabled = true;
    areaAxis.AxisY2.LabelStyle.Format = "#";
    areaAxis.AxisY2.LabelStyle.Font = area.AxisY.LabelStyle.Font;

    Thursday, July 12, 2012 1:14 PM