热门标签 | 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


推荐阅读
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文深入探讨了Linux系统中网卡绑定(bonding)的七种工作模式。网卡绑定技术通过将多个物理网卡组合成一个逻辑网卡,实现网络冗余、带宽聚合和负载均衡,在生产环境中广泛应用。文章详细介绍了每种模式的特点、适用场景及配置方法。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • 本文将介绍网易NEC CSS框架的规范及其在实际项目中的应用。通过详细解析其分类和命名规则,探讨如何编写高效、可维护的CSS代码,并分享一些实用的学习心得。 ... [详细]
  • 本文详细介绍了在 Windows 2000 系统中启用 TELNET 服务时需要注意的 NTLM 配置问题,帮助用户解决常见的身份验证失败错误。 ... [详细]
  • 本文探讨了如何在iOS开发环境中,特别是在Xcode 6.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社区 版权所有