Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chart axis label format vba settings

Tags:

excel

vba

I'm writing vb script to generate charts. On the X axis, I have have the date and on the Y axis, the temp.

On the X axis I want to present time with the format "dd-mm". My data looks like this:

2014-06-17 01:00
2014-06-17 02:00
2014-06-17 03:00
2014-06-17 04:00
2014-06-17 05:00
2014-06-17 06:00
2014-06-17 07:00
2014-06-17 08:00
2014-06-17 09:00

And this is what I have written in vb script so far:

 With chtChart.Chart
    .HasTitle = True
    .ChartTitle.Text = sheetName & vbCr & "2014"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
    .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
    .Axes(xlCategory, xlPrimary).MinimumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).MaximumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "dd-mm"

    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature [C]"
    End With

Unfortunately, when I generate the chart, the entire date value (e.g. 2014-06-07 01:00) is being applied to the X axis.

Any thoughts/ideas on how I can fix what I have?

Update:

whole code to create charts:

Function AddChartSheet(sheetName As String, title As String) As Boolean
    Dim ws As Worksheet
   Dim chtChart As ChartObject
   Dim measDataSheetName As String
   'Create a new chart.
   measDataSheetName = sheetName & "_measurements.csv"

   Dim Lastrow As Integer
   Dim seriesNames() As String

   ActiveWorkbook.Sheets.Add.name = sheetName & " chart"
   Set ws = ActiveWorkbook.Sheets(sheetName & " chart")

   Set chtChart = ActiveSheet.ChartObjects.Add(Left:=25, Top:=25, _
        Width:=700, Height:=500)

    With chtChart
        .name = sheetName
    End With


   Lastrow = ActiveWorkbook.Sheets(measDataSheetName).Cells(ActiveWorkbook.Sheets(measDataSheetName).Rows.Count, "P").End(xlUp).Ro

  With chtChart.Chart
    .HasTitle = True
    .ChartTitle.Text = sheetName & vbCr & "2014"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
    .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
    .Axes(xlCategory, xlPrimary).MinimumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).MaximumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "dd-mm"

    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature [C]"
    End With

   With chtChart.Chart.SeriesCollection.NewSeries
      .name = "Supply"
      .ChartType = xlXYScatterSmoothNoMarkers

      .XValues = Worksheets(measDataSheetName).Range("P2:P" & Lastrow) '. SelectRange("C3", Range("C3").End(xlDown))
      .Values = Worksheets(measDataSheetName).Range("T2:T" & Lastrow)

   End With


   With chtChart.Chart.SeriesCollection.NewSeries
      .name = "Return"
      .ChartType = xlXYScatterSmoothNoMarkers

      .XValues = Worksheets(measDataSheetName).Range("P2:P" & Lastrow) '. SelectRange("C3", Range("C3").End(xlDown))
      .Values = Worksheets(measDataSheetName).Range("U2:U" & Lastrow)

   End With
   AddChartSheet = True

End Function
like image 450
JosiP Avatar asked Oct 11 '25 22:10

JosiP


1 Answers

Ok i did find solution: add formatting after sending data:

 With chtChart.Chart
    .HasTitle = True
    .ChartTitle.Text = sheetName & vbCr & "2014"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
    .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
    .Axes(xlCategory, xlPrimary).MinimumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).MaximumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "dd-mm"

    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature [C]"
    End With

   With chtChart.Chart.SeriesCollection.NewSeries
      .name = "Supply"
      .ChartType = xlXYScatterSmoothNoMarkers

      .XValues = Worksheets(measDataSheetName).Range("P2:P" & Lastrow) '. SelectRange("C3", Range("C3").End(xlDown))
      .Values = Worksheets(measDataSheetName).Range("T2:T" & Lastrow)

   End With

 With chtChart.Chart
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "dd-mm"

   End With

and it worked out.

like image 147
JosiP Avatar answered Oct 14 '25 12:10

JosiP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!