跳至内容

什么是 D3?

D3(或 D3.js)是一个用于数据可视化的免费开源 JavaScript 库。它基于 Web 标准的低级方法提供了无与伦比的灵活性,可以创作动态的、数据驱动的图形。十多年来,D3 推动了突破性和屡获殊荣的可视化,成为更高级图表库的基础构建模块,并培养了全球数据从业者的充满活力的社区。

D3 “将该领域推向了前所未有的增长、多元化和创造力”,并“改变了新闻机构、网站和个人作品集中数百万数据可视化的创建方式”,信息是美丽 2022 年时间考验奖如是评论。IEEE VIS 2021 年时间考验奖指出,“通过创建一个让 Web 开发人员能够轻松地创作交互式可视化的框架,作者毫无疑问地帮助将数据可视化带入了主流。[D3] 是对本会议的基石贡献,更广泛地说,也是我们领域整体成功的基石。”

D3 由 Mike Bostock 于 2011 年创建。Mike 与斯坦福大学的 Jeff Heer 和 Vadim Ogievetsky 共同撰写了 D3 论文。Jason Davies 从 2011 年到 2013 年对 D3 做出了重大贡献,最值得注意的是对 D3 的地理投影系统的贡献。Philippe Rivière 从 2016 年起一直是 D3 及其文档的主要贡献者。多年来,无数善良的人通过分享代码和想法、通过教学和解答问题,以及通过将人们聚集在一起以促进可视化实践来为 D3 做出了贡献。Mike 和 Philippe 现在在 Observable 上维护 D3 和 Observable Plot

D3 是一个低级工具箱

D3 不是传统意义上的图表库。它没有“图表”的概念。当您使用 D3 可视化数据时,您会组合各种基元。

要制作一个 堆叠面积图,您可能会使用

有很多东西要学习,对吧?但深呼吸——您不必一次学习所有内容。每个部分都可以独立使用,因此您可以在将它们组合在一起之前单独学习它们。D3 不是一个单一的整体,而是一套 30 个独立的库(或“模块”)。我们出于方便而不是必要性将这些模块捆绑在一起,这样您在迭代设计时就可以触手可及。

D3 工具箱里都有些什么?我们建议您浏览文档和示例,以了解哪些与您相关。

