【发布时间】:2020-09-14 12:45:17
【问题描述】:
我想构建一个 D3.js 径向条形图。我见过的所有示例代码都显示了这种图表,其中条形图绘制为“楔形”。是否可以告诉 d3.js 将条形图绘制为直线而不是楔形?
这张图片是我希望条形图出现在图表上的示例
作为一个例子,这里是生成条形图的代码。
const data = [{
"name": "Burj Khalifa",
"height": "350"
},
{
"name": "Shanghai Tower",
"height": "263.34"
},
{
"name": "Abraj Al-Bait Clock Tower",
"height": "254.04"
},
{
"name": "Ping An Finance Centre",
"height": "253.20"
},
{
"name": "Lotte World Tower",
"height": "230.16"
},
{
"name": "Burj Khalifa 2",
"height": "350"
},
{
"name": "Shanghai Tower 2",
"height": "263.34"
},
{
"name": "Abraj Al-Bait Clock Tower 2",
"height": "254.04"
},
{
"name": "Ping An Finance Centre 2",
"height": "253.20"
}
]
data.forEach((d) => {
d.height = Number(d.height)
})
const heightExtent = d3.extent(data, d => d.height)
const bars = data.map(d => d.name)
const containerWidth = 400
const containerHeight = 800
const y = d3.scaleLinear()
.domain([0, heightExtent[1]])
.range([0, containerHeight])
const x = d3.scaleBand()
.domain(bars)
.range([0, containerWidth])
.paddingInner(0.02)
.paddingOuter(0)
const colourScale = d3.scaleOrdinal(d3.schemeBlues[data.length])
const svg = d3.select("#chart-area").append("svg")
.attr("width", containerWidth)
.attr("height", containerHeight)
const buildings = svg.selectAll('rect').data(data)
buildings.enter().append('rect')
.attr('x', (d, i) => x(d.name))
.attr('y', (d, i) => {
return containerHeight - y(d.height)
})
.attr('width', x.bandwidth())
.attr('height', (d, i) => y(d.height))
.attr('fill', (d, i) => colourScale(d.name))
const labels = svg.selectAll('text').data(data)
labels.enter().append('text')
.attr('text-anchor', 'end')
.attr('x', (d, i) => (x(d.name) + (x.bandwidth() / 2)))
.attr('y', (d, i) => containerHeight - 10)
.attr('writing-mode', 'tb')
.attr('fill', 'white')
.text((d, i) => (d.name + ' - ' + d.height + 'm'))
<!-- Bootstrap grid setup -->
<div class="container">
<div class="row">
<div id="chart-area"></div>
</div>
</div>
<!-- External JS libraries -->
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
【问题讨论】:
标签: javascript d3.js charts