我希望突出显示的 X/Y 坐标由同一行上的突出显示的文本标记。
使用这些数据:
尽管向其提供了我所选择的列中的数据(E 代表标签,J/K 代表 X/Y 点),但 X/Y 点上的标签与 X/Y 点所在的同一行上的标签并不匹配。
图表上的标签来自正确的列,但与获取 X/Y 数据的同一行不相关。
显然我误解了一些东西。我怎样才能让 E 列中的标签应用于 J/K 数据同一行?
[编辑:]更令人困惑的是,它似乎是相关的,但与其配对的 X/Y 坐标在该列下方有八行。但我已经仔细检查了我要输入图表的起始行...
答案1
如果不使用 VBA 或附加组件,这是不可能做到的。
本文解释一段简单的 VBA 代码来标记点。
我修改了它以适应上述情况。但是,它必须通过单击按钮手动触发。
Sub ApplyChartLabelsFromRange(chartName As String, labelColumn As String, startingRow As Integer)
Dim myChart As ChartObject
Set myChart = ActiveSheet.ChartObjects(chartName)
Dim chartSeries As Series
Set chartSeries = myChart.chart.SeriesCollection(1)
chartSeries.HasDataLabels = True
Dim row As Integer
row = startingRow 'Our starting row
Dim chartPoint As Point
For Each chartPoint In chartSeries.Points
'Apparently the Excel chart creates every Point even if they have empty cells in the range,
'so I don't need to worry about skipping empty cells.
'If IsNumeric(ActiveWorkbook.Worksheets("Data").Cells(row, "J").Value) Then
chartPoint.DataLabel.Text = ActiveWorkbook.Worksheets("Data").Cells(row, labelColumn).Value
'End If
row = row + 1
Next chartPoint
End Sub
Sub HideChartLabels(chartName As String)
Dim myChart As ChartObject
Set myChart = ActiveSheet.ChartObjects(chartName)
Dim chartSeries As Series
Set chartSeries = myChart.chart.SeriesCollection(1)
'If we merely set this to false, it'd hide the labels, but also lose all label formatting for when we re-show the labels.
'So, instead, we just change the label text.
chartSeries.HasDataLabels = True
Dim myPoint As Point
For Each myPoint In chartSeries.Points
myPoint.DataLabel.Text = ""
Next myPoint
End Sub
Sub Map_ShowLabels()
Call ApplyChartLabelsFromRange("Tree Map", "E", 7)
End Sub
Sub Map_HideLabels()
Call HideChartLabels("Tree Map")
End Sub