如何在 Y 轴上获取虚线以及如何在 d3 中在图形旁边显示值
How to get a dashed line on Y axis and how to display the value beside the graph in d3
我是 D3 的新手,我编写了以下脚本来显示条形图。但是,我还需要做三件事
1) Get a dashed line on Y-axis
2) Increase spacing between the graphs
3) Display Value beside the end of the bar graph
这是我下面的代码 -
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart div {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
</style>
<div class="chart"></div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
//var data = [4, 8, 15, 16, 23, 42];
var data = [ {
"label" : "You ",
"value" : 60
}, {
"label" : " Recommended",
"value" : 60
}, {
"label" : "Peers",
"value" : 40
} ];
var color = d3.scale.ordinal().range(
[ "#8BA870", "#000000", "#FF8040", "#ff8c00" ]);
var barPad = 10;
// var x = d3.scale.linear().domain([ 0, d3.max(data)]).range([ 0, 420 ]);
var x = d3.scale.linear().domain([0, 1000]).range([0, 1000]);
d3.select(".chart").selectAll("div").data(data).enter().append("div")
.style({stroke: "black", "stroke-width": "2px"})
.style("height", function(d) { return 20+"px"; })
.style("font-size","20px")
.style("width", function(d) {
return (d.value+100) + "px";
}).style("background-color", function(d, i) {
return color(i);
}).text(function(d, i) {
return data[i].label;
});
</script>
你可以做这样的事情:
用于制作 Y 轴
d3.select(".chart").selectAll("div").data(data).enter()
.append("div")
.style("width","1px")
.style("height",height*2 + "px")
.style("padding-top","0.1px")
.style("padding","0.1px")
.style("background-color","black")
用于制作酒吧
.append("div")
.style({stroke: "black", "stroke-width": "2px"})
.style("height", function(d) { return height+"px"; })
.style("font-size","20px")
.style("margin-top","10px")
.style("text-align","left")
.style("width", function(d) {
return (d.value+100) + "px";
}).style("background-color", function(d, i) {
return color(i);
}).text(function(d, i) {
return data[i].label;
})
为了使文本@成为栏的末尾:
.append("p")
.style("margin-top", function(d) {
return -height -5 + "px";
})
.style("margin-left", function(d) {
return (d.value+100) +10 + "px";
})
.style("color","black")
.text(function(d){ return d.value});
工作代码在这里
虚
线的描边宽度
svg.append("path")
.datum(data1)
.attr("class", "line blue")
.attr("stroke-width", "3px")
.attr("d", line1);
相关文章:
- Facebook:当发布期望对象引用时显示打开的图形对话框
- 没有使用Highcharts显示任何图形
- 在同一库存面板(amCharts)中显示不同的图形
- 我可以在HTML5画布中创建图形形状对象吗?以及当用户将鼠标悬停在形状上时如何将光标显示为指针
- 如何解析键盘字符并显示其等效图形
- 使用数据库中的两个变量在jqplot中显示图形
- 高图表错误 #15,图形显示正确
- 细胞景观:加载页面时不显示图形
- 如何用图形填充流星集合并单独显示它们
- JavaScript sigma.js图形未显示在Internet Explorer 8中
- Javascript 滑块未显示和图形提示
- 显示图形
- Cytoscape.js,图形未以正确的设置显示
- 将水平线添加到 d3 图形以错误的值显示
- Kanga建模和显示图形失败
- 显示图形点的平均值,随着用户缩放而动态修正
- AmChart股票图表未显示不同的图形
- 使用flot的图形显示问题
- 使用json_encode进行Google图形显示
- 使图形显示水平使用jqBarGraph