这是我的问题:我有一个用户指定数量的数据集,我想在聚类列图上绘制。我正在使用visual basic创建图表,我将数据集添加为单独的系列,以便它们可以通过颜色区分并在图例上具有不同的标题:
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轴值,而不是基于系列位置??
答案 0 :(得分: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子程序与问题中使用的相同。我的一次测试运行的最终结果如下:
列出了项目编号,但由于机密性,类别标签会从图片中删除。他们的阅读类似于"系列1","系列2"和"系列3"