亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

d3.js 圖表不考慮邊距

d3.js 圖表不考慮邊距

森林海 2023-10-04 15:31:20
所以我在這里定義邊距:  const svg1 = d3.select(graphElement1.current);  const margin = {top: 0, right: 30, bottom: 10, left: 10};  const width = 650 - margin.left - margin.right;  const height = 100 - margin.top - margin.bottom;在這里繪制圖表    const data = timeseries;    const x = d3.scaleTime()        .domain(d3.extent(data, function(d) {          return new Date(d['date']+'2020');        }))        .range([0, width]);    const y = d3.scaleLinear()        .domain([0, d3.max(data, function(d) {          return +d['totalconfirmed'];        })])        .range([height, 0]);    svg1.append('g')        .attr('transform', 'translate(0,' + height + ')')        .attr('class', 'axis')        .call(d3.axisBottom(x));svg1.append('path')        .datum(data)        .attr('fill', 'none')        .attr('stroke', '#ff073a99')        .attr('stroke-width', 5)        .attr('cursor', 'pointer')        .attr('d', d3.line()            .x(function(d) {              return x(new Date(d['date']+'2020'));            })            .y(function(d) {              return y(d['totalconfirmed'])-5;            })            .curve(d3.curveCardinal),        );    svg1.selectAll('.dot')        .data(data)        .enter()        .append('circle')        .attr('fill', '#ff073a')        .attr('stroke', '#ff073a')        .attr('r', 3)        .attr('cursor', 'pointer')        .attr('cx', function(d) {          return x(new Date(d['date']+'2020'));        })        .attr('cy', function(d) {          return y(d['totalconfirmed'])-5;        })        .on('mouseover', (d, i) => {          d3.select(d3.event.target).attr('r', '5');          setDatapoint(d);          setIndex(i);        })        .on('mouseout', (d) => {          d3.select(d3.event.target).attr('r', '3');        });我已經嘗試了很多 CSS 規則來允許 svg 尊重邊距定義,但我似乎無法使其工作。結果我也無法顯示 x 軸。這就是現在的樣子,右邊距有效,但是,
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

解釋的:

這些邊距將用于插入我們的比例范圍。范圍的起點和終點向內移動相應的邊距,而不是從 0 延伸到圖表的整個寬度和高度。

因此,對于x軸刻度,替換.range([0, width])為:

.range([margin.left,?width])

類似地,對于y軸,請嘗試以下范圍定義:

.range([height,?margin.top])


查看完整回答
反對 回復 2023-10-04
  • 1 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號