Column Chart vb.net - vb.net

I want to make a column chart of the occupation of machines per month. I have this data base:
And I'm using this code to make the chart:
private sub
rng1 = folhaexcel5.Range("B2:M2", "B3:M3")
grafico = folhaexcel5.Shapes.AddChart(xlColumns).Chart
grafico.SetSourceData(Source:=rng1)
grafico.HasTitle = True
grafico.ChartTitle.Characters.Text = "Occupation Rate of Machines"
grafico.Axes(XlAxisType.xlValue).MaximumScale = 100
grafico.Axes(XlAxisType.xlValue).HasTitle = True
grafico.Axes(XlAxisType.xlValue).axistitle.characters.text = "Occupation Rate (%)"
grafico.HasLegend = False
folhaexcel5.ChartObjects(1).Top = folhaexcel5.Range("p3").Top
folhaexcel5.ChartObjects(1).left = folhaexcel5.Range("p3").Left
folhaexcel5.ChartObjects(1).height = 300
folhaexcel5.ChartObjects(1).width = 1000
grafico.SeriesCollection(1).Interior.Color = Color.FromArgb(0, 124, 208)
grafico.SeriesCollection(1).Border.Color = Color.FromArgb(0, 55, 92)
End sub
When I run it, the chart is added to the excel file, but it's blank. And when I click in the chart to see the selected data this is showed to me:
I want to put line 2 as x-axis and line 3 as y-values. But it seems that i can't selecte these two lines. Anyone can help me?

Related

Excel Chart - How to draw discontinuous series in VBA without range reference

Excel 2010.
Issue : I need to plot a *single* *discontinuous* series in a XY-scatter chart *via VBA* without referencing a range in the sheet.
It is easy to achieve that when the Yvalues are laid-out in a sheet range, by inserting blank values at the discontinuities ; as long as one selects 'Show empty cells as: Gaps' in Select Data > Hidden and Empty Cells. Here is an example (the Series2 in red is the one that matters) :
So I was trying to reproduce the same via VBA :
Sub addDiscountinuousSingleSeries()
Dim vx As Variant, vy As Variant
Dim chrtObj As ChartObject, chrt As Chart, ser As Series
Set chrtObj = ActiveSheet.ChartObjects("MyChart"): Set chrt = chrtObj.Chart
Set ser = chrt.SeriesCollection.NewSeries
vx = Array(0.3, 0.3, 0.3, 0.7, 0.7, 0.7)
vy = Array(-1, 1, vbNullString, -1, 1, vbNullString)
'vy = Array(-1, 1, CVErr(xlErrNA), -1, 1, CVErr(xlErrNA)) 'doesn't work either
'vy = Range(RANGE_YVALUES_WITH_BLANK) 'this would work, but I do not want to reference a range
chrt.DisplayBlanksAs = xlNotPlotted 'VBA equivalent to 'Show empty cells as: Gaps'
With ser
ser.Name = "VBA Series"
.XValues = vx
.Values = vy
End With
End Sub
But the blank values in the vy array seems to be ignored and the two vertical bars are now connected, which I am trying to avoid (green series).
I know that I could delete the middle line programmatically, but in the real-life problem I am trying to solve it would not be the right solution (too complex, too slow).
My question : is there a way to specify the series' .Values array to get the expected behavior and get a gap between the two vertical green segments in vba (with only one series and no reference to a sheet range)?
You could just format the lines you don't want. Maybe not the prettiest way, but it'd achieve what your after.
ser.Points(3).Format.Line.Visible = msoFalse
ser.Points(4).Format.Line.Visible = msoFalse
ser.Points(6).Format.Line.Visible = msoFalse
Or:
For i = 1 To ser.Points.Count
If i <> 1 Then k = i - 1 Else k = i
If ser.Values(i) = 0 Or ser.Values(k) = 0 Then
ser.Points(i).Format.Line.Visible = msoFalse
End If
Next

Format datatable values in VBA

