Is it possible to create a plot from List(Of Decimal) values in Excel with VB? I would like to avoid dumping data directly into a table, then creating the chart from that. Here is what I have so far, but I am getting a data type mismatch when I try to assign .XValues and .Values to the list. Is there some sort of conversion that needs to happen to turn a list into a series?
Dim Series As Excel.Series
Dim xVals As New List(Of Decimal)(New Decimal() {1, 3, 4, 5})
Dim yVals As New List(Of Decimal)(New Decimal() {2, 2, 2, 2})
xlWorkSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)
xlWorkSheet.Activate()
xlWorkSheet.Name = "My Sheet"
xlCharts = xlWorkSheet.ChartObjects
xlChartObj = xlCharts.Add(150, 30, 400, 250)
xlChart = xlChartObj.Chart
With xlChart
.ChartType = Excel.XlChartType.xlXYScatterLines
.HasLegend = True
.Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight
.HasTitle = True
.ChartTitle.Text = "Test Chart"
For i = 0 To channels.Count - 1
.SeriesCollection.NewSeries()
Series = .SeriesCollection(i + 1)
Series.XValues = xVals
Series.Values = yvals
Next
.Location(Excel.XlChartLocation.xlLocationAsNewSheet)
End With
UPDATE:
I seems like the following code will work, but it is not very ideal. Is there a better way to do this other than converting the array to a string representation of the array like I did?
Dim Series As Excel.Series
Dim xVals As New List(Of Decimal)(New Decimal() {1, 3, 4, 5})
Dim yVals As New List(Of Decimal)(New Decimal() {2, 2, 2, 2})
Dim xString As String
Dim yString As String
xlWorkSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)
xlWorkSheet.Activate()
xlWorkSheet.Name = "My Sheet"
xlCharts = xlWorkSheet.ChartObjects
xlChartObj = xlCharts.Add(150, 30, 400, 250)
xlChart = xlChartObj.Chart
With xlChart
.ChartType = Excel.XlChartType.xlXYScatterLines
.HasLegend = True
.Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight
.HasTitle = True
.ChartTitle.Text = "Test Chart"
For i = 0 To channels.Count - 1
.SeriesCollection.NewSeries()
Series = .SeriesCollection(i + 1)
xString = "={"
xString += xVals(0).ToString
For k = 1 To xVals.Count - 1
xString += ","
xString += xVals(k).ToString
Next
xString = "}"
yString = "={"
yString += yVals(0).ToString
For k = 1 To yVals.Count - 1
yString += ","
yString += yVals(k).ToString
Next
yString = "}"
Series.XValues = xString
Series.Values = yString
Next
.Location(Excel.XlChartLocation.xlLocationAsNewSheet)
End With
I was able to get what I wanted with the following code using Ranges stored as objects. The string method I mentioned in my second block of code had a limit of 8000+ characters (not sure of actual number, 8192?).