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


推荐阅读
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • FineReport平台数据分析图表显示部分系列接口的应用场景和实现思路
    本文介绍了FineReport平台数据分析图表显示部分系列接口的应用场景和实现思路。当图表系列较多时,用户希望可以自己设置哪些系列显示,哪些系列不显示。通过调用FR.Chart.WebUtils.getChart("chartID").getChartWithIndex(chartIndex).setSeriesVisible()接口,可以获取需要显示的系列图表对象,并在表单中显示这些系列。本文以决策报表为例,详细介绍了实现方法,并给出了示例。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
author-avatar
炫彩十字绣I_775
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有