图表轴标签格式vba设置

[英]chart axis label format vba settings


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

我正在编写vb脚本来生成图表。在X轴上,我有日期,在Y轴上有临时。

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

在X轴上,我想以“dd-mm”格式显示时间。我的数据如下所示:

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:

这是我到目前为止用vb脚本编写的内容:

 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.

不幸的是,当我生成图表时,整个日期值(例如2014-06-07 01:00)正在应用于X轴。

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

1 个解决方案

#1


1  

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.

它成功了。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/05/22/cd0f03b16d1cd67b69b2662987c35dd.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告