Currently working on a vba script that makes charts automatically. I would like to add a datatable which is done using: .HasDataTable = True
However I would like to show the values of series as percentages. Currently the value is defined as a Double containing all the values but not the right formatting. Using Format() or FormatPercent() will give the right values but returned in a String. This works for the datatable but not for the chart itself since it doesn't recognize the values anymore.
My question comes down to whether it is possible to show the values as percentages in both the datatable and the chart? Without VBA it is easily done by formatting the data in the cells itself. The problem is that for formatting a String is returned but for the graph Integers or Doubles are needed.
Below is part of the code. If I dim Ratio as String and use FormatPercent() I get the requested formatting but then the values in Ratio ar no longer doubles so it doesn't give the required chart.
Dim Ratio() As Double
Dim labels() As String
ReDim Ratio(1 To Height)
ReDim labels(1 To Height)
For Each Column In sArray
labels(i) = Sheets(DataSheetName).Cells(LabelsRow, Column)
Ratio(i) = Math.Round(Sheets(DataSheetName).Cells(LabelsRow + 3, Column), 2)
i = i + 1
Next Column
Set myChtObj = Sheets(DrawSheetName).ChartObjects.Add(Left:=Left, Width:=Width, Top:=Top, Height:=HeightGraph)
Dim srsNew1 As Series
' Add the chart
With myChtObj.Chart
.ChartArea.Fill.Visible = False
.ChartArea.Border.LineStyle = xlNone
.PlotArea.Format.Fill.Solid
.PlotArea.Format.Fill.Transparency = 1
.HasTitle = True
.ChartTitle.text = Title
.HasLegend = False
.Axes(xlValue).TickLabels.NumberFormat = "0%"
.Axes(xlCategory, xlPrimary).HasTitle = False
'add data table
.HasDataTable = True
' Make Line chart
.ChartType = xlLine
' Add series
Set srsNew1 = .SeriesCollection.NewSeries
With srsNew1
.Values = Ratio
.XValues = labels
.Name = "Ratio"
.Interior.Color = clr3 'RGB(194, 84, 57)
End With
End With

Pie chart code in vb.net

I have used this code for pie chart in vb.net. This is working fine but one problem is when one of the yValue is 0 it is showing 0 in the pie chart.
I want to know how to stop showing 0 value in the pie chart and what I have to correct in the code so that it does not 0 value.
Dim yValues As Double() = {str_total, Totalmale.ToString, Totalfemale.ToString, Totalengagedact.ToString, Totalmaterial.ToString, Totalpattaholder.ToString} ' Getting values from Textboxes
Dim xValues As String() = {"Total Beneficiary", "Male", "Female", "Engaged in same activity before", "Material Support received", "Patta holder"} ' Headings
Dim seriesName As String = Nothing
' Note 1 : Clear chart before fill - VERY IMPORTANT and can generate exception if you are generating
' multiple charts in loop and have not included below lines !
' Note 2 : Chrt variable here is the Name of your Chart
Chart1.Series.Clear()
Chart1.Titles.Clear()
' Give unique Series Name
seriesName = "ChartInv"
Chart1.Series.Add(seriesName)
' Bind X and Y values
Chart1.Series(seriesName).Points.DataBindXY(xValues, yValues)
' Define Custom Chart Colors
Chart1.Series(seriesName).Points(0).Color = Color.MediumSeaGreen
Chart1.Series(seriesName).Points(1).Color = Color.PaleGreen
Chart1.Series(seriesName).Points(2).Color = Color.LawnGreen
Chart1.Series(seriesName).Points(3).Color = Color.Blue
Chart1.Series(seriesName).Points(4).Color = Color.Red
Chart1.Series(seriesName).Points(5).Color = Color.Yellow
' Define Chart Type
Chart1.Series(seriesName).ChartType = DataVisualization.Charting.SeriesChartType.Pie
Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
' If you want to show Chart Legends
Chart1.Legends(0).Enabled = True
' If you don't want to show data values and headings as label inside each Pie in chart
'Chart1.Series(seriesName)("PieLabelStyle") = "Disabled"
'Chart1.Series("ChartInv").IsValueShownAsLabel = False
' If you want to show datavalues as label inside each Pie in chart
Chart1.Series(seriesName)("PieLabelStyle") = "Enabled"
Chart1.Series("ChartInv").IsValueShownAsLabel = True
Take the Xvalues and Yvalues after calculating the 0
for example
consider variable x, and the value for this variable is 0. in that case dont add its reference at either xvalues or Yvalues

VBA Excel Graphing Add time to X-Axis

