【问题标题】:How can I access and change variable in concatenated text of tooltip using D3/javascript?如何使用 D3/javascript 访问和更改工具提示的连接文本中的变量?
【发布时间】:2015-08-18 13:21:53
【问题描述】:

如何设置 D3 工具提示,以便对来自变量(或只是 JSON 子项)的文本片段进行不同的样式设置?

我创建了一个工具提示,当您单击一个节点时,它会从 JSON 文件(开始上下文、单词、结束上下文)中提取文本。我想以不同的方式设置中间元素的样式(例如,使其粗体、更大等)。如果我将 .attr 或 .style 添加到变量中,我会收到“.attr 不是函数”错误。我还是很新,感谢您的任何提示!

这是相关的sn-p:

.on("click", function(d) {
      tooltip.transition()  
        .style('opacity', 1)

      var word = d.word
        .style("font-size", 60 + "px");

      tooltip.html(d.beginContext + " " + word + " " + d.endContext)
        .style('left', 1100 + "px")
        .style('top',  250 + 'px');

JSON 看起来像这样:

{"node": "",
  "children": [
  {
    "name":"cat",
    "location":141470,
    "beginContext":"cried; 'canna we be quick?'\n\nBut speed was not in Mrs. Marston. She came clinging to Edward's arm, very cautiously, like a",
    "word":"cat",
    "endContext":"on ice.\n\nMartha, her stout red arms bare, her blue gingham dress and white apron flying in the wind, was directed to hold on to Mrs"
  },
  {
    "name":"birds",
    "location":143666,
    "beginContext":"heard and blessed again. To Hazel, they seemed so many other Hazels singing because it was a festal day. To Mrs. Marston they were 'noisy",
    "word":"birds",
    "endContext":", and very disturbing.' Martha crotcheted. She was making edging, hundreds of yards of it, for wedding garments. This was all the more creditable"
  }]}

您还可以查看小提琴here:http://jsfiddle.net/westcoast_509/ofcqq8r0/(向右滚动或缩小以查看点击时出现的文本)。

【问题讨论】:

  • 你能更新小提琴,让它真正可以工作吗?
  • 更新了小提琴——它只有一个 https。改为http。现在应该可以正常工作了。

标签: javascript jquery json d3.js


【解决方案1】:

这是一个更新的示例: http://jsfiddle.net/ofcqq8r0/18/

我已经添加了 CSS 类:

.text-large {
     font-size: 80px;
}

并且我已经修改了你做评论div定位的地方的JS代码:

var word = '<span class="text-large">' + d.word + '</span>';

我认为一切都很清楚,但如果您有任何疑问,请询问我。 此外,由于您正在对 JS 的 'left''top'inside 进行硬编码,因此您也应该将其移至 CSS。

此外,您还可以制作更详细和具体的样式,这仅受您的想象力限制。

【讨论】:

  • 我觉得我的问题不是很清楚(我不知道怎么问)。我不希望根据选择对整个工具提示进行不同的样式设置。我只希望这个词的样式与上下文不同。例如当我看到鸟时,我就知道了。 (其中“鸟”是这个词,而“当...”和“我知道”是开始和结束的上下文。这有意义吗?
  • 所以你只需要设置属性'word'的值就可以说它是'Bird'不同?
  • 是的!基本上我想让它看起来比它的上下文更大。我正在寻找一个通用的解决方案(而不是硬编码)b/c 我还有大约十几个这样的可视化要制作,其中一些有数百个节点。感谢您的帮助!
  • 做到了!非常感谢!在我检查你的答案之前,我只是想增加一个跨度秒。再次感谢!
【解决方案2】:

我对 D3 不是很熟悉,但我想你可以这样做:

Javascript:

.on("click", function(d) {
      tooltip.transition()  
        .style('opacity', 1)

      var word = d.word
        .style("font-size", 60 + "px")
        .style(d.extraStyleName, d.extraStyleValue);

      tooltip.html(d.beginContext + " " + word + " " + d.endContext)
        .style('left', 1100 + "px")
        .style('top',  250 + 'px');

然后在你的 JSON 中:

{"node": "",
  "children": [
  {
    "name":"cat",
    "location":141470,
    "beginContext":"cried; 'canna we be quick?'\n\nBut speed was not in Mrs. Marston. She came clinging to Edward's arm, very cautiously, like a",
    "word":"cat",
    "extraStyleName":"color",
    "extraStyleValue":"red",
    "endContext":"on ice.\n\nMartha, her stout red arms bare, her blue gingham dress and white apron flying in the wind, was directed to hold on to Mrs"
  },
  {
    "name":"birds",
    "location":143666,
    "beginContext":"heard and blessed again. To Hazel, they seemed so many other Hazels singing because it was a festal day. To Mrs. Marston they were 'noisy",
    "word":"birds",
    "extraStyleName":"color",
    "extraStyleValue":"green",
    "endContext":", and very disturbing.' Martha crotcheted. She was making edging, hundreds of yards of it, for wedding garments. This was all the more creditable"
  }]}

或者,您可以使用 D3 的 classed method 分配一个或多个 CSS 类名称。

【讨论】:

  • 当我尝试在 var word = d.word 下添加它(或 .attr)时,恐怕 javascript 会给我“.style 不是函数”错误(如上所述) .分类也是如此。我不知道为什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多