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.
Related
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?
I am creating a VB.Net chart with date/time as the xAxis labels. It is working fine but what I want to do is sync the second label to a even hour boundary. The chart should have the first label as the first date/time in the dataset, but the labels following would then be on a 6 hour (00:00 - 06:00 - 12:00 - 18:00) boundry. I could also live with not having the first axis label as long as the rest were on a 6 (or 12, depending on the length of the data) hour boundary.
Current:
07:00-------13:00-------19:00-------01:00-------07:00-------13:00
What I'm after:
07:00--12:00-------18:00-------00:00-------06:00-------12:00
Chart as currently produced
I've got it... sort of.
For area As Integer = 0 To numLanes -1
myArea = New ChartArea("myArea" & area.ToString)
With myArea
.BackColor = mySetup.colorPlotBackground ' set from user preferences
.Area3DStyle.Enable3D = mySetup.is3D ' set from user preferences
.AxisX.LabelStyle.Format = "g"
.AxisX.Interval = 1
.AxisX.IntervalType = DateTimeIntervalType.Days
.AxisX.IntervalOffset = 0
.AxisX.IntervalOffsetType = DateTimeIntervalType.Hours ' Set the X value type of the displayed series to DateTime.
End With
cht.ChartAreas.Add(myArea)
Produces the attached chart.
Slightly Improved
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
I want to Change the captions of several Label boxes. The Labels are sequentially named (DAY1, DAY2, DAY3...DAY14). I need help finding a way to do this;
DAY1.Caption = "1"
without implicitly using the label name...more like:
("DAY" & i).Caption = 1
Where i is an integer. I get a variety of error. My guess is that I don't know the proper object variables or syntax. Any ideas?
I assume your question is in the context of VBA (please edit your question to add the "VBA" tag if so). A nice solution would be to retain a reference to your labels as you create them. You can programmatically create a list of labels like this:
Dim label As Label
Dim dayLabels As New List(Of Label)
For i = 1 To 7
Set label = UserForm.Controls.Add("Forms.Label.1", "Day" & i, True)
dayLabels.Add(label)
With label
.Caption = "Day" & i
.Left = 10
.Width = 50
.Top = 10 * i
End With
Next
Note that you need to show your UserForm as vbModeless to use this code. Also note that the positioning of the labels is accomplished with the .Top and .Left fields; I am using the .Top to value to avoid overlapping of the controls (but you could also use .Left, for example, to distribute them horizontally).
Now that you have all label references stored in the list, you can simply refer to them by index like you were originally trying to do:
dayLabels(3).Text = "The text to appear on Day3 label"
I like where your heads at. I run into two problems. Access VBA registers a compile error for
Dim daylabels As New List(Of Label)
If I remove that line, VBA doesn't recognize any object(s) in line 4;
set label = Userform.Controls.Add("Forms.Label.1, "Day" & I, True
I realize I didn't specify that I was using VBA for Access. I tried playing with the wording. Excel has Userforms but Access treats forms differently...no luck. Here is the full Sub I am using;
Private Sub Command6_Click()
Dim DURATION As Integer
Dim i As Integer
Dim x As Label
DURATION = ((END_DATE.Value) - (START_DATE.Value)) + 1
i = 1
For i = 1 To DURATION
Set x = UserForm.Comtrols.Add("Forms.Label.1", "DAYx" & i, True)
With x
.Caption = "Day" & i
.Left = 10
.Width = 50
.Top = 30 + (10 * i)
End With
Next
I'm using VBA to create some graphics I need to do.
Basically, what I want to do is to create the 1st series automatically and then, the 2nd series, copy the color and format from the 1st series.
I'm trying to do this code but with no success:
ActiveChart.SeriesCollection(a - 1).Select
ActiveChart.SeriesCollection(a).Border.ColorIndex = ActiveChart.SeriesCollection(a - 1).Border.ColorIndex
ActiveChart.SeriesCollection(a).MarkerBackgroundColorIndex = ActiveChart.SeriesCollection(a - 1).MarkerBackgroundColorIndex
ActiveChart.SeriesCollection(a).MarkerForegroundColorIndex = ActiveChart.SeriesCollection(a - 1).MarkerForegroundColorIndex
ActiveChart.SeriesCollection(a).MarkerStyle = ActiveChart.SeriesCollection(a - 1).MarkerStyle
Can someone please help me on how to read the properties from the previous series and apply them to the next?
Thanks.
In general something like this could work:
Sub Tester()
Dim Cht as Chart
Dim a As Series, b As Series, x As Long
Set cht = ActiveChart
Set a = cht.SeriesCollection(1)
For x = 2 To cht.SeriesCollection.Count
Set b = cht.SeriesCollection(x)
b.Border.Color = a.Border.Color
b.MarkerBackgroundColor = a.MarkerBackgroundColor
b.MarkerForegroundColor = a.MarkerForegroundColor
b.MarkerStyle = a.MarkerStyle
Next x
End Sub
Note however that some properties will not be read unless the first series was manually formatted, and is not just the "default" format.
Eg: see Fill and Border color property of data point marker (scatter or line) Excel VBA for similar problem.