As shown in the image below, I have a chart (on the left) that I created manually. And I have the chart on the right which I created with the following VB Script:
Sub StackedBarChart()
'
' StackedBarChart Macro
'
'
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.PlotBy = xlColumns
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection("Total").Format.Fill.Visible = msoFalse
End Sub
Where I am falling short with my macro is the following areas:
I need to set just the "Total" data label to InsideBase
I need to rescale the y-axis. But, this needs to work for any data set. So, for example, taking the highest total value and adding $2.5 to it in order to make it a decent looking chart.
Automatically make sure that all of the Data Labels on the Legend appear. Right now, only 4-12 appear.
Thanks for your help!
With the
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
you select the whole line including the totals. So on top of your stacked chart you have the total values again, so this doubles the high.
With (and here I am not quite sure)
ActiveChart.SeriesCollection("Total").Format.Fill.Visible = msoFalse
you just supress the display of the 'Total' stack. Just try to comment this line out, and I supose that you will get another element on top.
The best you probably can do is to redo the chart manually while recording a macro and checking the statements there.
For the legend you possibly need to increase the size of the display area.
The totals you can possibly dray an invisible stack with a 100% overly in the background. Then the values should be shown but no bar is displayed.
Related
I am looking for a way to reinstate the default/native resizing behaviour of a chart in Excel 2010 once it has been disabled (e.g. by manipulating the chart with VBA).
Now I haven't been able to find anything anywhere about the behaviour I have in mind, so I am going to assume that it needs detailed explanation.
Input and select random numerical data into 4-5 cells in Excel, and insert a new Clustered Columns chart. You need to see a the chart's plot area. Now select the chart, and get the PlotArea.Top value with the following line
ActiveChart.PlotArea.Top
If you haven't touched the chart, this should return a value of 7. Now use one of the chart's handlebars to resize the chart vertically, and use the same command line again.
activechart.plotarea.top
Notice how the value returned is still 7. Now set this property to 7 in VBA.
ActiveChart.PlotArea.Top = 7
Again, grab one of the handlebars, resize the chart vertically and get the .top property again using:
ActiveChart.PlotArea.Top
Notice how the value has now changed. It will be either smaller or greater than 7 depending on whetehr you decreased or increased the size of the chart.
Once any element of a chart has been moved either manually or with VBA code, it loses this "absolute position" property and begins moving inside the ChartArea whenever the chart is resized. While some elements can be reset using .SetElement, this does not work for the Plot Area. For example, the following command lines do not reinstate the behaviour I am describing.
ActiveChart.SetElement msoElementPlotAreaNone
ActiveChart.SetElement msoElementPlotAreaShow
I do a lot of automated resizing of charts with VBA, and having the plot area move around by itself makes it a lot harder to predict the effect of resizing the chart and leads to inconstant results.
So back to the question: does anyone know of a way to reinstate this default behaviour, either for the entire chart, or at least specifically for the PlotArea?
Thanks in advance to anyone who may help!
Vincent
I ran into this when I manually resized the plot area and then when the legend is moved it did not resize the plot area at all.
I had tried to save my chart as a template (right click save as template in Excel 2013) but this still had the plot area manually set.
Therefore I would recommend keeping the auto-size behavior before saving a template, since the only way I know to re-set the chart auto-sizing behavior after it has been manually modified is to use a macro
Here is the macro I used to reinstate the auto-sizing behavior
Sub Macro1()
'
' this selects the chart based on the chart name
ActiveSheet.ChartObjects("Chart 4").Activate
' this selects the plot area
ActiveChart.PlotArea.Select
' this clears any custom formatting such as borders or fill colors
ActiveChart.PlotArea.ClearFormats
' this resets the auto-sizing behavior after plot area manually re-sized
ActiveChart.PlotArea.Position = xlChartElementPositionAutomatic
End Sub
References
Why plot area does not expand after clearing series legend?
Excel Chart Plot Area Auto Size - ExcelBanter
Is there a way to use the xlfreefloating function but only to position a graph in one direction but not the other? I have a macro that positions my graph between a certain range of cells. I want it to stay freefloating for the y direction but not the x-direction. So if the graph is to the right of some data cells, and I adjust the cells and make them longer in the x direction, I want the graph to follow. But if I adjust them in the y direction, I want them to stay the same. Thanks!
I found out how to do this myself:
One way to do this is to manually activate the chart AFTER the cells are manipulate, and then position it in terms of left or right wherever you want it. The following code was used after my charts were created, positioned, and cells were manipulated:
Sub MoveCharts()
ActiveSheet.ChartObjects("Chart 7").Activate
With ActiveChart.Parent
.Left = Range("N2").Left
End With
End Sub
I'll try to be very precise with my problem but if you have any questions, please ask!
So basicelly I have a column A full of dates (from line 1 to line 80) and a column B full of values (stock prices, and also with the same range).
I have created a chart with this code :
Sub Chart()
Range(Cells(1, 1), Cells(80, 1))Select
Range(Selection, Selection.Offset(0, 1)).Select
Set rSource=selection
ActiveChart.SetSourceData Source:=rSource, PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
end sub
So this is not a chart object but a new chart on a new sheet (I say that because all the things I've read on this subject is dealing with chart objects and not with chart alone in a sheet).
Now what I need is to draw an horizontal line between two dates (a support line).
For example, I need to draw a line between 01/01/2013 (line 1) and 06/25/2013 (line 80).
The line has to stay at the same level on the y-axis (that's what we call an horizontal line :) ) at the level of the stock price of 01/01/2013.
I've tried that :
Sub supportline()
Dim CoordA As Variant
CoordA = [a1:b80]
ActiveChart.Shapes.AddLine(CoordA(1, 1), CoordA(1, 2), CoordA(80, 1), CoordA(80, 2)).Select
end sub
But the line isn't drawn where I want. I think there's a problem of scale but I can't figure out how to have the accurate coordinates.
Thank you very much for your time and your help
EDIT (06/11/2013)
I've simplified the data base to illustrate my problem : http://cjoint.com/?CFksHluf0VD I'm trying to make a line between the two dates (01/01/2013 and 16/01/2013)
If you download this file, you'll see in the VBA part : 1/ Test1 : I've tried to make a line to link 2 points of the chart : if you launch the sub you'll see that the line is drawn but at the wrong scale and wrong place 2/ Test 2: I've tried to make an horizontal line between two points but that doesn't work at all
Test 2 is what I need to do for my original project (an horizontal line which goes from one date to another with the same y-value)
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
//add line style etc
End with
Then use your select statement to choose which points you want joined.
If you look at this link, I'll think you'll find it most useful
VBA-Controlled Conditional Formatting of Line Chart Lines
http://peltiertech.com/WordPress/conditional-formatting-of-lines-in-an-excel-line-chart-using-vba/
Another question on Stack Overflow
Excel VBA - How do you set line style for chart series?
In response to your comments, please see the answer to this question. It offers some good explanation of using charts (in this case vb.net)
Chart: Show more value descriptions on X-Axis
And a link to MSDN re this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart.aspx
I'm trying to automate a process that so far I have been doing manually in Excel 2010. I create Pivot Charts often. One of the series on these charts is displayed as bars. I change the fill color of each bar based on one of the row labels of the pivot chart. For instance, if the row label = "GEO", I change the fill color of the bar to green.
I'm sure that it's possible to automate this process through VBA. Here's my code so far. When I run this macro, it stops at the first line of the If statement and gives this error. Compile error: Expected array. Can anyone give me some advice as to how to make this code work?
Sub By_Rig_PC_Coloring()
For i = 1 To ActiveChart.SeriesCollection(2).Points.Count
ActiveChart.SeriesCollection(2).Points(i).Select
If xlRowField("MFR") = "GEO" Then
Selection.Format.Fill.Forcolor.RGB = RGB(0, 176, 80)
End If
Next i
End Sub
As far as I know, you cannot access X axis labels associated with current Point. But this is a PIVOT chart, so you can use your pivot table to get the info you need.
Points are DataField's PivotItems, Series are ColumnFields and X labels are RowFields.
So SeriesCollection(2).Points(10) will be pvtYourPivotTable.ColumnFields(2).DataRange.Cells(10) (assuming that you have only one DataField, otherwise DataRange will be multi-column, and you'll have to adjust for that).
So once you have a cell in pivot table assosacietd with Point, the label will be located at Intersect(pvtYourPivotTable.RowFields("MFR").DataRange, pvtYourPivotTable.ColumnFields(2).DataRange.Cells(10).EntireRow. You can also use Offset or other method.
Here is another method of colouring chart bars based on X labels: Peltier Tech Blog without VBA.
Hope this helps.
So I have a graph that has 6 series on it and I want to be able to make each series visible or invisible using a set of ActiveX check boxes. The check boxes seem to be working fine so far, but I can't figure out how to make an individual series invisible. Is there a property of the series that I can use to make it not visible? Google and Microsoft help aren't turning it up for me.
(Granted, that doesn't mean it isn't there...)
Any and all help is appreciated.
Update
Wait, I remember that I have come across this before in the past. A good way to hide the data you want is to hide the actual rows / columns of data being charted. So your ActiveX control won't touch the chart, but rather hide / unhide the columns / rows of chart data.
You will just need to set up your chart data in such a way that it doesn't interfere with any other data that needs to be shown.
Leaving my original answer for anyone's benefit...
I just recorded a macro and played with it ...
Got this great result:
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
'Turn it on
Selection.Format.Line.Visible = msoTrue
'Turn it off
Selection.Format.Line.Visible = msoFalse
This is eqiuvalent to selecting No Line in the Line Color section of the Format Data Series dialog box.
When I get stumped in Excel, I always try recording a macro, a la WWBGD? (What Would Bill Gates Do?)
Here's the result for a bar chart where I set the fill to "None":
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
Selection.Format.Fill.Visible = msoFalse
Does that help?
The above 2 did not work for me, in case of ChartType = xlLineMarkers.
What did work was:
For Each mySeries In .SeriesCollection
With mySeries
.Border.LineStyle = xlNone
.MarkerStyle = xlNone
.MarkerBackgroundColorIndex = xlColorIndexNone
End With
Next
However, the legend does still display the line. It is necessary to delete that entry separately.
In Excel 2013, to make a serie invisible, taking it also out of the legend etc, use the filter option, .
Vba Example:
The Following code Hides / Restores the series by filtering it Out/In of the chart.
ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).IsFiltered = True / False
This is equivalent to manually filtering a series in/out using the Ribbon command Design + Select Data, (which is available when the char is selected).
All series options, like dashes colors, markers etc.. are restored unchanged.
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(linenumber).Select
Selection.Format.Fill.Visible = msoFalse
This works great, select the chart than i have 24 lines, with a listview with checkboxes and with 24 entries. Click on 1 and it disapears, click again and its back. You alsow see the line dis- and appear in the legend.