/D3/D3/Introduction/D3/Visualization/D3/Animation/D3/Interaction/D3/Data/D3/API index/D3/API index/D3/Introduction/What is D3?/D3/Introduction/What is D3?/D3/Introduction/Getting started/D3/Introduction/Getting started/D3/Visualization/d3-axis/D3/Visualization/d3-axis/D3/Visualization/d3-chord/D3/Visualization/d3-chord/D3/Visualization/d3-color/D3/Visualization/d3-color/D3/Visualization/d3-interpolate/D3/Visualization/d3-interpolate/D3/Visualization/d3-contour/D3/Visualization/d3-contour/D3/Visualization/d3-delaunay/D3/Visualization/d3-delaunay/D3/Visualization/d3-force/D3/Visualization/d3-force/D3/Visualization/d3-geo/D3/Visualization/d3-geo/D3/Visualization/d3-hierarchy/D3/Visualization/d3-hierarchy/D3/Visualization/d3-path/D3/Visualization/d3-path/D3/Visualization/d3-polygon/D3/Visualization/d3-polygon/D3/Visualization/d3-quadtree/D3/Visualization/d3-quadtree/D3/Visualization/d3-scale/D3/Visualization/d3-scale/D3/Visualization/d3-scale-chromatic/D3/Visualization/d3-scale-chromatic/D3/Visualization/d3-selection/D3/Visualization/d3-selection/D3/Visualization/d3-shape/D3/Visualization/d3-shape/D3/Animation/d3-ease/D3/Animation/d3-ease/D3/Animation/d3-timer/D3/Animation/d3-timer/D3/Animation/d3-transition/D3/Animation/d3-transition/D3/Interaction/d3-brush/D3/Interaction/d3-brush/D3/Interaction/d3-dispatch/D3/Interaction/d3-dispatch/D3/Interaction/d3-drag/D3/Interaction/d3-drag/D3/Interaction/d3-zoom/D3/Interaction/d3-zoom/D3/Data/d3-array/D3/Data/d3-array/D3/Data/d3-dsv/D3/Data/d3-dsv/D3/Data/d3-fetch/D3/Data/d3-fetch/D3/Data/d3-format/D3/Data/d3-format/D3/Data/d3-random/D3/Data/d3-random/D3/Data/d3-time/D3/Data/d3-time/D3/Data/d3-time-format/D3/Data/d3-time-format/D3/Visualization/d3-chord/Chords/D3/Visualization/d3-chord/Chords/D3/Visualization/d3-chord/Ribbons/D3/Visualization/d3-chord/Ribbons/D3/Visualization/d3-interpolate/Value interpolation/D3/Visualization/d3-interpolate/Value interpolation/D3/Visualization/d3-interpolate/Color interpolation/D3/Visualization/d3-interpolate/Color interpolation/D3/Visualization/d3-interpolate/Transform interpolation/D3/Visualization/d3-interpolate/Transform interpolation/D3/Visualization/d3-interpolate/Zoom interpolation/D3/Visualization/d3-interpolate/Zoom interpolation/D3/Visualization/d3-contour/Contour polygons/D3/Visualization/d3-contour/Contour polygons/D3/Visualization/d3-contour/Density estimation/D3/Visualization/d3-contour/Density estimation/D3/Visualization/d3-delaunay/Delaunay triangulations/D3/Visualization/d3-delaunay/Delaunay triangulations/D3/Visualization/d3-delaunay/Voronoi diagrams/D3/Visualization/d3-delaunay/Voronoi diagrams/D3/Visualization/d3-force/Force simulations/D3/Visualization/d3-force/Force simulations/D3/Visualization/d3-force/Center force/D3/Visualization/d3-force/Center force/D3/Visualization/d3-force/Collide force/D3/Visualization/d3-force/Collide force/D3/Visualization/d3-force/Link force/D3/Visualization/d3-force/Link force/D3/Visualization/d3-force/Many-body force/D3/Visualization/d3-force/Many-body force/D3/Visualization/d3-force/Position forces/D3/Visualization/d3-force/Position forces/D3/Visualization/d3-geo/Paths/D3/Visualization/d3-geo/Paths/D3/Visualization/d3-geo/Projections/D3/Visualization/d3-geo/Projections/D3/Visualization/d3-geo/Streams/D3/Visualization/d3-geo/Streams/D3/Visualization/d3-geo/Spherical shapes/D3/Visualization/d3-geo/Spherical shapes/D3/Visualization/d3-geo/Spherical math/D3/Visualization/d3-geo/Spherical math/D3/Visualization/d3-hierarchy/Hierarchies/D3/Visualization/d3-hierarchy/Hierarchies/D3/Visualization/d3-hierarchy/Stratify/D3/Visualization/d3-hierarchy/Stratify/D3/Visualization/d3-hierarchy/Tree/D3/Visualization/d3-hierarchy/Tree/D3/Visualization/d3-hierarchy/Cluster/D3/Visualization/d3-hierarchy/Cluster/D3/Visualization/d3-hierarchy/Partition/D3/Visualization/d3-hierarchy/Partition/D3/Visualization/d3-hierarchy/Pack/D3/Visualization/d3-hierarchy/Pack/D3/Visualization/d3-hierarchy/Treemap/D3/Visualization/d3-hierarchy/Treemap/D3/Visualization/d3-scale/Linear scales/D3/Visualization/d3-scale/Linear scales/D3/Visualization/d3-scale/Time scales/D3/Visualization/d3-scale/Time scales/D3/Visualization/d3-scale/Pow scales/D3/Visualization/d3-scale/Pow scales/D3/Visualization/d3-scale/Log scales/D3/Visualization/d3-scale/Log scales/D3/Visualization/d3-scale/Symlog scales/D3/Visualization/d3-scale/Symlog scales/D3/Visualization/d3-scale/Ordinal scales/D3/Visualization/d3-scale/Ordinal scales/D3/Visualization/d3-scale/Band scales/D3/Visualization/d3-scale/Band scales/D3/Visualization/d3-scale/Point scales/D3/Visualization/d3-scale/Point scales/D3/Visualization/d3-scale/Sequential scales/D3/Visualization/d3-scale/Sequential scales/D3/Visualization/d3-scale/Diverging scales/D3/Visualization/d3-scale/Diverging scales/D3/Visualization/d3-scale/Quantile scales/D3/Visualization/d3-scale/Quantile scales/D3/Visualization/d3-scale/Quantize scales/D3/Visualization/d3-scale/Quantize scales/D3/Visualization/d3-scale/Threshold scales/D3/Visualization/d3-scale/Threshold scales/D3/Visualization/d3-scale-chromatic/Categorical schemes/D3/Visualization/d3-scale-chromatic/Categorical schemes/D3/Visualization/d3-scale-chromatic/Cyclical schemes/D3/Visualization/d3-scale-chromatic/Cyclical schemes/D3/Visualization/d3-scale-chromatic/Diverging schemes/D3/Visualization/d3-scale-chromatic/Diverging schemes/D3/Visualization/d3-scale-chromatic/Sequential schemes/D3/Visualization/d3-scale-chromatic/Sequential schemes/D3/Visualization/d3-selection/Selecting elements/D3/Visualization/d3-selection/Selecting elements/D3/Visualization/d3-selection/Modifying elements/D3/Visualization/d3-selection/Modifying elements/D3/Visualization/d3-selection/Joining data/D3/Visualization/d3-selection/Joining data/D3/Visualization/d3-selection/Handling events/D3/Visualization/d3-selection/Handling events/D3/Visualization/d3-selection/Control flow/D3/Visualization/d3-selection/Control flow/D3/Visualization/d3-selection/Local variables/D3/Visualization/d3-selection/Local variables/D3/Visualization/d3-selection/Namespaces/D3/Visualization/d3-selection/Namespaces/D3/Visualization/d3-shape/Arcs/D3/Visualization/d3-shape/Arcs/D3/Visualization/d3-shape/Areas/D3/Visualization/d3-shape/Areas/D3/Visualization/d3-shape/Curves/D3/Visualization/d3-shape/Curves/D3/Visualization/d3-shape/Lines/D3/Visualization/d3-shape/Lines/D3/Visualization/d3-shape/Links/D3/Visualization/d3-shape/Links/D3/Visualization/d3-shape/Pies/D3/Visualization/d3-shape/Pies/D3/Visualization/d3-shape/Stacks/D3/Visualization/d3-shape/Stacks/D3/Visualization/d3-shape/Symbols/D3/Visualization/d3-shape/Symbols/D3/Visualization/d3-shape/Radial areas/D3/Visualization/d3-shape/Radial areas/D3/Visualization/d3-shape/Radial lines/D3/Visualization/d3-shape/Radial lines/D3/Visualization/d3-shape/Radial links/D3/Visualization/d3-shape/Radial links/D3/Animation/d3-transition/Selecting elements/D3/Animation/d3-transition/Selecting elements/D3/Animation/d3-transition/Modifying elements/D3/Animation/d3-transition/Modifying elements/D3/Animation/d3-transition/Timing/D3/Animation/d3-transition/Timing/D3/Animation/d3-transition/Control flow/D3/Animation/d3-transition/Control flow/D3/Data/d3-array/Adding numbers/D3/Data/d3-array/Adding numbers/D3/Data/d3-array/Binning data/D3/Data/d3-array/Binning data/D3/Data/d3-array/Bisecting data/D3/Data/d3-array/Bisecting data/D3/Data/d3-array/Blurring data/D3/Data/d3-array/Blurring data/D3/Data/d3-array/Grouping data/D3/Data/d3-array/Grouping data/D3/Data/d3-array/Interning values/D3/Data/d3-array/Interning values/D3/Data/d3-array/Set operations/D3/Data/d3-array/Set operations/D3/Data/d3-array/Sorting data/D3/Data/d3-array/Sorting data/D3/Data/d3-array/Summarizing data/D3/Data/d3-array/Summarizing data/D3/Data/d3-array/Ticks/D3/Data/d3-array/Ticks/D3/Data/d3-array/Transforming data/D3/Data/d3-array/Transforming data/D3/Visualization/d3-geo/Projections/Azimuthal projections/D3/Visualization/d3-geo/Projections/Azimuthal projections/D3/Visualization/d3-geo/Projections/Conic projections/D3/Visualization/d3-geo/Projections/Conic projections/D3/Visualization/d3-geo/Projections/Cylindrical projections/D3/Visualization/d3-geo/Projections/Cylindrical projectionsAPI index/D3/API indexAPI index/D3/API indexWhat is D3?/D3/Introduction/What is D3?What is D3?/D3/Introduction/What is D3?Getting started/D3/Introduction/Getting startedGetting started/D3/Introduction/Getting startedd3-axis/D3/Visualization/d3-axisd3-axis/D3/Visualization/d3-axisd3-color/D3/Visualization/d3-colord3-color/D3/Visualization/d3-colord3-path/D3/Visualization/d3-pathd3-path/D3/Visualization/d3-pathd3-polygon/D3/Visualization/d3-polygond3-polygon/D3/Visualization/d3-polygond3-quadtree/D3/Visualization/d3-quadtreed3-quadtree/D3/Visualization/d3-quadtreed3-ease/D3/Animation/d3-eased3-ease/D3/Animation/d3-eased3-timer/D3/Animation/d3-timerd3-timer/D3/Animation/d3-timerd3-brush/D3/Interaction/d3-brushd3-brush/D3/Interaction/d3-brushd3-dispatch/D3/Interaction/d3-dispatchd3-dispatch/D3/Interaction/d3-dispatchd3-drag/D3/Interaction/d3-dragd3-drag/D3/Interaction/d3-dragd3-zoom/D3/Interaction/d3-zoomd3-zoom/D3/Interaction/d3-zoomd3-dsv/D3/Data/d3-dsvd3-dsv/D3/Data/d3-dsvd3-fetch/D3/Data/d3-fetchd3-fetch/D3/Data/d3-fetchd3-format/D3/Data/d3-formatd3-format/D3/Data/d3-formatd3-random/D3/Data/d3-randomd3-random/D3/Data/d3-randomd3-time/D3/Data/d3-timed3-time/D3/Data/d3-timed3-time-format/D3/Data/d3-time-formatd3-time-format/D3/Data/d3-time-formatChords/D3/Visualization/d3-chord/ChordsChords/D3/Visualization/d3-chord/ChordsRibbons/D3/Visualization/d3-chord/RibbonsRibbons/D3/Visualization/d3-chord/RibbonsValue interpolation/D3/Visualization/d3-interpolate/Value interpolationValue interpolation/D3/Visualization/d3-interpolate/Value interpolationColor interpolation/D3/Visualization/d3-interpolate/Color interpolationColor interpolation/D3/Visualization/d3-interpolate/Color interpolationTransform interpolation/D3/Visualization/d3-interpolate/Transform interpolationTransform interpolation/D3/Visualization/d3-interpolate/Transform interpolationZoom interpolation/D3/Visualization/d3-interpolate/Zoom interpolationZoom interpolation/D3/Visualization/d3-interpolate/Zoom interpolationContour polygons/D3/Visualization/d3-contour/Contour polygonsContour polygons/D3/Visualization/d3-contour/Contour polygonsDensity estimation/D3/Visualization/d3-contour/Density estimationDensity estimation/D3/Visualization/d3-contour/Density estimationDelaunay triangulations/D3/Visualization/d3-delaunay/Delaunay triangulationsDelaunay triangulations/D3/Visualization/d3-delaunay/Delaunay triangulationsVoronoi diagrams/D3/Visualization/d3-delaunay/Voronoi diagramsVoronoi diagrams/D3/Visualization/d3-delaunay/Voronoi diagramsForce simulations/D3/Visualization/d3-force/Force simulationsForce simulations/D3/Visualization/d3-force/Force simulationsCenter force/D3/Visualization/d3-force/Center forceCenter force/D3/Visualization/d3-force/Center forceCollide force/D3/Visualization/d3-force/Collide forceCollide force/D3/Visualization/d3-force/Collide forceLink force/D3/Visualization/d3-force/Link forceLink force/D3/Visualization/d3-force/Link forceMany-body force/D3/Visualization/d3-force/Many-body forceMany-body force/D3/Visualization/d3-force/Many-body forcePosition forces/D3/Visualization/d3-force/Position forcesPosition forces/D3/Visualization/d3-force/Position forcesPaths/D3/Visualization/d3-geo/PathsPaths/D3/Visualization/d3-geo/PathsStreams/D3/Visualization/d3-geo/StreamsStreams/D3/Visualization/d3-geo/StreamsSpherical shapes/D3/Visualization/d3-geo/Spherical shapesSpherical shapes/D3/Visualization/d3-geo/Spherical shapesSpherical math/D3/Visualization/d3-geo/Spherical mathSpherical math/D3/Visualization/d3-geo/Spherical mathHierarchies/D3/Visualization/d3-hierarchy/HierarchiesHierarchies/D3/Visualization/d3-hierarchy/HierarchiesStratify/D3/Visualization/d3-hierarchy/StratifyStratify/D3/Visualization/d3-hierarchy/StratifyTree/D3/Visualization/d3-hierarchy/TreeTree/D3/Visualization/d3-hierarchy/TreeCluster/D3/Visualization/d3-hierarchy/ClusterCluster/D3/Visualization/d3-hierarchy/ClusterPartition/D3/Visualization/d3-hierarchy/PartitionPartition/D3/Visualization/d3-hierarchy/PartitionPack/D3/Visualization/d3-hierarchy/PackPack/D3/Visualization/d3-hierarchy/PackTreemap/D3/Visualization/d3-hierarchy/TreemapTreemap/D3/Visualization/d3-hierarchy/TreemapLinear scales/D3/Visualization/d3-scale/Linear scalesLinear scales/D3/Visualization/d3-scale/Linear scalesTime scales/D3/Visualization/d3-scale/Time scalesTime scales/D3/Visualization/d3-scale/Time scalesPow scales/D3/Visualization/d3-scale/Pow scalesPow scales/D3/Visualization/d3-scale/Pow scalesLog scales/D3/Visualization/d3-scale/Log scalesLog scales/D3/Visualization/d3-scale/Log scalesSymlog scales/D3/Visualization/d3-scale/Symlog scalesSymlog scales/D3/Visualization/d3-scale/Symlog scalesOrdinal scales/D3/Visualization/d3-scale/Ordinal scalesOrdinal scales/D3/Visualization/d3-scale/Ordinal scalesBand scales/D3/Visualization/d3-scale/Band scalesBand scales/D3/Visualization/d3-scale/Band scalesPoint scales/D3/Visualization/d3-scale/Point scalesPoint scales/D3/Visualization/d3-scale/Point scalesSequential scales/D3/Visualization/d3-scale/Sequential scalesSequential scales/D3/Visualization/d3-scale/Sequential scalesDiverging scales/D3/Visualization/d3-scale/Diverging scalesDiverging scales/D3/Visualization/d3-scale/Diverging scalesQuantile scales/D3/Visualization/d3-scale/Quantile scalesQuantile scales/D3/Visualization/d3-scale/Quantile scalesQuantize scales/D3/Visualization/d3-scale/Quantize scalesQuantize scales/D3/Visualization/d3-scale/Quantize scalesThreshold scales/D3/Visualization/d3-scale/Threshold scalesThreshold scales/D3/Visualization/d3-scale/Threshold scalesCategorical schemes/D3/Visualization/d3-scale-chromatic/Categorical schemesCategorical schemes/D3/Visualization/d3-scale-chromatic/Categorical schemesCyclical schemes/D3/Visualization/d3-scale-chromatic/Cyclical schemesCyclical schemes/D3/Visualization/d3-scale-chromatic/Cyclical schemesDiverging schemes/D3/Visualization/d3-scale-chromatic/Diverging schemesDiverging schemes/D3/Visualization/d3-scale-chromatic/Diverging schemesSequential schemes/D3/Visualization/d3-scale-chromatic/Sequential schemesSequential schemes/D3/Visualization/d3-scale-chromatic/Sequential schemesSelecting elements/D3/Visualization/d3-selection/Selecting elementsSelecting elements/D3/Visualization/d3-selection/Selecting elementsModifying elements/D3/Visualization/d3-selection/Modifying elementsModifying elements/D3/Visualization/d3-selection/Modifying elementsJoining data/D3/Visualization/d3-selection/Joining dataJoining data/D3/Visualization/d3-selection/Joining dataHandling events/D3/Visualization/d3-selection/Handling eventsHandling events/D3/Visualization/d3-selection/Handling eventsControl flow/D3/Visualization/d3-selection/Control flowControl flow/D3/Visualization/d3-selection/Control flowLocal variables/D3/Visualization/d3-selection/Local variablesLocal variables/D3/Visualization/d3-selection/Local variablesNamespaces/D3/Visualization/d3-selection/NamespacesNamespaces/D3/Visualization/d3-selection/NamespacesArcs/D3/Visualization/d3-shape/ArcsArcs/D3/Visualization/d3-shape/ArcsAreas/D3/Visualization/d3-shape/AreasAreas/D3/Visualization/d3-shape/AreasCurves/D3/Visualization/d3-shape/CurvesCurves/D3/Visualization/d3-shape/CurvesLines/D3/Visualization/d3-shape/LinesLines/D3/Visualization/d3-shape/LinesLinks/D3/Visualization/d3-shape/LinksLinks/D3/Visualization/d3-shape/LinksPies/D3/Visualization/d3-shape/PiesPies/D3/Visualization/d3-shape/PiesStacks/D3/Visualization/d3-shape/StacksStacks/D3/Visualization/d3-shape/StacksSymbols/D3/Visualization/d3-shape/SymbolsSymbols/D3/Visualization/d3-shape/SymbolsRadial areas/D3/Visualization/d3-shape/Radial areasRadial areas/D3/Visualization/d3-shape/Radial areasRadial lines/D3/Visualization/d3-shape/Radial linesRadial lines/D3/Visualization/d3-shape/Radial linesRadial links/D3/Visualization/d3-shape/Radial linksRadial links/D3/Visualization/d3-shape/Radial linksSelecting elements/D3/Animation/d3-transition/Selecting elementsSelecting elements/D3/Animation/d3-transition/Selecting elementsModifying elements/D3/Animation/d3-transition/Modifying elementsModifying elements/D3/Animation/d3-transition/Modifying elementsTiming/D3/Animation/d3-transition/TimingTiming/D3/Animation/d3-transition/TimingControl flow/D3/Animation/d3-transition/Control flowControl flow/D3/Animation/d3-transition/Control flowAdding numbers/D3/Data/d3-array/Adding numbersAdding numbers/D3/Data/d3-array/Adding numbersBinning data/D3/Data/d3-array/Binning dataBinning data/D3/Data/d3-array/Binning dataBisecting data/D3/Data/d3-array/Bisecting dataBisecting data/D3/Data/d3-array/Bisecting dataBlurring data/D3/Data/d3-array/Blurring dataBlurring data/D3/Data/d3-array/Blurring dataGrouping data/D3/Data/d3-array/Grouping dataGrouping data/D3/Data/d3-array/Grouping dataInterning values/D3/Data/d3-array/Interning valuesInterning values/D3/Data/d3-array/Interning valuesSet operations/D3/Data/d3-array/Set operationsSet operations/D3/Data/d3-array/Set operationsSorting data/D3/Data/d3-array/Sorting dataSorting data/D3/Data/d3-array/Sorting dataSummarizing data/D3/Data/d3-array/Summarizing dataSummarizing data/D3/Data/d3-array/Summarizing dataTicks/D3/Data/d3-array/TicksTicks/D3/Data/d3-array/TicksTransforming data/D3/Data/d3-array/Transforming dataTransforming data/D3/Data/d3-array/Transforming dataAzimuthal projections/D3/Visualization/d3-geo/Projections/Azimuthal projectionsAzimuthal projections/D3/Visualization/d3-geo/Projections/Azimuthal projectionsConic projections/D3/Visualization/d3-geo/Projections/Conic projectionsConic projections/D3/Visualization/d3-geo/Projections/Conic projectionsCylindrical projections/D3/Visualization/d3-geo/Projections/Cylindrical projectionsCylindrical projections/D3/Visualization/d3-geo/Projections/Cylindrical projectionsD3/D3Introduction/D3/IntroductionVisualization/D3/VisualizationAnimation/D3/AnimationInteraction/D3/InteractionData/D3/Datad3-chord/D3/Visualization/d3-chordd3-chord/D3/Visualization/d3-chordd3-interpolate/D3/Visualization/d3-interpolated3-interpolate/D3/Visualization/d3-interpolated3-contour/D3/Visualization/d3-contourd3-contour/D3/Visualization/d3-contourd3-delaunay/D3/Visualization/d3-delaunayd3-delaunay/D3/Visualization/d3-delaunayd3-force/D3/Visualization/d3-forced3-force/D3/Visualization/d3-forced3-geo/D3/Visualization/d3-geod3-geo/D3/Visualization/d3-geod3-hierarchy/D3/Visualization/d3-hierarchyd3-hierarchy/D3/Visualization/d3-hierarchyd3-scale/D3/Visualization/d3-scaled3-scale/D3/Visualization/d3-scaled3-scale-chromatic/D3/Visualization/d3-scale-chromaticd3-scale-chromatic/D3/Visualization/d3-scale-chromaticd3-selection/D3/Visualization/d3-selectiond3-selection/D3/Visualization/d3-selectiond3-shape/D3/Visualization/d3-shaped3-shape/D3/Visualization/d3-shaped3-transition/D3/Animation/d3-transitiond3-transition/D3/Animation/d3-transitiond3-array/D3/Data/d3-arrayd3-array/D3/Data/d3-arrayProjections/D3/Visualization/d3-geo/ProjectionsProjections/D3/Visualization/d3-geo/Projections

