簡體   English   中英

使用 D3.js 實現不連續條形圖

[英]Implement discontinuous bar chart with D3.js

我正在嘗試創建一個如下所示的設計。 我想知道我是否可以使用 d3 將 x 軸作為時區並隱藏 y 軸並顯示如下圖所示的時間段。

TIA

[1]

  1. 使用 x 比例來確定矩形的 x 坐標和寬度。
  2. 使用 y 比例來定位矩形的 y 坐標和高度。

 <!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ .bar { fill: steelblue; } </style> <body> <!-- load the d3.js library --> <script src="https://d3js.org/d3.v4.min.js"></script> <script> // set the dimensions and margins of the graph var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set the ranges var x = d3.scaleLinear() .range([0, width]); var y = d3.scaleBand() .range([0, height]) .padding(0.1); // append the svg object to the body of the page // append a 'group' element to 'svg' // moves the 'group' element to the top left margin var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); data = [ {"name" : "Bob", "start" : 0, "end" : 5}, {"name" : "Robin", "start" : 6, "end" : 7}, {"name" : "Anne", "start" : 7, "end" : 9}, {"name" : "Mark", "start" : 9, "end" : 14} ]; x.domain([0, d3.max(data, function(d) { return d.end; })]); y.domain(data.map(function(d) { return d.name; })); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.start); }) .attr("width", function(d) { return x(d.end) - x(d.start) }) .attr("y", function(d, i) { return y(d.name); }) .attr("height", function(d) { return y.bandwidth(); }); svg.append("g") .call(d3.axisTop(x)); svg.append("g") .call(d3.axisLeft(y)); </script> </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM