這是我的問題:我有一個用戶指定數量的數據集,我想繪製在聚簇列圖上。我創建Visual Basic中的圖表,我添加數據集作爲獨立的系列,讓他們通過顏色區分和傳說有不同的稱謂:Excel VBA:創建一個基於值不繫列排序的聚集列圖表?
ActiveWorkbook.Charts.Add 'all of this just adds a new chart
ActiveChart.ChartArea.Select
With ActiveChart
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = "Ordered Distribution Graph"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Item"
.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Total"
.Legend.Position = xlLegendPositionBottom
End With
ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.count)
ActiveSheet.Name = "Distribution Chart"
For j = 0 To UBound(chartLabels) 'here is where I handle the data based on global variables
If IsEmpty(chartLabels(j)) Then Exit For
Erase xval
Erase yval
ReDim Preserve xval(0 To 0)
ReDim Preserve yval(0 To 0)
xval(0) = chartData(0, j, 0)
yval(0) = chartData(2, j, 0)
For i = 0 To UBound(chartData, 3) - 1
If Not IsEmpty(chartData(2, j, i + 1)) Then
ReDim Preserve xval(0 To i + 1)
ReDim Preserve yval(0 To i + 1)
xval(i + 1) = chartData(0, j, i + 1)
yval(i + 1) = chartData(2, j, i + 1)
End If
Next
Call bubblesortData(j, UBound(xval)) 'separate sort function
ActiveChart.SeriesCollection.NewSeries 'plots each series
ActiveChart.SeriesCollection(j + 1).XValues = xval
ActiveChart.SeriesCollection(j + 1).Values = yval
ActiveChart.SeriesCollection(j + 1).Name = main.chartLabels(j)
ActiveChart.ChartGroups(1).GapWidth = 10
ActiveChart.ChartGroups(1).Overlap = -10
Next
Sheets(ActiveWorkbook.Sheets.count).Activate
目前,各組數據採用分類在bubblesortData(setNumber,numberOfDataPoints)子程序(XVAL和yval全局數組):
Sub bubblesortLosses(b As Variant, tot As Variant)
Dim changed As Integer, temp As Variant
Do
changed = 0
For i = 0 To tot - 1
If Not IsEmpty(xval(i)) Then
If yval(i) > yval(i + 1) Then
temp = xval(i)
xval(i) = xval(i + 1)
xval(i + 1) = temp
temp = yval(i)
yval(i) = yval(i + 1)
yval(i + 1) = temp
changed = 1
End If
End If
Next
Loop Until changed = 0
End Sub
這是工作正常,但結果是這樣的:
每套都是根據我的排序排序,但我想所有的數據都要根據y軸值排序。我想不出一種方法來實現這一點,同時保持數據按系列分隔。有沒有辦法根據相應的y軸值顯示x軸值,而不是基於系列位置?
調整數據陣列大小並在必要時填入空白值,以便每個系列具有相同數量的數據點。考慮到Excel/VBA使用數組的限制,可能不是最容易實現的。你可以顯示函數'bubbleSortData'的代碼嗎? –
或者,將其創建爲單個數據系列,並將着色格式選擇性地應用於每個數據點。再次,可能不容易*考慮需要進行的排序並確保它們保持映射到正確的顏色。在這種情況下使用'Dictionary'或'Collection'可能比數組好。 –
@DavidZemens我添加了排序代碼,但是我認爲你是正確的,因爲你列出的選項可能是唯一的我必須繼續。感謝您的幫助! –