提示

除非您需要 D3 的低级控制,否则我们建议使用我们高级的姐妹库:Observable Plot。在 D3 中,直方图可能需要 50 行代码,而 Plot 只需要一行代码!Plot 简洁但表达力强的 API 使您能够更多地专注于分析和可视化数据,而不是 Web 开发。您甚至可以将 Plot 和 D3 结合使用,以获得两者的优势。

D3 很灵活

由于 D3 没有“图表”的抽象概念,因此即使是最基本的图表也可能需要几十行代码。好处是,所有部分都摆在你面前,你可以完全控制发生的事情。您可以调整可视化效果以精确地实现您想要的效果。D3 没有您数据的默认演示——只有您自己编写的代码。(或从示例中复制。)

将 D3 视为“自己做所有事情”的替代方案,而不是高级图表库的替代方案。如果您对其他工具不满意,并且正在考虑使用 SVG 或 Canvas(甚至 WebGL)滚动自己的图表,那么您不妨浏览一下 D3 的工具箱!这里几乎肯定会有东西可以帮助您构建梦寐以求的图表,而不会影响您的创造力。

D3 与 Web 配合使用

D3 不会引入新的图形表示形式;相反,您可以直接使用 D3 与 Web 标准(例如 SVG 和 Canvas)配合使用。

