热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Excel:在XY图表中为数据点添加标签-Excel:AddlabelstodatapointsinXYchart

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:

它到目前为止工作。但只有当集合没有名称时:

enter image description here

When I name the collection then the macro returns an error:

当我命名该集合时,宏返回一个错误:

enter image description here

Does anyone know how to use the code provided by Mircosoft and still be able to name the data collection?

有谁知道如何使用Mircosoft提供的代码,仍然能够命名数据集合?

4 个解决方案

#1


2  

I had the same problem. All you need to do is replace the hardcoded '9' with 'InStr(xVals, ",")' and it will accept any length SERIES name in the field before the first comma.

我有同样的问题。您需要做的就是用'InStr(xVals,“,”)'替换硬编码的'9',它将在第一个逗号之前的字段中接受任何长度的SERIES名称。

#2


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+textinstead 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:

它是这样的:

final

Disclosure : I’m a developer of funfun

披露:我是funfun的开发者

#3


2  

Excel 2013 introduced the capability to label a chart series with data from cells, after many years of users begging for it. Select the series, and add data labels. Select the data labels and format them. Under Label Options in the task pane, look for Label Contains, select the Value From Cells option, and select the range containing the label text.

Excel 2013引入了使用单元格数据标记图表系列的功能,经过多年的用户请求。选择系列,然后添加数据标签。选择数据标签并格式化。在任务窗格的“标签选项”下,查找“标签包含”,选择“从单元格值”选项,然后选择包含标签文本的范围。

enter image description here

And even before this, you could use a free add-in called the XY Chart Labeler (which works on all charts that support data labels, not just XY charts), which you can download from Applications Professionals. It's written by Rob Bovey, a former Microsoft Excel MVP.

甚至在此之前,您可以使用名为XY Chart Labeler的免费加载项(适用于所有支持数据标签的图表,而不仅仅是XY图表),您可以从Applications Professionals下载。它由前微软Excel MVP Rob Bovey编写。

#4


1  

Try this after chart generation (assuming chart in the same sheet): (modify this according to your need)

图表生成后尝试这个(假设图表在同一张表中):(根据您的需要修改)

Option Explicit
Sub RenameChartDataLabel()

Dim rngDLabel As Range
Dim iii as integer, pp as integer, dlcount as integer

Set rngDLabel = ActiveSheet.Range("A2:A6")    'change range for datalabels text
ActiveSheet.ChartObjects("Chart 2").Activate  'change chart name
dlcount = ActiveChart.SeriesCollection(1).DataLabels.Count
iii = 1
pp = 1

For iii = dlcount To 1 Step -1
ActiveChart.SeriesCollection(1).DataLabels(iii).Select
Selection.Text = rngDLabel(pp).Value
Selection.Font.Bold = True
Selection.Position = xlLabelPositionAbove
pp = pp + 1
Next
Set rngDLabel = Nothing
End Sub

enter image description here


推荐阅读
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • 本文探讨了如何在Classic ASP中实现与PHP的hash_hmac('SHA256', $message, pack('H*', $secret))函数等效的哈希生成方法。通过分析不同实现方式及其产生的差异,提供了一种使用Microsoft .NET Framework的解决方案。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 在过去两周中,我们利用 ReportViewer 开发了与生产良率相关的报表,其中每个制程的直通率是所有测试项良率的乘积。由于 ReportViewer 没有内置的累乘函数,因此需要借助自定义代码来实现这一功能。本文将详细介绍实现步骤和相关代码。 ... [详细]
  • 本文详细介绍如何在VSCode中配置自定义代码片段,使其具备与IDEA相似的代码生成快捷键功能。通过具体的Java和HTML代码片段示例,展示配置步骤及效果。 ... [详细]
  • 本文介绍了如何使用 Python 的 Bokeh 库在图表上绘制菱形标记。Bokeh 是一个强大的交互式数据可视化工具,支持丰富的图形自定义选项。 ... [详细]
  • 本文将介绍网易NEC CSS框架的规范及其在实际项目中的应用。通过详细解析其分类和命名规则,探讨如何编写高效、可维护的CSS代码,并分享一些实用的学习心得。 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 实用正则表达式有哪些
    小编给大家分享一下实用正则表达式有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下 ... [详细]
  • 历经三十年的开发,Mathematica 已成为技术计算领域的标杆,为全球的技术创新者、教育工作者、学生及其他用户提供了一个领先的计算平台。最新版本 Mathematica 12.3.1 增加了多项核心语言、数学计算、可视化和图形处理的新功能。 ... [详细]
author-avatar
一个人的生活啦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有