Ticket T343862
Visible to All Users

ASPxSpreadsheet - The "System.InvalidOperationException" error is thrown when the SetSolidFill method is executed

created 9 years ago

Hello,

I'm struggling with adding a secondary axis to a chart.  The following code snippet produces the attached charting result "onlyPrimaryAxis.png":

C#
for (int i = 0; i < plotSeries.Count(); i++) { var ser = plotSeries.ElementAt(i); var chartSeries = chart.Series[i]; Color color = ColorTranslator.FromHtml(ser.ChartColor); chartSeries.Outline.SetSolidFill(color); chartSeries.Fill.SetSolidFill(color); chartSeries.SeriesName.SetValue(ser.Name); }

Changing the above to:

C#
for (int i = 0; i < plotSeries.Count(); i++) { var ser = plotSeries.ElementAt(i); var chartSeries = chart.Series[i]; Color color = ColorTranslator.FromHtml(ser.ChartColor); chartSeries.Outline.SetSolidFill(color); chartSeries.Fill.SetSolidFill(color); chartSeries.SeriesName.SetValue(ser.Name); if (ser.Axis == 2) chartSeries.AxisGroup = AxisGroup.Secondary; }

gives the attached chart "addSecondaryAxis.png".

If i instead change the code to be:

C#
for (int i = 0; i < plotSeries.Count(); i++) { var ser = plotSeries.ElementAt(i); var chartSeries = chart.Series[i]; Color color = ColorTranslator.FromHtml(ser.ChartColor); if (ser.Axis == 2) chartSeries.AxisGroup = AxisGroup.Secondary; chartSeries.Outline.SetSolidFill(color); chartSeries.Fill.SetSolidFill(color); chartSeries.SeriesName.SetValue(ser.Name); }

I now get a crash.  The DevExpress portion of the stack trace is:

C#
System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=Can't use delete object. Source=DevExpress.Spreadsheet.v15.1.Core StackTrace: at DevExpress.XtraSpreadsheet.API.Native.Implementation.NativeObjectBase.CheckValid() at DevExpress.XtraSpreadsheet.API.Native.Implementation.NativeSeries.CreateNativeShapeFormat() at DevExpress.XtraSpreadsheet.API.Native.Implementation.NativeSeries.get_Outline()

Please advise if there's something I'm doing wrong.

thank you.

EDIT:  I note I can't attach two files,  I'll try a followup comment to attach the 2nd one

Show previous comments (6)
DevExpress Support Team 9 years ago

    Hello Don,

    I have thoroughly researched all the details you provided and tried to reproduce the same behavior in a project using your code snippets, but my attempts were unsuccessful. Please see the attachment.
    You can find the "Change Color"  button. When you click it, one color of  the chart series will be changed.
    Would you please check the project and add the missing details, so I can test it on my side and see the problem? This will allow us to collect more information on why the issue occurs on your side and find a solution, or proceed with researching your scenario remotely.

    I am waiting for your response.

      Stason, Alessandro -

      I've attached a visual studio solution which should serve as a complete demonstration of *two distinct* bugs we've found:

      Bug #1: when using secondary axes, you must assign the primary axis before the secondary.  If you do not, the series name and color will not be respected.

      Bug #2 (by far more serious): you can not use a secondary axis if the ranges are built against a set of dates for the x-axis.  If you try to, the series placed on the secondary axis will not be rendered.

      running the code produces a set of 4 charts, which I'll place in the next comment so you can quickly see what I'm talking about.  All 4 charts use the same data, but differ as to how the secondary axis is handled and the use of dates for the x-axis.

      if you have any suggestions/work-arounds, etc.  they would be most appreciated.  This is a show-stopper for our client.

      Thank you

        The image the code generates - demonstrating the 2 bugs.

        Answers approved by DevExpress Support

        created 9 years ago (modified 9 years ago)

        Hello Don,

        Thank you for providing the project. I have reproduced the behavior you described. Let me comment on these cases sequentially.

        System.InvalidOperationException is thrown if a series is moved to a group of secondary axes, since there is no way to make all series on the chart secondary. An exception occurs when changing the AxisGroup value for a series that is the only primary series of a chart. This is designed behavior. Please refer to the Series.AxisGroup help topic for more information.

        >>when using secondary axes, you must assign the primary axis before the secondary.<<

        When the series' axes are changed (primary axes are moved to a secondary axes group), the order of the series in the Chart.Series collection is changed. To avoid the issue, you can customize series settings and then set primary and secondary axes. See the code below for clarification:

        C#
        private void assignAxes(Chart chart, List<PlotSeries> series, int seriesOnSecondary) { chart.PrimaryAxes[0].TickLabelPosition = AxisTickLabelPosition.Low; chart.Legend.Position = LegendPosition.Bottom; int count = series.Count; for (int i = 0; i < count; i++) { var ser = series.ElementAt(i); var chartSeries = chart.Series[i]; Color color = ser.ChartColor; chartSeries.Outline.SetSolidFill(color); chartSeries.Fill.SetSolidFill(color); chartSeries.SeriesName.SetValue(ser.Name); } if (seriesOnSecondary >= 0 && seriesOnSecondary < count) chart.Series[seriesOnSecondary].AxisGroup = AxisGroup.Secondary; }

        >>: you can not use a secondary axis if the ranges are built against a set of dates for the x-axis.  If you try to, the series placed on the secondary axis will not be rendered.<<

        It appears that the current behavior is incorrect. I have created a separate thread to research it:Charts - Series with secondary axes is not rendered if one of the axes is of the DateTime data type. Our developers will do their best to fix it. In the meantime, wrap the code to customize the chart into the Workbook.BeginUpdate-Workbook.EndUpdate statement to overcome the problem. I have modified your previous project correspondingly. You can find it in the attachment.

          Comments (2)

            Thank you so very much Yulia!

            DevExpress Support Team 9 years ago

              I'm happy to hear that my assistance was helpful to you. Thank you for informing me. I greatly appreciate it!

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.