我可以毫不费力地抽出spline using d3分.

<html>
<head>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <style>
        .graph-container {
            width: 100%;
            height: 800px;
            position: relative;
        }
    </style>
</head>
<body>
    <div id="graph-container" class="graph-container"></div>
    <script>
        // Define the data points for the spline curve
        const data = [
            { x: 0, y: 80, r: 12 },
            { x: 100, y: 100, r: 40 },
            { x: 200, y: 30, r: 6 },
            { x: 300, y: 50, r: 8 },
            { x: 400, y: 40, r: 10 },
            { x: 500, y: 80, r: 12 },
        ];

        // Set up the SVG container
        const svgWidth = 600;
        const svgHeight = 200;
        const svg = d3.select('#graph-container')  // Select the graph-container div
            .append('svg')
            .attr('width', svgWidth)
            .attr('height', svgHeight);

        // Create the spline generator
        const line = d3.line()
            .x((d) => d.x)
            .y((d) => d.y)
//            .curve(d3.curveMonotoneX);
            .curve(d3.curveCardinal);

        // Draw the spline curve
        svg.append('path')
            .datum(data)
            .attr('d', line)
            .attr('fill', 'none')
            .attr('stroke', 'green')
            .attr('stroke-width', (d) => d.r);
    </script>
</body>
</html>

enter image description here

我的问题是用不同的粗细(笔划宽度)绘制每个线段.我希望每条线段都能顺利过渡到下一个控制点.

D3.attr(‘笔划宽度’,(D)=&gt;D.R)似乎不起作用. 我怎么才能在Reaction JS中做到呢?

谢谢,

推荐答案

以下是构建为近似多线的样条线:

const data = [
  { x: 0, y: 80, r: 12 },
  { x: 100, y: 100, r: 30 },
  { x: 200, y: 30, r: 6 },
  { x: 300, y: 50, r: 8 },
  { x: 400, y: 40, r: 10 },
  { x: 500, y: 80, r: 12 },
];

const rValueByX = (x) => {
    if (x === data[0].x) {
    return data[0].r;
  }

  const index = data.findIndex(item => item.x >= x);
  const prevX = data[index - 1].x;
  const nextX = data[index].x;
  const delta = (x - prevX) / (nextX - prevX);
  const prevR = data[index - 1].r;
  const nextR = data[index].r;
  return (nextR - prevR) * delta + prevR;
}

// Set up the SVG container
const svgWidth = 600;
const svgHeight = 150;
const svg = d3.select('#graph-container')
  .append('svg')
  .attr('width', svgWidth)
  .attr('height', svgHeight);

const line = d3.line()
  .x((d) => d.x)
  .y((d) => d.y)
  .curve(d3.curveCardinal);
            
const path = svg.append('path')
  .attr('d', line(data))
  .attr('fill', 'none')
  .attr('stroke', 'green');
  
const total = path.node().getTotalLength();
const step = total / 100;

for (let len = 0; len <= total; len += step) {
  const fromLen = Math.max(0, len - step);
  const toLen = Math.min(len + step, total);
  const point = path.node().getPointAtLength(len);
  const r = rValueByX(point.x);
  const from = path.node().getPointAtLength(fromLen);
  const to = path.node().getPointAtLength(toLen);
  
  svg.append('line')
    .attr('x1', from.x)
    .attr('y1', from.y)
    .attr('x2', to.x)
    .attr('y2', to.y)
    .attr('stroke-width', r)
    .attr('stroke', 'green');
}
<script src="https://d3js.org/d3.v7.min.js"></script>
<div id="graph-container"/>

Reactjs相关问答推荐

如何避免react 使用ESLINTreact 挂钩/穷举-deps进行第一次呈现

无法从正在使用Reaction的Electron 商务store 项目中的购物车中删除项目

如何在react 流中使用文本区域和改变 node 的输入大小?

StyleX中的多语言支持

可以使用mode.css/mode.scss引用常规的类名吗?

透明MOV文件未出现在我的React网页中

获取类别和页面的参数

使用reactrouterdom时显示"对象无效作为React子组件"错误

React中的功能性组件克隆优化

如何修改 react/jsx-no-useless-fragment 规则以允许 <>{children}

为什么我会收到此错误:无法在我的react 包上读取 null 的属性(读取useState)?

单击按钮或文本从一个组件移动到另一个组件

响应式媒体 React Tailwind

vdom 最终更新了 dom(在比较之后)任何 dom 更改实际上应该触发整个树的重绘和回流,那么 vdom 有什么用?

如何禁用来自 React Native API 的某些特定项目的可touch 不透明度

React 中的 Slide API Mui 在开始而不是结束时触发侦听器

如何在props 更改时运行自定义挂钩?

如何在 React 中单击鼠标的位置动态添加 div?

从输入中获取数字时,react 计数器不会增加

设置 Material UI 表格默认排序