Results 1 to 2 of 2

Thread: VSTO/VS2008 Excel Chart issue

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    VSTO/VS2008 Excel Chart issue

    Automating the generation of a chart through Excel 2007 using VS 2008 and VSTO, and I ran into a "referencing" issue (don't know what else to call it). The chart is just graphing two series on seperate axes (temp on primary, precip on secondary). I am referencing the chart object's seriescollection, and adding two different series to it. However, the second series always seems to overwrite the first one, regardless of the parameters that are passed into the Series.Add method, so it only displays the last series added. I 'solved' this by setting the 'MySeriesColl' variable back to the same object after the first series is added and that fixed the problem (see the commented line of code below), but why is this necessary? Is this normal behavior? The variable was assigned to the same object at the beginning...
    Code:
    Dim MyChart As Excel.Chart = Globals.ThisWorkbook.Charts.Add(System.Type.Missing, Globals.ThisWorkbook.Sheets("Sheet1"), System.Type.Missing, System.Type.Missing)
            MyChart.ChartType = Excel.XlChartType.xlLine
            Dim TempRange As Excel.Range = Globals.Sheet1.Range("E4", "P4")
            Dim PrecipRange As Excel.Range = Globals.Sheet1.Range("E5", "P5")
            Dim MySeriesColl As Excel.SeriesCollection = MyChart.SeriesCollection
            Dim TempSeries As Excel.Series = MySeriesColl.Add(TempRange, Excel.XlRowCol.xlRows, False, False, False)
            TempSeries.Name = "Temperature"
            TempSeries.AxisGroup = Excel.XlAxisGroup.xlPrimary
            '*******
            MySeriesColl = MyChart.SeriesCollection 'required or else second series overwrites first one?
            '*******
            Dim PrecipSeries As Excel.Series = MySeriesColl.Add(PrecipRange, Excel.XlRowCol.xlRows, False, False, False)
            PrecipSeries.Name = "Precipitation"
            PrecipSeries.AxisGroup = Excel.XlAxisGroup.xlSecondary

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VSTO/VS2008 Excel Chart issue

    Code:
            Dim MyChart As Excel.Chart
            Dim TempRange As Excel.Range
            Dim PrecipRange As Excel.Range 
            Dim MySeriesColl As Excel.SeriesCollection 
            Dim TempSeries As Excel.Series 
            Dim PrecipSeries As Excel.Series 
    
            set MyChart = Globals.ThisWorkbook.Charts.Add(System.Type.Missing, Globals.ThisWorkbook.Sheets("Sheet1"), System.Type.Missing, System.Type.Missing)
    
            MyChart.ChartType = Excel.XlChartType.xlLine
    
            set TempRange = Globals.Sheet1.Range("E4", "P4")
            set PrecipRange = Globals.Sheet1.Range("E5", "P5")
            setMySeriesColl = MyChart.SeriesCollection
            set tempSeries = MySeriesColl.Add(TempRange, Excel.XlRowCol.xlRows, False, False, False)
            TempSeries.Name = "Temperature"
            TempSeries.AxisGroup = Excel.XlAxisGroup.xlPrimary
            '*******
    '        MySeriesColl = MyChart.SeriesCollection 
    'required or else second series overwrites first one?
             myserirescoll.refresh '? <-- or requery? perhaps the collection isn't refreshing?
            '*******
            set PrecipSeries = MySeriesColl.Add(PrecipRange, Excel.XlRowCol.xlRows, False, False, False)
            PrecipSeries.Name = "Precipitation"
            PrecipSeries.AxisGroup = Excel.XlAxisGroup.xlSecondary
    ** Code may be wrong but isn't this a better way of seting the variables? (your choice of course)
    Refresh collection is from memory and I haven't checked if that exists... but there is a vague memory of using it.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width