none
Ways of Adding data to a VSTO Chart?? RRS feed

  • Question

  • Hi everyone,

    Im developing a Document-level Add-in for Excel 2010 using .NET 4.0 and C#.
    I want to add  Microsoft.Office.Tools.Excel.Chart dinamically and I know how to do it, but Im having troubles
    adding the data to the chart. I guess I have to use the method SetSourceData(Excel.Range r),

    ¿Is there another different way to add data to the chart?

    Becuase the source of the data will be a Collection (List or Queue or Array), and the method SetSourceData only has a Excel.Range argument.

    ¿what about DataBinding? ¿Could I bind the the collection to the chart in order to update data automatically in the chart?

    Thank you very much.

    coco.     

    Wednesday, March 14, 2012 10:14 AM

Answers

  • Hi Tom,

    sorry, I´ve been quite busy and I forgot about this post.

    Well, I solved my problem using Excel.Series and adding them to the chart. Using them, I can add both and array or a Excel.Range from a sheet. Here is a little example of the code Im using.

      VSTO.Worksheet vstws = Globals.Factory.GetVstoObject(Globals.ThisWorkbook.Sheets[7]);
                VSTO.Chart michart = this.Controls.AddChart(vstws.Range[ch1.dataRange], "michart");
               
                Excel.SeriesCollection s = michart.SeriesCollection() as Excel.SeriesCollection;
                michart.ChartType = Excel.XlChartType.xlLineMarkers;
    
                Excel.Series serie = s.NewSeries();
                serie.Values = ch1.dataRange;
                serie.Name = "serie1";
    
               serie = s.NewSeries();
               serie.Values = ch2.dataRange;
               serie.Name = "serie2";
    

    But there is still sth around it, in the MSDN doccumentation,

    http://msdn.microsoft.com/es-es/library/microsoft.office.interop.excel.seriescollection.add.aspx

    It is suggested to use the method Add from an Excel.SeriesCollecion, instead of using NewSerie(), but when I use it, apart from the serie I´m adding, many labels for nonexistent series are added to the chart.

    The code above works fine, and I think the Add method form the SeriesCollection class or doesnt work properly, or I dont know how to use it. But from now, I have everything undercontrol.

    Thanks for asking.

    • Marked as answer by Bruce Song Wednesday, April 4, 2012 6:36 AM
    Tuesday, March 20, 2012 2:53 PM

All replies

  • Hi coco,

    Thanks for posting in the MSDN Forum.

    It seems only way to add data to Chart which you mentioned. I think you can fill your data in to the Excel document and use it in the Chart.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 16, 2012 6:52 AM
    Moderator
  • Hi coco,

    Any update?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 6:10 AM
    Moderator
  • Hi Tom,

    sorry, I´ve been quite busy and I forgot about this post.

    Well, I solved my problem using Excel.Series and adding them to the chart. Using them, I can add both and array or a Excel.Range from a sheet. Here is a little example of the code Im using.

      VSTO.Worksheet vstws = Globals.Factory.GetVstoObject(Globals.ThisWorkbook.Sheets[7]);
                VSTO.Chart michart = this.Controls.AddChart(vstws.Range[ch1.dataRange], "michart");
               
                Excel.SeriesCollection s = michart.SeriesCollection() as Excel.SeriesCollection;
                michart.ChartType = Excel.XlChartType.xlLineMarkers;
    
                Excel.Series serie = s.NewSeries();
                serie.Values = ch1.dataRange;
                serie.Name = "serie1";
    
               serie = s.NewSeries();
               serie.Values = ch2.dataRange;
               serie.Name = "serie2";
    

    But there is still sth around it, in the MSDN doccumentation,

    http://msdn.microsoft.com/es-es/library/microsoft.office.interop.excel.seriescollection.add.aspx

    It is suggested to use the method Add from an Excel.SeriesCollecion, instead of using NewSerie(), but when I use it, apart from the serie I´m adding, many labels for nonexistent series are added to the chart.

    The code above works fine, and I think the Add method form the SeriesCollection class or doesnt work properly, or I dont know how to use it. But from now, I have everything undercontrol.

    Thanks for asking.

    • Marked as answer by Bruce Song Wednesday, April 4, 2012 6:36 AM
    Tuesday, March 20, 2012 2:53 PM