- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为我的图表添加标签 Waterfall_Chart因此,如果它是正值,它会在矩形的右侧显示值,如果是负值,则会在左侧显示值。
在代码的第 197-198 行,我按以下方式执行:
label.selectAll("tspan")
.attr("x", function(d) {
return x(d.value1) + (d.value0 < d.value1 ? 5 : -25);
});
// 5px space from the right of the rect to (positive) label :
// -38px space from the lefr of the rect to (negative) label;
它适用于小数字,但当我有大数字时,标签与矩形相交。
var margin = {top: 30, right: 200, bottom: 50, left: 100},
width = 900 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
var formatChange = d3.format("+d"),
formatValue = d3.format("d");
var w = width + margin.left + margin.right;
var h = height + margin.top + margin.bottom;
var svg = d3.select("body").append("svg")
.attr("viewBox", "0 0 " + w + " " + h)
.attr("preserveAspectRatio", "xMidYMid meet")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data = [{id:"name1",val1:23,val2:344224},
{id:"name2",val1:26544,val2:13222},
{id:"name3",val1:15433,val2:154324},
{id:"name4",val1:22453,val2:1654437},
{id:"name5",val1:23213,val2:154325},
{id:"name6",val1:254321,val2:22457},
{id:"name7",val1:22344,val2:32353},
{id:"name8",val1:13222,val2:245329}];
//d3.requestCsv("data_WfH.csv", function(error, data) {
var values = d3.keys(data[0]).filter(function(key) {
return key !== "id";});
data.forEach(function(d) {
for (var i = 0; i < values.length; i++) {
d.value = +d[values[i]];
return d;}});
var value1Sum = 0,
value2Sum = 0;
value1Sum = d3.sum(data, function(d){return d[values[0]];});
value2Sum = d3.sum(data, function(d){return d[values[1]];});
data = function (array) {
r = array.map(function (d) {
return { id: d.id, value: d[values[1]] - d[values[0]] };
});
return [{ id: values[0], value: value1Sum }].concat(r, { id: values[1], value: value2Sum });
}(data);
data.reduce(function(v, d) { return d.value1 = (d.value0 = v) + d.value; },0);
/*---------- Setting Up Dynamic Scales ----------*/
var x = d3.scaleLinear()
.domain([d3.min(data,function(d){return d.value0;}), d3.max(data, function(d) { return d.value0; })])
.range([0, width]);
var y = d3.scaleBand()
.domain(data.map (function(d) { return d.id; }))
.range([0, height])
.padding(0.1);
/*---------- Build the Waterfall Diagram's rectangles ----------*/
svg.append("g").selectAll("rect")
.data(data)
.enter().append("rect")
.style("stroke", "gray") // stroke color for all rects
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "green";} // color rect with positive value in green if condition is true (1)
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "red";} // color rect with negative value in red if condition is true (1)
else {return "gray";} // color rect in gray if both previous conditions are false (0)
})
.attr("y", function(d) { return y(d.id); })
.attr("x", function(d) { if (d.value!==value2Sum) {return x(d.value0 < d.value1 ? d.value0 : d.value1); }})
.attr("width", function(d) { return d.value0 < d.value1 ? x(d.value1) - x(d.value0) : x(d.value0) - x(d.value1); })
.attr("height", y.bandwidth())
//при наводці курсора змінює колір ректів крім першого
.on ( "mouseover" , function ( d , i ) {
d3 . select ( this )
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "#84E884";}
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "#FFA8A8";}
else {return "#E8E8E8";} }); })
. on ( "mouseout" , function ( d , i ) {
d3 . select ( this )
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "green";}
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "red";}
else {return "gray";} }); });
/*---------- Fill first rect with gray color ----------*/
d3.select("rect") // choose the first rect
.style("fill", "#A2A2A2") // color it in gray
//при наводці курсора змінює колір першого ректа
. on ( "mouseover" , function ( d , i ) {
d3 . select ( this )
.style("fill", "#E8E8E8")})
. on ( "mouseout" , function ( d , i ) {
d3 . select ( this )
.style("fill", "grey")});
/*---------- First vertical line ----------*/
//var minX =data[0].value;//select first value
svg.append("g")
.append("line")
.style("stroke","gray")
.attr("x1", x(value1Sum))
.attr("y1", 2)
.attr("x2",x(value1Sum))
.attr("y2", height + 50);
/*---------- Last vertical line ----------*/
//var maxX = d3.sum(data, function(d){if (d.value!==value2Sum) {return d.value;}});
svg.append("g")
.append("line")
.style("stroke","gray")
.attr("x1", x(value2Sum))
.attr("y1", 2)
.attr("x2", x(value2Sum))
.attr("y2", height + 50);
/*---------- Count the change ----------*/
var change = Math.round(value2Sum-value1Sum);
svg.append("text") // append text
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.style("stroke", "black")
.style("position","middle")
.attr("x", x(change/2+value1Sum)) // set x position of left side of text
.attr("y", height + 40) // set y position of bottom of text
.text(change);
/*---------- Left Arrow ----------*/
svg.append("image")
.attr("xlink:href", "img/ArrowRight.png")
.attr("x", x(value1Sum)-37)
.attr("y", height+15)
.attr("width", 37)
.attr("height", 37);
/*---------- Right Arrow ----------*/
svg.append("image")
.attr("xlink:href", "img/ArrowLeft.png")
.attr("x", x(value2Sum))
.attr("y", height+15)
.attr("width", 37)
.attr("height", 37);
/*---------- Label the diagram ----------*/
var label = svg.append("g").selectAll("text")
.data(data)
.enter().append("text")
.attr("class", "label")
.attr("y", function(d) { return y(d.id) + y.bandwidth(); });
/*---------- Label the first element ----------*/
svg.append("text")
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.attr("x", x(value1Sum)/2)
.attr("y", 20)
.text(function(d) {return Math.round(value1Sum);});
/*---------- Label the last element ----------*/
svg.append("text")
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.attr("x", x(value2Sum)/2)
.attr("y", height-5)
.text(function(d) {return Math.round(value2Sum);});
/*---------- Label the value from the table ----------*/
label.append("tspan")
.attr("class", "label")
.attr("dy", "-.3em")
.text(function(d) {
if (d.value!==value1Sum & d.value!==value2Sum) {
return formatChange(d.value1 - d.value0); }});
label.selectAll("tspan")
.attr("x", function(d) { return x(d.value1) + (d.value0 < d.value1 ? 5 : -25); }); // 5px space from the right of the rect to (positive) label : -38px space from the lefr of the rect to (negative) label;
svg.append("g") // the following set of svg attributes visualizes Y axis
.attr("class", "axis axis--y")
.attr("transform", "translate(" + x(0) + ",0)")
.call(d3.axisLeft(y).tickSize(2)); // deleted .tickPadding(x(0) + 6) which wrote labels of axis y on the right side of the axis.
//}
//)
;
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: black; /*with fill: none; the axis line is thin, and without 'color'*/
stroke: black;
shape-rendering: crispEdges;
stroke-width: 4px;
}
.axis text {
font-family: sans-serif;
font-size: 12px;
}
.label {
fill: black;
font-size: 12px;
}
.label-change {
font-weight: bold;
}
.label-value {
fill-opacity: 0.8;
}
.label--negative {
}
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//d3js.org/d3.v4.0.0-alpha.9.min.js"></script>
最佳答案
您可以使用 text-anchor
属性来确定文本相对于矩形的显示方式:
text-anchor
设置为start
,它就会显示在右边x
到rect稍微偏左的位置,设置text-anchor
到end
举个例子
label.selectAll("tspan")
.attr("x", function(d) {
var rect = d3.select("#rect-" + d.id); // get the rect
var rectx = parseInt(rect.attr('x')), // x position
rectwidth = parseInt(rect.attr('width')); // width
if (!rectx) return 0;
if (d.value0 < d.value1)
// place the text to the right of the rect
return rectx + rectwidth + 5;
else
// place the text to the left of the rect
return rectx - 5;
})
.attr("text-anchor", function(d) {
return (d.value0 < d.value1) ? "start" : "end";
});
注意我 set an id on the rects to reference them并避免重新计算值
fiddle https://jsfiddle.net/nikoshr/8yv9dzpc/和一个片段
var margin = {top: 30, right: 200, bottom: 50, left: 100},
width = 900 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
var formatChange = d3.format("+d"),
formatValue = d3.format("d");
var w = width + margin.left + margin.right;
var h = height + margin.top + margin.bottom;
var svg = d3.select("body").append("svg")
.attr("viewBox", "0 0 " + w + " " + h)
.attr("preserveAspectRatio", "xMidYMid meet")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data = [{id:"name1",val1:23,val2:344224},
{id:"name2",val1:26544,val2:13222},
{id:"name3",val1:15433,val2:154324},
{id:"name4",val1:22453,val2:1654437},
{id:"name5",val1:23213,val2:154325},
{id:"name6",val1:254321,val2:22457},
{id:"name7",val1:22344,val2:32353},
{id:"name8",val1:13222,val2:245329}];
//d3.requestCsv("data_WfH.csv", function(error, data) {
var values = d3.keys(data[0]).filter(function(key) {
return key !== "id";});
data.forEach(function(d) {
for (var i = 0; i < values.length; i++) {
d.value = +d[values[i]];
return d;}});
var value1Sum = 0,
value2Sum = 0;
value1Sum = d3.sum(data, function(d){return d[values[0]];});
value2Sum = d3.sum(data, function(d){return d[values[1]];});
data = function (array) {
r = array.map(function (d) {
return { id: d.id, value: d[values[1]] - d[values[0]] };
});
return [{ id: values[0], value: value1Sum }].concat(r, { id: values[1], value: value2Sum });
}(data);
data.reduce(function(v, d) { return d.value1 = (d.value0 = v) + d.value; },0);
/*---------- Setting Up Dynamic Scales ----------*/
var x = d3.scaleLinear()
.domain([d3.min(data,function(d){return d.value0;}), d3.max(data, function(d) { return d.value0; })])
.range([0, width]);
var y = d3.scaleBand()
.domain(data.map (function(d) { return d.id; }))
.range([0, height])
.padding(0.1);
/*---------- Build the Waterfall Diagram's rectangles ----------*/
svg.append("g").selectAll("rect")
.data(data)
.enter().append("rect")
.attr("id", function(d) { return "rect-"+d.id; })
.style("stroke", "gray") // stroke color for all rects
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "green";} // color rect with positive value in green if condition is true (1)
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "red";} // color rect with negative value in red if condition is true (1)
else {return "gray";} // color rect in gray if both previous conditions are false (0)
})
.attr("y", function(d) { return y(d.id); })
.attr("x", function(d) { if (d.value!==value2Sum) {return x(d.value0 < d.value1 ? d.value0 : d.value1); }})
.attr("width", function(d) { return d.value0 < d.value1 ? x(d.value1) - x(d.value0) : x(d.value0) - x(d.value1); })
.attr("height", y.bandwidth())
//при наводці курсора змінює колір ректів крім першого
.on ( "mouseover" , function ( d , i ) {
d3 . select ( this )
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "#84E884";}
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "#FFA8A8";}
else {return "#E8E8E8";} }); })
. on ( "mouseout" , function ( d , i ) {
d3 . select ( this )
.style("fill", function(d) {
if (d.value0 < d.value1 & d.value!==value2Sum) {return "green";}
else if (d.value0 > d.value1 & d.value!==value2Sum) {return "red";}
else {return "gray";} }); });
/*---------- Fill first rect with gray color ----------*/
d3.select("rect") // choose the first rect
.style("fill", "#A2A2A2") // color it in gray
//при наводці курсора змінює колір першого ректа
. on ( "mouseover" , function ( d , i ) {
d3 . select ( this )
.style("fill", "#E8E8E8")})
. on ( "mouseout" , function ( d , i ) {
d3 . select ( this )
.style("fill", "grey")});
/*---------- First vertical line ----------*/
//var minX =data[0].value;//select first value
svg.append("g")
.append("line")
.style("stroke","gray")
.attr("x1", x(value1Sum))
.attr("y1", 2)
.attr("x2",x(value1Sum))
.attr("y2", height + 50);
/*---------- Last vertical line ----------*/
//var maxX = d3.sum(data, function(d){if (d.value!==value2Sum) {return d.value;}});
svg.append("g")
.append("line")
.style("stroke","gray")
.attr("x1", x(value2Sum))
.attr("y1", 2)
.attr("x2", x(value2Sum))
.attr("y2", height + 50);
/*---------- Count the change ----------*/
var change = Math.round(value2Sum-value1Sum);
svg.append("text") // append text
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.style("stroke", "black")
.style("position","middle")
.attr("x", x(change/2+value1Sum)) // set x position of left side of text
.attr("y", height + 40) // set y position of bottom of text
.text(change);
/*---------- Left Arrow ----------*/
svg.append("image")
.attr("xlink:href", "img/ArrowRight.png")
.attr("x", x(value1Sum)-37)
.attr("y", height+15)
.attr("width", 37)
.attr("height", 37);
/*---------- Right Arrow ----------*/
svg.append("image")
.attr("xlink:href", "img/ArrowLeft.png")
.attr("x", x(value2Sum))
.attr("y", height+15)
.attr("width", 37)
.attr("height", 37);
/*---------- Label the diagram ----------*/
var label = svg.append("g").selectAll("text")
.data(data)
.enter().append("text")
.attr("class", "label")
.attr("y", function(d) { return y(d.id) + y.bandwidth(); });
/*---------- Label the first element ----------*/
svg.append("text")
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.attr("x", x(value1Sum)/2)
.attr("y", 20)
.text(function(d) {return Math.round(value1Sum);});
/*---------- Label the last element ----------*/
svg.append("text")
.style("fill", "black") // fill the text with the colour black
.style("font-size", "12px") // size of the text
.attr("x", x(value2Sum)/2)
.attr("y", height-5)
.text(function(d) {return Math.round(value2Sum);});
/*---------- Label the value from the table ----------*/
label.append("tspan")
.attr("class", "label")
.attr("dy", "-.3em")
.text(function(d) {
if (d.value!==value1Sum & d.value!==value2Sum) {
return formatChange(d.value1 - d.value0); }});
label.selectAll("tspan")
.attr("x", function(d) {
var rect = d3.select("#rect-" + d.id);
var rectx = parseInt(rect.attr('x')),
rectwidth = parseInt(rect.attr('width'));
if (!rectx) return 0;
if (d.value0 < d.value1)
return rectx + rectwidth + 5;
else
return rectx - 5;
})
.attr("text-anchor", function(d) {
return (d.value0 < d.value1) ? "start" : "end";
});
svg.append("g") // the following set of svg attributes visualizes Y axis
.attr("class", "axis axis--y")
.attr("transform", "translate(" + x(0) + ",0)")
.call(d3.axisLeft(y).tickSize(2)); // deleted .tickPadding(x(0) + 6) which wrote labels of axis y on the right side of the axis.
//}
//)
;
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: black; /*with fill: none; the axis line is thin, and without 'color'*/
stroke: black;
shape-rendering: crispEdges;
stroke-width: 4px;
}
.axis text {
font-family: sans-serif;
font-size: 12px;
}
.label {
fill: black;
font-size: 12px;
}
.label-change {
font-weight: bold;
}
.label-value {
fill-opacity: 0.8;
}
.label--negative {
}
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//d3js.org/d3.v4.0.0-alpha.9.min.js"></script>
关于javascript - svg 矩形不同边上的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353495/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!