配置:react+d3

这篇博客展示了如何在React应用中整合D3.js库,利用`useEffect`和`useRef`钩子创建可复用的图表组件。作者首先配置了npm、parcel和react环境,然后通过导入d3库,定义了一个SVG元素的引用。在`useEffect`中,设置了图表的属性,异步加载数据,并绘制了线形图。示例代码详细展示了如何将D3与React组件完美融合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将npm+parcel+react配置完成后

  1. npm i d3
  2. import * as d3 from "d3";

使用React useEffect和useRef

  • import React, { useEffect, useRef } from 'react';

示例代码:

import React, { useEffect, useRef } from 'react';
import * as d3 from "d3"; 
import "../styles/LineChart.css"; 

export default function LineChart() {
   const d3Chart = useRef();
   useEffect(()=>{
       // attr
       var margin = {top: 10, right: 30, bottom: 20, left: 60};
       var width = 700 - margin.right - margin.left, height = 500 - margin.top - margin.bottom;
       var svg = d3.select(d3Chart.current)
               .attr("width", width+margin.right+margin.left)
               .attr("height", height+margin.top+margin.bottom+40)
               .append("g")
               .attr("transform", "translate(" + margin.left + ", " + margin.top + ")");
   		// 异步获取数据并画图
   		d3.csv("https://raw.githubusercontent.com/zih-an/visualization/master/lab1/2.csv")
   	    .then(d => chart_lines(d, svg, margin, width, height));
   },[]);

   return (
       <div>
           <p>This is a D3 and Component test.</p>
           <svg ref={d3Chart}></svg>
       </div>
   );
}

function chart_lines(data, svg, margin, width, height) {
   // 画图
}
D3act 可以创建一个轻量级的 React 组件包装,它将 React 整个生命周期的方法 componentDidMount, componentDidUpdate 和 componentWillUnmount 映射到了 D3 图表的 create, update 和 unmount 方法中。当涉及到数据可视化的时候,D3 就是实际上的标准,React 近期已经成为 go-to 库,用于构建用户界面。 D3React 都是数据中心库,二者是天作之合。D3 专注于数据驱动,而 React 解决了构建大型应用时的数据改变问题。示例代码:import React from "react";import Chart from "d3act";export default class ExampleBubbleChart extends React.Component {     constructor(props) {        super(props);        this.state = {             data: {                 children: [                     { name: "Alaa", value: 1 },                     { name: "Zaid", value: 1 },                     { name: "Kareem", value: 2 },                     { name: "Mahmoud", value: 1 },                     { name: "Tariq", value: 1 },                     { name: "Shareef", value: 1 },                     { name: "Tom", value: 41 },                     { name: "Forest", value: 2 },                     { name: "John", value: 84 },                     { name: "Alex", value: 11 },                     { name: "Donald", value: 7 },                     { name: "Mark", value: 29 },                     { name: "Charles", value: 20 },                     { name: "Quincy", value: 5 },                     { name: "Alvan", value: 1 },                     { name: "Don", value: 32 },                     { name: "Hassan", value: 2 },                     { name: "Jordan", value: 8 },                     { name: "Michael", value: 32 },                     { name: "Steven", value: 5 },                     { name: "Rafael", value: 2 },                     { name: "Rick", value: 12 },                 ]             }         }     }    render () {        return (            <div className="main">                 <Chart                     type={"bubble"}                     diameter={500}                     showTooltips={true}                     data={this.state.data}                 />             </div>         );     } } React.render(React.createElement(ExampleBubbleChart), document.getElementById(&#39;content&#39;))import React from "react";import Chart from "d3act";export default class ExampleBubbleChart extends React.Component {     constructor(props) {        super(props);        this.state = {             data: [                 { xValue: "React", yValue: 2 },                 { xValue: "Relay", yValue: 12 },                 { xValue: "GraphQL", yValue: 5 },                 { xValue: "Radium", yValue: 7 },                 { xValue: "Babel", yValue: 5 },             ]         }     }    render () {        return (            <div className="main">                 <Chart                     type={"bar"}                     width={500}                     height={500}                     margin={{ top: 40, right: 40, bottom: 40, left: 40 }}                     showTooltips={true}                     data={this.state.data}                 />             </div>         );     } } React.render(React.createElement(ExampleBubbleChart), document.getElementById(&#39;content&#39;)); 标签:D3act
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值