作者:炫彩十字绣I_775 | 来源:互联网 | 2023-05-16 08:16
IwanttohavelabelsnexttodatapointsinanExcelchart.ThereisaVBAcodefromMicrosoftfor
I want to have labels next to data points in an Excel chart. There is a VBA code from Microsoft for this purpose:
我想在Excel图表中的数据点旁边放置标签。为此目的,Microsoft提供了一个VBA代码:
http://support2.microsoft.com/kb/914813/en-us
Sub AttachLabelsToPoints()
'Dimension variables.
Dim Counter As Integer, ChartName As String, xVals As String
' Disable screen updating while the subroutine is run.
Application.ScreenUpdating = False
'Store the formula for the first series in "xVals".
xVals = ActiveChart.SeriesCollection(1).Formula
'Extract the range for the data from xVals.
xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
Do While Left(xVals, 1) = ","
xVals = Mid(xVals, 2)
Loop
'Attach a label to each data point in the chart.
For Counter = 1 To Range(xVals).Cells.Count
ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
True
ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter
End Sub
It works so far. But only if the collection has no name:
它到目前为止工作。但只有当集合没有名称时:
When I name the collection then the macro returns an error:
当我命名该集合时,宏返回一个错误:
Does anyone know how to use the code provided by Mircosoft and still be able to name the data collection?
有谁知道如何使用Mircosoft提供的代码,仍然能够命名数据集合?
4 个解决方案
2
There already are some good answers like ZAT's one, explaining how to add labels to a data point in native Excel with VBA language.
已经有一些很好的答案,比如ZAT的答案,解释了如何使用VBA语言向本机Excel中的数据点添加标签。
But if you don't know anything about VBA it might be difficult to understand. For complex charts like this one I prefer to use Javascript which I think is more "readable" than VBA. And if you want to make a dynamic and interactive chart, Javascript comes with a lot of powerful libraries.
但如果您对VBA一无所知,可能很难理解。对于像这样的复杂图表,我更喜欢使用Javascript,我认为它比VBA更“可读”。如果你想制作一个动态的交互式图表,Javascript带有很多强大的库。
Here is a working code I have written for you, with plotly.js (the documentation is very good for js beginners) :
这是我为你编写的一个工作代码,带有plotly.js(文档非常适合js初学者):
https://www.funfun.io/1/#/edit/5a60bbe7404f66229bda3e39
So to build this chart I put my data in the embedded spreadsheet, which I can then use in my Javascript code thanks to a Json file.
因此,为了构建此图表,我将我的数据放入嵌入电子表格中,然后由于Json文件,我可以在我的Javascript代码中使用它。
I can create a scatter plot like so :
我可以像这样创建一个散点图:
var trace1 = {
x: firstX,
y: firstY,
text: firstLabel,
mode: 'markers+text',
textposition:'top right'
};
The firstX
and firstY
variable are the X and Y values.
firstX和firstY变量是X和Y值。
To add a label to each point I added a label to text
and changed the mode to marker+text
instead of just marker
.
要为每个点添加标签,我在文本中添加了标签,并将模式更改为标记+ text而不仅仅是标记。
Once you've made your chart you can load it in Excel by passing the URL in an Excel add-in called Funfun.
创建图表后,可以通过在名为Funfun的Excel加载项中传递URL将其加载到Excel中。
Here is how it looks like:
它是这样的:
Disclosure : I’m a developer of funfun
披露:我是funfun的开发者