none
Unable to load X axis value on Scatter chart using c# Microsoft.office.interop.excel RRS feed

  • Question

  •  

    I have X axis values in column A and Y axis values in remaining columns of excel sheet. It has multiple series thats the reason Y axis values are in multiple columns.

    My chart loads the Y axis values but for some reason it takes X axis values as some default values.

    Code below:

    oRng = oSheet.get_Range("B1","S1134");

    oChart.ChartWizard(oRng, Excel.XlChartType.xlXYScatterSmoothNoMarkers,Missing.value,Missing.value,true,true,true,Missing.value,"x axis",Missing.value,Missing.value);

    oSeries=(Excel.Series)oChart.SeriesCollection(1);

    oSeries.XValues= oSheet.get_Range("A1","A1134");

    oChart.Refresh();

    Please suggest me in loading X axis values which are in column A to the chart.

    Thanks.

    Tuesday, June 5, 2012 6:52 PM

Answers

  • Hi Codinggrockzz,

    I can't reproduce your issue. I created a Excel workbook project,and tried to reprooduce your issue via follwing snippet:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    
    namespace ExcelWorkbook1
    {
        public partial class ThisWorkbook
        {
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Excel.Workbook wb = Application.ActiveWorkbook;
                Excel.Worksheet ws = wb.ActiveSheet as Excel.Worksheet;
                Excel.Range oRng = ws.get_Range("B1", "D20"); ;
                Excel.Chart ct = ws.Shapes.AddChart().Chart;
                var missing = System.Type.Missing;
                ct.ChartWizard(oRng, Excel.XlChartType.xlXYScatterSmoothNoMarkers, missing, missing, missing, missing, missing, missing, "x axis", missing, missing);
                Excel.Series oSeries = (Excel.Series)ct.SeriesCollection(1);
                oSeries.XValues = ws.get_Range("A1", "A20");
                ct.Refresh();
            }
    
            private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisWorkbook_Startup);
                this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
            }
    
            #endregion
    
        }
    }

    It works fine on my side.

    Please provide more details for trouble shooting.

    T.X.

    Wednesday, June 6, 2012 5:00 AM

All replies

  • Hi Codinggrockzz,

    I can't reproduce your issue. I created a Excel workbook project,and tried to reprooduce your issue via follwing snippet:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    
    namespace ExcelWorkbook1
    {
        public partial class ThisWorkbook
        {
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Excel.Workbook wb = Application.ActiveWorkbook;
                Excel.Worksheet ws = wb.ActiveSheet as Excel.Worksheet;
                Excel.Range oRng = ws.get_Range("B1", "D20"); ;
                Excel.Chart ct = ws.Shapes.AddChart().Chart;
                var missing = System.Type.Missing;
                ct.ChartWizard(oRng, Excel.XlChartType.xlXYScatterSmoothNoMarkers, missing, missing, missing, missing, missing, missing, "x axis", missing, missing);
                Excel.Series oSeries = (Excel.Series)ct.SeriesCollection(1);
                oSeries.XValues = ws.get_Range("A1", "A20");
                ct.Refresh();
            }
    
            private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisWorkbook_Startup);
                this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
            }
    
            #endregion
    
        }
    }

    It works fine on my side.

    Please provide more details for trouble shooting.

    T.X.

    Wednesday, June 6, 2012 5:00 AM
  • I am having the same issue using your code you posted.  Try changing your A column values to different numbers.  I am not having luck using the code you posted to do this.  I get 1-20 on the x axis even though the numbers in my A column start at 20 and end at 360
    Sunday, November 13, 2016 10:52 PM