2015-12-09 53 views
0

這是我的問題:我有一個用戶指定數量的數據集,我想繪製在聚簇列圖上。我創建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 

這是工作正常,但結果是這樣的:

ExampleChart

每套都是根據我的排序排序,但我想所有的數據都要根據y軸值排序。我想不出一種方法來實現這一點,同時保持數據按系列分隔。有沒有辦法根據相應的y軸值顯示x軸值,而不是基於系列位置?

+0

調整數據陣列大小並在必要時填入空白值,以便每個系列具有相同數量的數據點。考慮到Excel/VBA使用數組的限制,可能不是最容易實現的。你可以顯示函數'bubbleSortData'的代碼嗎? –

+0

或者,將其創建爲單個數據系列,並將着色格式選擇性地應用於每個數據點。再次,可能不容易*考慮需要進行的排序並確保它們保持映射到正確的顏色。在這種情況下使用'Dictionary'或'Collection'可能比數組好。 –

+0

@DavidZemens我添加了排序代碼,但是我認爲你是正確的,因爲你列出的選項可能是唯一的我必須繼續。感謝您的幫助! –

回答

0

很多搜​​索之後,我發現,工作對我來說,從這個環節主要是繪製信息化解決方案的組合:http://peltiertech.com/chart-with-a-dual-category-axis/

表示,這樣做是...以及從各種StackOverflow的帖子不可能以編程方式進行,必須通過工作表完成,這對我而言已經奏效。我在上面的鏈接中填寫了工作表單元格,除了使用visual basic。然後,在數據繪製完成後,我隱藏了工作表。這適用於我,因爲每次用戶重新開始使用新的數據集時,工作表都會被清除。這是我的代碼:

Sub Distribution() 
Dim runningTotal, seriesNumber, sheetName 

seriesNumber = 1 
runningTotal = 2 

currDist = currDist + 1 
sheetName = "DistData" + CStr(currDist) 

ActiveWorkbook.Sheets.Add 
ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.count) 
ActiveSheet.Name = sheetName 
ActiveSheet.Visible = True 

For j = 0 To UBound(chartLabels) 
    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 bubblesortLosses(j, UBound(xval)) 

    Sheets(sheetName).Select 

    Cells(1, seriesNumber + 2) = chartLabels(j) 
    Cells(runningTotal, 1) = chartLabels(j) 

    For k = 0 To UBound(xval) 
     Cells(runningTotal, 2) = xval(k) 
     Cells(runningTotal, seriesNumber + 2) = yval(k) 
     runningTotal = runningTotal + 1 
    Next 

    seriesNumber = seriesNumber + 1 

Next 

ActiveWorkbook.Charts.Add 
ActiveChart.ChartArea.Select 
With ActiveChart 
    .ChartType = xlColumnStacked 
    .HasTitle = True 
    .ChartTitle.Text = "Ordered Distribution Graph" 
    .Axes(xlCategory).TickLabels.MultiLevel = True 
    .Axes(xlCategory).HasTitle = True 
    .Axes(xlCategory).AxisTitle.Characters.Text = "Item" 
    .Axes(xlCategory).CategoryType = xlCategoryScale 
    .Axes(xlValue).HasTitle = True 
    .Axes(xlValue).AxisTitle.Characters.Text = "Total" 
    .Legend.Position = xlLegendPositionBottom 
End With 

ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.count) 
ActiveSheet.Name = "Distribution " + CStr(currDist) 

ActiveChart.ChartGroups(1).GapWidth = 10 
ActiveChart.ChartGroups(1).Overlap = 100 

Sheets(sheetName).Visible = False 
Sheets(ActiveWorkbook.Sheets.count).Activate 

End Sub 

bubblesort子例程與問題中使用的相同。我的測試運行的一個最終的結果是在這裏:

enter image description here

項目數列,但類別標籤被切割出來的畫面,由於保密。它們的讀數類似於「系列1」,「系列2」和「系列3」