名称“D3”是数据驱动的文档的缩写,其中文档指的是 文档对象模型 (DOM) 标准,它表示网页的内容。虽然 D3 的一些模块(例如 选择转换)涉及 DOM,但其他模块(包括 刻度形状)只对数据进行操作。D3 也可以与 React、Vue 和 Svelte 等 Web 框架配对;有关建议,请参阅 入门指南

D3 对 Web 标准的采用带来了许多好处。例如,您可以使用外部样式表来更改图表的外观(即使是响应媒体查询,例如响应式图表或深色模式);您可以使用调试器和元素检查器来查看您的代码在做什么;而 D3 的同步、命令式评估模型——立即调用 选择.attr 会修改 DOM——可以使其比具有复杂异步运行时的框架更容易调试。

D3 用于定制可视化

D3 使事情成为可能,但不一定容易;即使是一些应该很容易的事情,通常也不容易。用 Amanda Cox 的话说:“如果您认为为一个条形图编写一百行代码是完全正常的,那么就使用 D3。”

如果您需要为定制可视化提供最大的表现力,则应考虑使用 D3。D3 对像纽约时报The Pudding 等媒体机构很有意义,他们的单幅图形可能被一百万读者看到,并且编辑团队可以共同努力来推进视觉传达的艺术水平。

另一方面,对于拼凑私有仪表板或一次性分析而言,D3 过于复杂。不要被炫目的示例所吸引:它们中的许多都是经过巨大努力才实现的!如果您受到时间的限制(谁没有?),那么您更有可能使用 Observable Plot 制作出更好的可视化或分析结果。

D3 用于动态可视化

D3 最新颖的概念是它的 数据连接:给定一组数据和一组 DOM 元素,数据连接允许您对进入更新退出元素应用单独的操作。如果您只创建静态图表(不会动画化或响应用户输入的图表),您可能会发现这个概念不直观甚至很奇怪,因为它不需要。

数据连接存在是为了让您能够完全控制数据发生变化时会发生什么,并相应地更新显示。这种直接控制允许极其高效的更新——您只触碰需要更改的元素和属性,而不会对 DOM 进行差异化——以及状态之间平滑的动画转换。D3 非常适合动态、交互式可视化。(尝试在 “通往白宫的 512 条道路”(2012 年)中点击选项卡状态切换按钮。真的。)