2015-12-09 49 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”