简体   繁体   English

D3.js:访问函数的输出

[英]D3.js: accessing a function's output

As a novice in Javascript, I'm having trouble understanding how to access the following function's output: 作为Javascript的新手,我无法理解如何访问以下函数的输出:

function dimensionLabels(d) {
  return __.dimensions[d].title ? __.dimensions[d].title : d;  // dimension display names
}

What I do understand is that when it is passed as a callback function in .text, it successfully returns some text which displays in the browser (eg "Header [n°1]"): 我的理解是,当它作为.text中的回调函数传递时,它成功返回了一些显示在浏览器中的文本(例如“ Header [n°1]”):

    .append("svg:text")
      .attr("text-anchor", "middle")
      .attr('x', 0)
      .attr('y', -10)
      .attr('class', "label")

      .append('svg:tspan')
      .attr('x', 0)
      .attr('dy', 0)
      .text(dimensionLabels)

So I'm assuming dimensionLabels returns some string, as if I replace .text(dimensionLabels) with .text("myString"), the original text ("Header [n°1]") is successfully replaced with "myString". 因此,我假设DimensionLabels返回了一些字符串,就好像我将.text(dimensionLabels)替换为.text(“ myString”)一样,原始文本(“ Header [n°1]”)也已成功地替换为“ myString”。

However, what I'd need to do is access the text from dimensionLabels and process it eg using: 但是,我需要做的是从维度标签访问文本并进行处理,例如使用:

.text(dimensionLabels.split('[', 2)) 

But this produces the following error: 但这会产生以下错误:

TypeError: dimensionLabels.split is not a function TypeError:DimensionLabels.split不是函数

Could someone kindly describe how the function above works, and what I should do to access its output? 有人可以描述上面的功能如何工作,以及我应该如何访问其输出?

dimensionLabels looks like it is a function, not a string, that takes a string as an argument. dimensionLabels看起来像是一个函数,而不是字符串,它以字符串作为参数。

Then, it looks like it is looking at the __ object for a property, that is an array, called dimensions , and then looking for an index [d] , which may have a property title to use as its text. 然后,看起来它正在寻找__对象的属性,该属性是一个数组,称为dimensions ,然后寻找索引[d] ,该索引可能具有用作其文本的属性title If not, then it just returns the argument that you gave it d . 如果不是,那么它仅返回您给它d的参数。

So, if you are trying to use it, you need to pass it a string to return either the result of __.dimensions[d].title , or if it is not found, then just the string, perhaps like this dimensionsLabels("someText") . 因此,如果您尝试使用它,则需要向其传递一个字符串以返回__.dimensions[d].title ,或者如果找不到它,则仅返回该字符串,也许像这样的dimensionsLabels("someText") Then, you will get a string back to you and you can call .split() on the result, or on the whole thing: dimensionsLabels("someText").split('[', 2) 然后,您将得到一个字符串,然后可以对结果或整个对象调用.split()dimensionsLabels("someText").split('[', 2)

The way you are doing it is calling .split() on a function, which, for the most part I believe, .split() is for strings. 你正在做的方式是调用.split()上的功能,其中,在大多数情况下,我相信, .split()是用于字符串。

.text takes a function as a parameter and then calls it internally passing it a d (datum) argument. .text将函数用作参数,然后在内部对其进行调用,并将其传递给d (基准)参数。 So, since .text wants a function just create your own anonymous one and wrapper the call to dimensionLabels : 因此,由于.text一个函数,只需创建您自己的匿名函数并包装对dimensionLabels的调用即可:

.text(function(d){
  var string = dimensionLabels(d);
  return string.split('[', 2);
})

Response to Comments 对评论的回应

The d3 .text method can take either a string or a function that returns a string. d3 .text方法可以采用字符串返回字符串的函数。 If you give it a string it sets the text to that string. 如果给它一个字符串,它将文本设置为该字符串。 If you give it a function, that function is not invoked immediately. 如果为它提供一个函数, 则不会立即调用该函数。 Instead, under the hood, d3 invokes it later. 取而代之的是, d3d3调用它。 When it does, it passes in the d argument and this d argument is the datum bound to that piece of the DOM. 如果这样做,它将传入d参数,而此d参数是绑定到该DOM片段的数据。 (At some point your code is data-binding with a .data call). (在某些时候,您的代码是通过.data调用进行数据绑定的)。

You get an error when you call dimensionLabels("myString") because of this line: 由于以下原因,在调用dimensionLabels("myString")时会出现错误:

__.dimensions[d].title

You have some object named __ which has a property of dimensions . 您有一个名为__对象,它具有dimensions属性。 The property is either an array or object which can be indexed into (I don't know because you have provided enough code for me to sure): 该属性可以是可以索引到的数组或对象(我不知道,因为您可以提供足够的代码来确保我确信):

__.dimensions["myString"]

returns undefined . 返回undefined And undefined will never have a property named title . 而且undefined永远不会有一个名为title的属性。 And BAM, you get your error. 和BAM,您会遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM