python openpyxl bar chart set datalabels position to center - openpyxl

How can I set Bar Chart datalabels position (for instance, center).
I'm trying:
Bar = BarChart()
Bar.dataLabels.showVal = True
Bar.dataLabels.dLblsPos ='ctr'
I'm new to Python. Any help would be highly appreciated.

Related

How to maintain the size of fonts in anchored MSGraph Chart object

I have a anchored MSGraph Chart object on my form to stretch left and bottom. It stretches well but the fonts also stretch. How do I maintain the same font size for all the Data labels but still be able to view the graph in full size on stretched or zoomed mode? My aim is to keep font sizes for all Data labels and Axis to 11pt. The original font size for the Data Labels and Axis is 8pt
Edit:
I have notice that when I select the Zoom or Stretch features in the Chart's properties sheet, the chart stretches and the so does the fonts. This does not happen if I select Clip
Is it possible to make the chart stretch whilst maintaining the font sizes to 11pt? I am asking this because when I looked at the chart on a 4:3 screen today, some fonts were barely visible. Please see the image of my current graph and together with its properties settings below:
I have inserted a Stacked Layout with two cells onto the the form, then inserted the chart object in the first cell and left the second cell empty.
A good thing is that an empty cell can be anchored to Strech when the form resizes to suit the screen's aspect ratio.
I then adjusted the height of the blank cell and anchored both the object and the blank cell to Sctrech Down and Across.
On the chart properties, the chart Size Mode must be set to Strecth
Then used the below code on the form open event:
Note: This code requires the use of Microsoft Graph Object Library which should be added using the Tools -> References feature on the code editor.
I am using Microsoft Graph 16.0 Object Library.
I shall be exploring how to use latebinding inorder to avoid this step or perhalps somone can comment.
Dim myChart As Graph.Chart
Dim myChartSeries As Graph.Series
Dim mySeriesDataLabel As Graph.DataLabel
Set myChart = Me.myGraph.Object
For Each myChartSeries In myChart.SeriesCollection
For Each mySeriesDataLabel In myChartSeries.DataLabels
mySeriesDataLabel.Font.Name = "Times New Roman"
mySeriesDataLabel.Font.FontStyle = "Normal"
mySeriesDataLabel.Font.Size = 8
Next mySeriesDataLabel
Next myChartSeries
With Me.myGraph.Axes(1).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With
With Me.myGraph.Axes(2).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With
Without Anchoring:
After Anchoring:

Gridlines showing on my teechart graph in .net

So I have a TeeChart graph in VB.net winForms.
It has a few logarithmic gridlines on it.
However, when I add labels, I get extra gridlines too. I never wanted these there, and I'm wondering if anyone's got ideas on how to get rid of them.
This is the code I ran:
tcRPP.Axes.Bottom.Labels.Items.Clear()
tcRPP.Axes.Bottom.Labels.Items.Add(xMin / 2.3D, "Points in bucket:")
For Each group In xGrouped
tcRPP.Axes.Bottom.Labels.Items.Add(group.Average(Function(point) point.X), CStr(group.Count()))
Next
My graph now looks like this:
Any help would be much appreciated!
In the end I hid the graph altogether by saying:
Dim pointSeries As Series = chartseriesAsList().FirstOrDefault(Function(x) x.GetType() = GetType(Points3D))
Dim xAxis = pointSeries.GetHorizAxis
xAxis.Grid.Visible = false
Dim yAxis = pointSeries.GetVertAxis
yAxis.Grid.Visible = false

Excel VBA changing properties of Form Control on Chart Tab

