I need to add a trendline to an Excel scatterplot. There's a class in the EPPlus library - ExcelChartTrendline.cs - that is clearly intended to support this. There is also an Enum - eTrendLine that allows me to specify the type of line.
The code for adding the scatterplot was simple and the chart renders fine:
var chart = worksheet.Drawings.AddChart(chartName, eChartType.XYScatter);
But after a many hours searching and tinkering I cannot figure out the syntax for adding the trendline.
You have to get reference to the serie that is created when adding the chart series to access the TrendLines collection. Like this:
var chart = worksheet.Drawings.AddChart("chart test", eChartType.XYScatter);
var series = chart.Series.Add(worksheet.Cells["B1:B10"], worksheet.Cells["A1:A10"]);
series.TrendLines.Add(eTrendLine.Linear);
Related
I want to add a chart inside a textbox using VBA in Microsoft word.
I have tried using
ActiveDocument.InlineShapes.AddChart()
API;
with cursor inside the textbox and still it doesn't produce the desired result
Unless you specify where the chart is to be placed it will simply be added to the document. If you had looked at the help text, IntelliSense or the Object Browser you would have seen that the method takes a Range as an argument. To place the chart in a specific location the range is required.
For example:
Set shp = ActiveDocument.InlineShapes.AddChart(Range:=Selection.Range)
I am trying to read the category names of a waterfall chart in a PowerPoint VSTO project.
So far, I was unable to do so.
Here is what I tried:
chart.SeriesCollection(x).Axes(y).CategoryNames - not available for this chart type
chart.SeriesCollection(x).XValues - not available for this chart type
chart.SeriesCollection(x).Points(y).DataLabel.Text / .Caption - this returns the point value, not the category name, e.g. -130
chart.SeriesCollection(x).DataLabels(y).Text / .Caption - same as previous: It returns the point values
Then, I tried reading the source data directly via chart.ChartData.Workbook but this is also not available.
So, how can I read the category names?
It appears that, as of this writing, the XlChartType enumeration is missing a member for Waterfall. (Waterfall has a ChartType integer value of 119, which is simply missing in the enumeration.)
As the missing enumeration creates all sorts of issues, I decided to write code that would convert the chart to an enumerated type, place the Category Names into an array, and then use PowerPoint's Undo functionality to restore the chart.
PowerPoint.Chart myChart = Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes[2].Chart;
myChart.ChartType = Office.XlChartType.xlBarStacked;
PowerPoint.Axis CategoryAxis = Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes[2].Chart.Axes(PowerPoint.XlAxisType.xlCategory, PowerPoint.XlAxisGroup.xlPrimary);
Array CatNames = (Array)((object)CategoryAxis.CategoryNames);
Globals.ThisAddIn.Application.CommandBars.ExecuteMso("Undo");
//Do something here with the CatNames array
I have a x,y scatter chart with lines connecting the dots from one source. I have between 1 and 8 lines and I need to have the MarkerStyle assigned to each line. Since the lines are not fixed and depend on the current data, I can't say which lines are there.
In general I could just assign
ActiveChart.FullSeriesCollection(i).MarkerStyle = xlMarkerStyleAutomatic
But that also assigns unwanted/unreadable Markers. So could I create a Collection with the Markerstyles I want and than assign that?
I tested
Dim colMarker As Collection
Set colMarker = New Collection
colMarker.Add "xlMarkerStyleCircle"
colMarker.Add "xlMarkerStyleSquare"
colMarker.Add "xlMarkerStyleTriangle"
With ActiveChart.FullSeriesCollection(i)
.MarkerStyle = colMarker(1)
End With
But the error msg is wrong type
What type do I need?
Thanks Kaz
You can set the marker style for each series individually:
Dim s as Series
s = ActiveChart.SeriesCollection.NewSeries
s.MarkerStyle = xlMarkerStyleCircle
Edit:
To assign different marker styles use the approach you suggested, but when adding styles to the collection it should be done like this:
colMarker.Add xlMarkerStyleDiamond
etc, ie without quotation marks.
I am trying to add data labels to all my series in a bar chart but the amount of series varies from chart to chart because this is a function used for many charts. So, I used a For Loop to add them, but I need to determine the size of the SeriesCollection array. Would there be a function that would do so? When I try to use .Size it gives me an error.
Below is what I tried:
With Chart
For i = 1 To .SeriesCollection.Size
.SeriesCollection(i).Points(i).HasDataLabel = True
Next i
End With
Some of the stuff in the Chart object model isn't properly hooked up to Intellisense. You'll notice that you get no suggestions when you type .SeriesCollection.
.SeriesCollection.Count will give you what you're after, I think.
I am using Microsoft.Office.Interop.Excel to create a chart. Some of my series in chart series collection are logarithmic, so I want to get hold of axis related a particular series, to set scale type to logarithmic.
Here is a way you could do it:
xlAxisValue = CType(xlChart.Axes(, Excel.XlAxisGroup.xlPrimary), Excel.Axes)
xlAxisValue.Item(Excel.XlAxisType.xlValue).ScaleType = Excel.XlScaleType.xlScaleLogarithmic
It is a generic example, if it is not working plese post your code.