我如何使用D3/javascript访问和更改工具提示的连接文本中的变量
How can I access and change variable in concatenated text of tooltip using D3/javascript?
如何设置D3工具提示,以便我可以从变量(或只是JSON子)不同的样式文本块?
我创建了一个工具提示,当您单击节点时,它从JSON文件(开始上下文、单词、结束上下文)中提取文本。我想样式中间元素不同(例如,使其粗体,更大,等)。如果我将。attr或。style添加到变量中,我将得到一个。"Attr不是函数"错误。我还是个新手,感谢你给我的任何建议!
下面是相关的代码片段:
.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"
}]}
您也可以在这里查看小提琴:http://jsfiddle.net/westcoast_509/ofcqq8r0/(向右滚动或缩小以查看单击时出现的文本)
下面是一个更新后的例子:http://jsfiddle.net/ofcqq8r0/18/
我已经添加了CSS类:.text-large {
font-size: 80px;
}
我已经修改了JS代码的地方,你做评论div:
定位的地方var word = '<span class="text-large">' + d.word + '</span>';
我想一切都清楚了,但如果你有什么疑问,请问我。此外,由于你在JS中硬编码'left'
和'top'
,你也应该将其移动到CSS中。
此外,你可以制作更详细和特定的样式,它只是受限于你的想象力。
我不是很熟悉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"
}]}
或者你可以分配一个或多个CSS类名,使用D3的classed方法
相关文章:
- d3.js Chord图的动态工具提示
- Graphiti中是否有任何工具提示功能
- "工具提示"jQuery插件坏了
- 单元格的工具提示或标题不显示超过2000个字符
- d3.js用按钮更新条形图工具提示
- 使Intro.js工具提示响应
- 画布中绘制的矩形区域的弹出工具提示
- 使用CSS/JavaScript更改剑道图工具提示文本颜色
- 禁用NVD3 multiBarHorizontalChart中0值的工具提示
- 如何更改已显示的工具提示
- 如何访问高图表工具提示中的任何特定数据
- 如何在jQuery点击函数中添加并显示引导工具提示
- 我可以't使用jQuery更改工具提示引导程序的位置
- 语义UI中格式化的工具提示
- 柱状图上的Highcharts工具提示
- 引导工具提示用法,在搜索字段中
- 引导工具提示并没有连接到被点击的按钮上
- 如何连接jquery工具提示和asp.net文本框
- 我如何使用D3/javascript访问和更改工具提示的连接文本中的变量
- 我不知道如何将我创建的工具提示连接到图像映射