none
COMException When Setting Axis MinumumScale Value RRS feed

  • Question

  • Hi,

    I'm trying to set the Excel.Axis properties on a chart in VSTO. Here is my software versions:

    Microsoft Visual Studio 2010
    Version 10.0.30319.1 RTMRel
    Microsoft .NET Framework
    Version 4.0.30319 RTMRel
    Hotfix for Microsoft Visual Studio 2010 Ultimate - ENU (KB2455033)   KB2455033

    Excel

    2007 12.0.6557.5000 SP2 MSO (12.0.6554.5001)

     

    Here's the code

    Excel.Axis x = chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
                   x.HasTitle = true;
                   x.AxisTitle.Text = "Time (Sec)";
                   x.HasMajorGridlines = true;
                   x.HasMinorGridlines = true;
                   x.MinimumScale = 200.0;

    Everything works except the last property set. I get a COMException.

    Any ideas?

    Thanks,

    Joseph

    Tuesday, July 19, 2011 8:47 PM

Answers

  • Hi Joseph,

     

    Sorry for the misunderstood.

    After some researches on this problem, I found that the exception is caused by that not every Category Axis of ChartTypes support MinimumScale property, such Line type, 3DColumn type and so on…

     

    The reason these chart types don’t support MinimumScale property is that value type of Category Axis is text by default. Text type has no Maximum or minimum property…

     

    You have two choices to reach your target:

    1.       Use other Chart type such as the four types in X Y (Scatter) tab (which is used in Bessie’s post)

    2.       Set the CategoryType property of Category axis to xlTimeScale instead of xlCategoryScale

    Check the code below:

     

                axis.CategoryType = Excel.XlCategoryType.xlTimeScale;

                axis.MinimumScale = 20;

     

    I hope this hleps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by joeslide Wednesday, July 20, 2011 1:30 PM
    Wednesday, July 20, 2011 1:09 PM
    Moderator

All replies

  • Hi Joseph,

     

    Thanks for your post.

     

    Axis.MinimumScale Property is only works for value axis.

     

    So you need to do a little bit of modifications:

         Excel.Axis x = chartPage.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 20, 2011 6:03 AM
    Moderator
  • Calvin,

    I appreciate your quick response. I'm part engineer so when referring to a chart we think of the horizontal axis as the x axis and the vertical axis as the y axis. My intent is to draw a line chart of some value verses time. I think by definition xlValue refers to the y axis and xlCategory refers x axis?? The link by VSTO_Beginner points to a post by Bessie Zhao on Tuesday, December 01, 2009 7:44 AM seems to bear that out. I pass a user defined object that contains all the properties my chart needs for drawing. So here is my complete function:

            private void CreateSingleChart(PropsForExcelChart pxL)
            {
                Excel.ChartObjects xlCharts = (Excel.ChartObjects)this.ChartObjects(missing);
                Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                myChart.Chart.HasTitle = true;
                myChart.Height = 1000;
                myChart.Width = 1000;
                Excel.Chart chartPage = myChart.Chart;
                chartPage.ChartType = Excel.XlChartType.xlLine;

                //chartPage.HasAxis[Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary] = true;
                //chartPage.HasAxis[Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary] = true;

                Excel.Axis yAxis = chartPage.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
                yAxis.HasTitle = true;
                yAxis.AxisTitle.Text = pxL.YAxisTitle;

                Excel.Axis xAxis = chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
                xAxis.HasTitle = true;
                xAxis.AxisTitle.Text = "Time (Sec)";
                xAxis.HasMajorGridlines = true;
                xAxis.HasMinorGridlines = true;
                //The next line throws a COMException
                xAxis.MinimumScale = 200.0;

                Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)chartPage.SeriesCollection(missing);
                Excel.Series series = seriesCollection.NewSeries();

                series.XValues = pxL.XAxisData;
                series.Values = pxL.YAxisData;
                chartPage.ChartTitle.Text = pxL.ChartName;
                chartPage.Location(Excel.XlChartLocation.xlLocationAsNewSheet, pxL.ChartName);
                
                //var sb = new System.Text.StringBuilder(String.Format("{0}{1}.png", _App.ImageDirectory, pxL.ChartName));
                //The next line also throws a COMException - I had it working at one point, but something changed...
                //chartPage.Export(sb.ToString(), "PNG", missing);

            }
    Wednesday, July 20, 2011 11:19 AM
  • Hi Joseph,

     

    Sorry for the misunderstood.

    After some researches on this problem, I found that the exception is caused by that not every Category Axis of ChartTypes support MinimumScale property, such Line type, 3DColumn type and so on…

     

    The reason these chart types don’t support MinimumScale property is that value type of Category Axis is text by default. Text type has no Maximum or minimum property…

     

    You have two choices to reach your target:

    1.       Use other Chart type such as the four types in X Y (Scatter) tab (which is used in Bessie’s post)

    2.       Set the CategoryType property of Category axis to xlTimeScale instead of xlCategoryScale

    Check the code below:

     

                axis.CategoryType = Excel.XlCategoryType.xlTimeScale;

                axis.MinimumScale = 20;

     

    I hope this hleps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by joeslide Wednesday, July 20, 2011 1:30 PM
    Wednesday, July 20, 2011 1:09 PM
    Moderator