是否有可能在excel中绘制这样的图表?怎么样?
最佳答案 这是一个有趣的.我不知道是否有一种很好的方法来扩展Excel中现有的盒子和胡须图表的功能,但幸运的是我能够用一种快速而肮脏的方式用散点图来伪造它.基本上,我们只是沿着间隔迭代并选择足够的点来显示在散点图上,它看起来与盒子和胡须无法区分.因为它使每个间隔成为不同的系列,所以您还可以格式化着色等内容,以便更容易区分它们.
这是我的一些数据的最终结果(您可以修改底层散点图使用的标记来更改线条粗细等内容):
代码正在查找带有每个系列名称的格式化表,以及每个维度的最小值/最大值的单独列.以下是输入表的格式:
最后,这是我运行的宏来生成它:
' Build and display an "Interval Chart"
Public Sub MakeIntervalChart()
Dim inRow As Long, outRow As Long, lastRow As Long, startRow As Long
Dim interX As Double, interY As Double, intervalAmt As Double
intervalAmt = 0.01 ' CHANGE ME
' Source / Destination Worksheet Parameters (CHANGE ME)
Dim wsSource As Worksheet, wsDest As Worksheet
Set wsSource = Worksheets("Data")
Set wsDest = Worksheets.Add()
wsDest.Name = "IntervalChart"
' Create output chart
Dim boxChart As Chart
Set boxChart = wsDest.Shapes.AddChart2(240, xlXYScatter).Chart
boxChart.HasLegend = True
outRow = 1
lastRow = wsSource.Cells(wsSource.Rows.Count, 1).End(xlUp).Row
For inRow = 2 To lastRow
' Retrieve current interval (CHANGE ME)
Dim seriesName As String
Dim minX As Double, maxX As Double, minY As Double, maxY As Double
seriesName = wsSource.Cells(inRow, 1)
minX = wsSource.Cells(inRow, 2)
maxX = wsSource.Cells(inRow, 3)
minY = wsSource.Cells(inRow, 4)
maxY = wsSource.Cells(inRow, 5)
startRow = outRow
'intervalAmt = (maxX - minX) / 50.0
' Top and Bottom of box
For interX = minX To maxX Step intervalAmt
wsDest.Cells(outRow, 1) = seriesName
wsDest.Cells(outRow, 2) = interX
wsDest.Cells(outRow, 3) = minY
outRow = outRow + 1
wsDest.Cells(outRow, 1) = seriesName
wsDest.Cells(outRow, 2) = interX
wsDest.Cells(outRow, 3) = maxY
outRow = outRow + 1
Next
'intervalAmt = (maxY - minY) / 50.0
' Left and Right of box
For interY = minY To maxY Step intervalAmt
wsDest.Cells(outRow, 1) = seriesName
wsDest.Cells(outRow, 2) = minX
wsDest.Cells(outRow, 3) = interY
outRow = outRow + 1
wsDest.Cells(outRow, 1) = seriesName
wsDest.Cells(outRow, 2) = maxX
wsDest.Cells(outRow, 3) = interY
outRow = outRow + 1
Next
' Add new series (box)
With boxChart.SeriesCollection.newSeries()
.Name = seriesName
.XValues = wsDest.Range("B" & startRow & ":B" & outRow - 1)
.Values = wsDest.Range("C" & startRow & ":C" & outRow - 1)
End With
Next
End Sub
您需要更改输入/输出工作表名称和输入表的预期格式(假设您不希望被我的格式卡住).您还需要调整数据的0.01参数.我把它放在一个非常小的间隔,你可以使用你的样本数据.您可以根据需要将此增量值调整为0.01以外的值,或者您可以使用注释行intervalAmt =(maxX – minX)/ 50.0中的代码并调整50.0值(这将是标记的数量)绘制每个框的部分).小心调整这些值 – 你可能很容易浪费大量的时间来生成比你需要更多的标记,以实现一条直线.