简体   繁体   English

d3中的水平分割条形图

[英]horizontal split bar chart in d3

在此处输入图像描述

I would like to create a split difference bar chart using d3 like the attached image.我想使用 d3 创建一个拆分差异条形图,如所附图像。 I have 2 input arrays one for y-axis labels and one for data as below:我有 2 个输入 arrays 一个用于 y 轴标签,一个用于数据,如下所示:

data = [[35,90], [60,45], [80,90], [95,90]]
months = ["Jan - 20", "Feb - 20", "Mar - 20", "Apr -20"]

I tried my hands in d3-observable .我尝试了d3-observable Feel free to fork this observable.I am stuck at getting the nested data rendered properly with scale.随意分叉这个 observable。我坚持让嵌套数据按比例正确渲染。 Any help would be much appreciated.任何帮助将非常感激。

Thanks谢谢

You can indeed avoid SVG and use vanilla HTML to draw a chart like that.您确实可以避免 SVG 并使用香草 HTML 绘制这样的图表。 Of course, this doesn't mean you can't use d3 to make your life easier - it has methods to simplify the handling of scales, date formatting and the creation of elements which isn't in any way restricted to SVG.当然,这并不意味着您不能使用d3来让您的生活更轻松 - 它具有简化比例处理、日期格式和元素创建的方法,这些方法绝不限于 SVG。 For example, you can create the first bar chart and its label like so:例如,您可以像这样创建第一个条形图及其 label:

d3.select("#first-bar-column")
    .selectAll("div")
    .data(data)
    .enter()
    .append("div")
    .style("width", d => `${xScale(+d.A)}px`)
    .classed("first-bar", true)
    .append("p")
    .classed("label", true)
    .text(d => d.A);

Have a look at this fiddle for the full example.看看这个小提琴的完整例子。 If you want any "special effects" to apply to the entire row on hover / click, you'd need to assign some common class based on the element index: .attr("class", (d, i) => `row${i}`)如果您希望任何“特殊效果”应用于 hover / click 上的整行,您需要根据元素索引分配一些常见的 class: .attr("class", (d, i) => `row${i}`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM