代码之家  ›  专栏  ›  技术社区  ›  Fredrik

VBA图表x轴不适合

  •  0
  • Fredrik  · 技术社区  · 6 年前

    chart example

    替换为孔代码: 如下所示,我的combobox1包含一个可以从字典中读取的项,我可以通过它访问它 mainGUI.getDiagramFunc

    If ComboBox1.text = "Select item" Or ComboBox1.text = "" Then Exit Sub
        Dim chartIndex As Integer
        ReDim chartItems(1) As String
        ReDim chartdata(20) As Long
        Dim myChart As Chart
        Dim mySeries As Series
        Dim index As Long: index = -1
        Dim value As Variant
        Dim temp As Variant: temp = split(mainGUI.getDiagramFunc.item(ComboBox1.text), ",")
        For Each value In temp
            index = index + 1
            If UBound(chartItems) <= index Then ReDim Preserve chartItems(index)
            chartItems(index) = mainGUI.getCalcKey(ComboBox1.text & "_*" & value)
        Next value
    
        ReDim chartdata(UBound(chartItems))
        index = -1
        For Each value In chartItems
            index = index + 1
            chartdata(index) = doCalculation(CStr(value))
        Next value
        On Error GoTo errorhandler
        Set myChart = ActiveWorkbook.Charts(1)
        Set mySeries = myChart.SeriesCollection(1)
    
        With mySeries
            .ChartType = xlColumnClustered
            .XValues = temp
            .Values = chartdata
        End With
    
        Dim picFileName As String
        picFileName = "C:\Users\extmartefr\Desktop\data\mychart.gif"
        myChart.Export Filename:=picFileName, Filtername:="GIF"
        Image1.Picture = LoadPicture(picFileName)
        Exit Sub
    
    1 回复  |  直到 6 年前
        1
  •  -1
  •   TempleGuard527    6 年前

    我同意@ashleedawg,你的问题不是特别清楚。不过,我想我能帮上忙。

    尝试添加一行代码,检测您的最大值并设置x轴最大值,如下所示:

    myChart.Axes(xlCategory).MaximumScale = (Your code to determine max x-value)