I have added an Label on my Chart tab, but when I try to change it's font all of the fields are greyed out. I have searched enough but didn't find any solution. I am attaching a screenshot of my chart tab.
I want to change the font properties of this Label 10 on the chart tab. Is there a way I can access the properties by VBA.
To change the text I recorded a macro.
ActiveChart.Shapes("Label 10").Select
Selection.Characters.Text = "Mohit"
ActiveChart.ChartArea.Select
I tried the line code:
Selection.TextFrame.Characters.Font.Size = 20
but it doesn't work. is there a way to change the color and font size of this label ?
Alternate that I am using now
Now I am using Rectangle shape instead of Label. What I wanted to acheive was to display the name of the selected button ( PV , Terrorism or SRCC ) as an indication that this button have been selected. So I clicked PV and my series changed and Rectangle displayed the name of selected Peril.
Now if I select SRCC again the same thing happens.
For the code I only have to change the text of the rectangle every-time a button is pressed as I preset the font and size of text once which remains the same. I wasn't able to change the font and size of text with a label.
ActiveChart.Shapes("Label 10").Select
With Selection
.Characters.Text = "Mohit"
.Font.Size = 20
End With
or you can avoid Select/Selection accessing your Chart object by its name:
With Charts("ChartName").Shapes("Label 10").TextFrame2.TextRange
.Text = "Mohit"
.Font.Size = 20
End With

DevExpress SpreadSheetControl Chart set line color /style

DevExpress website is unable to tell me. How can I influence the line color, thickness, etc. of created graphs?
Dim chart As Chart = myworb.Worksheets(1).Charts.Add(Charts.ChartType.ScatterLine)
chart.TopLeftCell = myworb.Worksheets(1).Cells("A83")
chart.BottomRightCell = myworb.Worksheets(1).Cells("F112")
chart.PlotArea.Fill.SetNoFill()
chart.Series.Add(myworb.Worksheets(1)("A1:A71"), myworb.Worksheets(1)("B1:B71"))
chart.Series.Add(myworb.Worksheets(1)("D1:D81"), myworb.Worksheets(1)("E1:E81"))
chart.Series.Add(myworb.Worksheets(1)("G1:G81"), myworb.Worksheets(1)("H1:H81"))
chart.Series.Add(myworb.Worksheets(1)("J1:J71"), myworb.Worksheets(1)("K1:K71"))
chart.Series.Add(myworb.Worksheets(1)("M1:M66"), myworb.Worksheets(1)("N1:N66"))
chart.Title.Visible = False
Dim axisX = chart.PrimaryAxes(0)
axisX.MajorTickMarks = AxisTickMarks.None
axisX.Scaling.AutoMax = False
axisX.Scaling.AutoMin = False
axisX.Scaling.Max = maxX1
axisX.Scaling.Min = minX1
Dim axisY = chart.PrimaryAxes(1)
axisY.MajorTickMarks = AxisTickMarks.None
axisY.Scaling.AutoMax = False
axisY.Scaling.AutoMin = False
axisY.Scaling.Max = maxY1
axisY.Scaling.Min = minY1
Normally I would just create an Excelfile as a template, load it into the control an just fill the values the chart is using as a series. But as I have to scale it manually because Excel autoscale is NOT WORKING, I have to create the whole chart from scratch. I am unable to find information about how I can set colors etc in the chart programatically. Any help is greatly apreciated.
You can set the color and thickness by using ShapeFormat.Outline property of your series objects. Call to ShapeOutlineFill.SetSolidFill method to set the color of your line and use the ShapeOutline.Width property to set the line width.
Here is example:
chart.Series(5).Outline.SetSolidFill(Color.Magenta)
chart.Series(5).Outline.Width = 20
The "lines" are the chart series and can be accessed in the Series property. From there you can change various things about the series including the color. Alternatively you can custom draw series yourself using the CustomDrawSeries event.

Excel VBA: Positioning and Alignment of Shapes

I am trying to make a printable report from excel sheet with some shapes such as charts and pictures. I am able to put the charts on the sheet using the below code:
oSheetReport.Range("A51").Select()
Dim oChart1 As Excel.Shape
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlLine
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
oSheetReport.Range("A70").Select()
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
oSheetReport.Range("A100").Select()
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked100
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
But positioning and alignment is failing which is making my reports look ugly. Any better way of achieving this?
To add, position and align in one step you can use the following:
Set oChart1 = ActiveSheet.ChartObjects.Add _
(Left:=250, Width:=375, Top:=75, Height:=225)
Left is the left alignment and Top is the top alignment. Width and Height - well, you can figure that out!
More info at http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html