I am graphing a trend for a pump for a day, I am getting a sample data every minuet
I got the graph all working but the problem I am running into is trying to get the time on the X-axis. There is a column that shows the time each sample data was taken, but when I tried to add it, it put the time for every point in the chart. Because I have 1440 points in the chart, it was just a blob of unreadable text, I removed that code and right now I am doing without. But I would like to figure it out.
'Chart for Monday
With wBM.ActiveSheet.ChartObjects.Add(Left:=300, Width:=300, Top:=10, Height:=300)
.Chart.SetSourceData Source:=wBM.ActiveSheet.Range("B1:B1440"), PlotBy:=xlColumns
.Chart.ChartType = xlLine
.Left = rngChart2.Left
.Top = rngChart2.Top
.Width = rngChart2.Width
.Height = rngChart2.Height
.Chart.HasTitle = True
.Chart.ChartTitle.Text = "Monday " & Monday
.Chart.HasLegend = False
End With
This is my code so far. In Column D has the time for each point so it would be "D1:D1440".
Is there a way to only have 1 label for every 60 points, or 120? So that I don't have such a blob of labels?
Starting with sample data that looks like this:
I ran this macro to hide all labels which 'location' is not multiple of 5
Sub formateDataLabels()
Dim chrt As Chart
Set chrt = ActiveChart
Dim cnt As Long: cnt = 1
For Each lbl In chrt.SeriesCollection(1).DataLabels
If cnt Mod 5 <> 0 Then
lbl.Text = ""
End If
cnt = cnt + 1
Next lbl
End Sub
Result:
Note that the current code is simply set up to run on the selected chart. Change the 5 to whatever you need such as 50 or 100 and adapt to your current chart.

Excel 2010 Chart axis not shown up

I am converting a EXCEL 2003 application to EXCEL 2010. Data are shown up fine but the axis does not show any more. which function to show the axis with automatic scale?
For Example: If you plot the following series in an Excel Line Chart. [0.22,0.33,0.44,0.55,0.66,0.77,0.88,0.99,1.1,1.21,1.32,1.43,1.54,1.65,1.76,1.87,1.98,2.09,2.2] Excel determines that the y-axis values should be [0,0.5,1,1.5,2,2.5] [How does Excel determine the axis values for charts?1. How to make the y-axis with the automatic values [0,0.5,1,1.5,2,2.5] shown in the chart?
Thanks
Updated with related codes -
With ActiveChart
.SeriesCollection(2).Select
'.SeriesCollection(2).AxisGroup = 2
.HasTitle = True
.ChartTitle.Text = OutputTitle & Chr(10) & ChartTitle2
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = AxisTitle1
.Axes(xlValue).AxisTitle.Font.Bold = False
.HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary) = True
.Export Filename:=ExportFile, FilterName:="GIF"
End with
If I uncomment '.SeriesCollection(2).AxisGroup = 2, I will get the y axis to show but the x axis labels are messed up with mismatch with the Values.
Current chart -
Desired chart with scaled axis shown -
To make sure the axis is on use this:
With xlApp.ActiveChart
.HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary) = True
End With
Range values are automatic unless otherwise specified like this:
' Set Axis Scales
With xlApp.Charts("Chart Name").Axes(2)
.MinimumScale = 100
.MaximumScale = 2000
.MajorUnit = 1000
.MinorUnit = 100
End With
Just to be a little more complete try explicitly addressing each value and category and see if that helps.
With xlApp.ActiveChart
.SeriesCollection(1).XValues = "='sheet name'!R21C4:R46C4"
.SeriesCollection(1).Values = "='sheet name'!R21C5:R46C5"
.SeriesCollection(1).Name = "='series name'"
.SeriesCollection(1).axisgroup = Excel.XlAxisGroup.xlPrimary
.HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary) = True
.HasAxis(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary) = True
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasTitle = True
.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "x axis"
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasTitle = True
.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text = "y axis"
End With
I see your axes group is set to 2, are you using dual axis?
Set it like this:
.SeriesCollection(2).axisgroup = Excel.XlAxisGroup.xlPrimary
*Edit*
To set autoscale on the axis:
.Axes(xlValue).MinimumScaleIsAuto = True
.Axes(xlValue).MaximumScaleIsAuto = True
.Axes(xlValue).MinorUnitIsAuto = True
.Axes().MajorUnitIsAuto = True
.Axes().DisplayUnit = xlHundreds