### Create a minimal annotation with d3-annotation Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/custom_annotation.html This example demonstrates the basic setup required to render an annotation, including loading the library and defining the annotation object with coordinates and text. ```html ``` ```javascript ``` -------------------------------- ### Transition with Data Reduction Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/interactivity_transition.html Initial HTML setup for a transition scenario where the second dataset contains fewer entries than the first. ```html ``` -------------------------------- ### Headless UI Component Example Source: https://github.com/holtzy/d3-graph-gallery/blob/master/javascript-frontend-glossary.html A minimal example representing the logic-only approach of headless components. ```javascript const toto = 1 + 1 ``` -------------------------------- ### Initialize D3.js Environment Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/connectedscatter_legend.html Setup code for loading D3.js and creating the container div for the visualization. ```html
``` ```html ``` -------------------------------- ### Generic Code Examples Source: https://github.com/holtzy/d3-graph-gallery/blob/master/d3-function-glossary.html Placeholder examples for various programming concepts. ```javascript const toto = 1 + 1 function(toto){ 1+1} // comment ``` ```javascript const toto = 1 + 1 ``` -------------------------------- ### Implement Brushing and Zooming Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/stackedarea_template.html Setup for clipPath, brush interaction, and the updateChart function to handle zooming behavior. ```javascript // Add a clipPath: everything out of this area won't be drawn. const clip = svg.append("defs").append("svg:clipPath") .attr("id", "clip") .append("svg:rect") .attr("width", width ) .attr("height", height ) .attr("x", 0) .attr("y", 0); // Add brushing const brush = d3.brushX() // Add the brush feature using the d3.brush function .extent( [ [0,0], [width,height] ] ) // initialise the brush area: start at 0,0 and finishes at width,height: it means I select the whole graph area .on("end", updateChart) // Each time the brush selection changes, trigger the 'updateChart' function // Create the scatter variable: where both the circles and the brush take place const areaChart = svg.append('g') .attr("clip-path", "url(#clip)") // Area generator const area = d3.area() .x(function(d) { return x(d.data.year); }) .y0(function(d) { return y(d[0]); }) .y1(function(d) { return y(d[1]); }) // Show the areas areaChart .selectAll("mylayers") .data(stackedData) .join("path") .attr("class", function(d) { return "myArea " + d.key }) .style("fill", function(d) { return color(d.key); }) .attr("d", area) // Add the brushing areaChart .append("g") .attr("class", "brush") .call(brush); let idleTimeout function idled() { idleTimeout = null; } // A function that update the chart for given boundaries function updateChart(event,d) { extent = event.selection // If no selection, back to initial coordinate. Otherwise, update X axis domain if(!extent){ if (!idleTimeout) return idleTimeout = setTimeout(idled, 350); // This allows to wait a little bit x.domain(d3.extent(data, function(d) { return d.year; })) }else{ x.domain([ x.invert(extent[0]), x.invert(extent[1]) ]) areaChart.select(".brush").call(brush.move, null) // This remove the grey brush area as soon as the selection has been done } // Update axis and area position xAxis.transition().duration(1000).call(d3.axisBottom(x).ticks(5)) areaChart .selectAll("path") .transition().duration(1000) .attr("d", area) } ``` -------------------------------- ### Initialize Google Analytics Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/streamgraph_template.html Standard Google Analytics tracking setup for the page. ```javascript window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag("js", new Date()); gtag("config", "UA-79254642-6"); ``` -------------------------------- ### D3 v4 HTML Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/connectionmap_csv.html HTML boilerplate for loading D3 v4 and necessary geo-projection libraries. ```html ``` -------------------------------- ### Generic Javascript Example Source: https://github.com/holtzy/d3-graph-gallery/blob/master/javascript-frontend-glossary.html A placeholder example used across multiple glossary definitions. ```javascript const toto = 1 + 1 function(toto){ 1+1} // comment ``` -------------------------------- ### Initialize Map Environment Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/bubblemap_leaflet_basic.html Setup code for loading D3.js and Leaflet dependencies for different D3 versions. ```html ``` ```html ``` -------------------------------- ### Configure Axes and Labels Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/stackedarea_template.html Setup for X and Y axes including labels and scale definitions. ```javascript .attr("transform", `translate(0, ${height})`) .call(d3.axisBottom(x).ticks(5)) // Add X axis label: svg.append("text") .attr("text-anchor", "end") .attr("x", width) .attr("y", height+40 ) .text("Time (year)"); // Add Y axis label: svg.append("text") .attr("text-anchor", "end") .attr("x", 0) .attr("y", -20 ) .text("# of baby born") .attr("text-anchor", "start") // Add Y axis const y = d3.scaleLinear() .domain([0, 200000]) .range([ height, 0 ]); svg.append("g") .call(d3.axisLeft(y).ticks(5)) ``` -------------------------------- ### Load D3.js v4 and dependencies Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/circular_barplot_animation_start.html Setup for D3.js v4 including the radial scale library. ```html ``` -------------------------------- ### HTML Setup for D3 v4 Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/density2d_hexbin.html Initial HTML structure including the d3-hexbin plugin for D3.js v4. ```html ``` -------------------------------- ### HTML Setup for D3 Wordcloud Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/wordcloud_basic.html Includes the necessary D3.js and d3-cloud libraries and defines the container div for the visualization. ```html ``` -------------------------------- ### Get Closest Integer Source: https://github.com/holtzy/d3-graph-gallery/blob/master/DOC/cheat_sheet.txt Math.floor() returns the largest integer less than or equal to a given number. For example, Math.floor(3.7) results in 3. ```javascript Math.floor ``` -------------------------------- ### Create a basic HTML document Source: https://github.com/holtzy/d3-graph-gallery/blob/master/intro_d3js.html Demonstrates the structure of a simple HTML file with a title, paragraph, and link. ```htmlThis is my first sentence
This is a link to the d3 graph gallery
``` -------------------------------- ### D3.js Ridgeline Plot Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/ridgeline_animation.html Initial setup and library loading for ridgeline plots in d3.js v4 and v6. ```html ``` ```html ``` -------------------------------- ### Ridgeline plot template setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/ridgeline_template.html HTML and script setup for rendering a ridgeline plot using d3.js versions 4 and 6. ```html ``` ```html ``` -------------------------------- ### D3.js Transition Implementation Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/interactivity_transition.html A complete example showing how to initialize SVG circles with a dataset and trigger a transition to a new dataset using a button. ```html ``` -------------------------------- ### D3.js Connected Scatter Plot Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/connectedscatter_tooltip.html Basic HTML and script setup for initializing a D3.js connected scatter plot with hover effects. ```html ``` ```html ``` -------------------------------- ### Initialize Force-Directed Network Graph Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/network_test.html Sets up the SVG container, loads network data, and configures force simulation parameters for nodes and links. ```javascript // set the dimensions and margins of the graph const margin = {top: 10, right: 30, bottom: 30, left: 40}, width = 400 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; // append the svg object to the body of the page const svg = d3.select("#my_dataviz") .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})`); d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/test.json").then( function(data) { // Initialize the links const link = svg .selectAll("line") .data(data.links) .join("line") .style("stroke", "#aaa") // Initialize the nodes const node = svg .selectAll("circle") .data(data.nodes) .join("circle") .attr("r", 20) .style("fill", "#69b3a2") // Let's list the force we wanna apply on the network const simulation = d3.forceSimulation(data.nodes) // Force algorithm is applied to data.nodes .force("link", d3.forceLink() // This force provides links between nodes .id(function(d) { return d.id; }) // This provide the id of a node .links(data.links) // and this the list of links ) .force("charge", d3.forceManyBody().strength(-400)) // This adds repulsion between nodes. Play with the -400 for the repulsion strength .force("center", d3.forceCenter(width / 2, height / 2)) // This force attracts nodes to the center of the svg area .on("tick", ticked); // This function is run at each iteration of the force algorithm, updating the nodes position. function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function (d) { return d.x+6; }) .attr("cy", function(d) { return d.y-6; }); } }); ``` -------------------------------- ### D3.js v6 Bubble Chart Tooltip Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/bubble_tooltip.html Initial HTML and CSS setup for a bubble chart using D3.js v6, including hover effects defined in CSS. ```html ``` -------------------------------- ### Initialize tooltips and versioning Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/bubblemap_basic.html Initializes Bootstrap tooltips and sets the D3 version display. ```javascript $(function () { $('[data-toggle="tooltip"]').tooltip(); }); ``` ```javascript showCodeVersion("v4"); ``` -------------------------------- ### D3.js v4 Bubble Chart Tooltip Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/bubble_tooltip.html Initial HTML and CSS setup for a bubble chart using D3.js v4, including hover effects defined in CSS. ```html ``` -------------------------------- ### HTML Script Tag Source: https://github.com/holtzy/d3-graph-gallery/blob/master/DOC/reference.html A simple script tag example. ```html ``` -------------------------------- ### Get Unique Entries Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/basic_datamanipulation.html Extract unique values from a specific column. ```javascript var allGroup = d3.map(data, function(d){return(d.name)}).keys() ``` -------------------------------- ### Load UI Components with jQuery Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/violin_basicHist.html Initializes newsletter and footer sections and enables tooltips. ```javascript $(function(){ $("#contact").load("../html_chunk/newsletter_section.html"); }); ``` ```javascript $(function(){ $("#myFooter").load("../html_chunk/footer.html"); }); $(function () { $("[data-toggle='tooltip']").tooltip() }) ``` -------------------------------- ### Google Analytics Tracking Setup Source: https://github.com/holtzy/d3-graph-gallery/blob/master/javascript-frontend-glossary.html Initializes Google Analytics and defines a helper function for tracking outbound link clicks. ```javascript window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'UA-79254642-6'); var getOutboundLink = function (url) { gtag('event', 'click', { 'event_category': 'outbound', 'event_label': url, 'transport_type': 'beacon', 'event_callback': function () { document.location = url; } }); } ``` -------------------------------- ### Example Node Coordinate Structure Source: https://github.com/holtzy/d3-graph-gallery/blob/master/d3-function-glossary.html Represents the structure of a node object after coordinate calculation. ```json { x: 0.45, y: 0, children: [ { x: 0.2, y: 1, children: [Array] }, { x: 1 y: 0.8, children: [Array] } ] } ``` -------------------------------- ### Initialize and animate a network graph with D3.js Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/network_animationStart.html Sets up an SVG container, loads network data, and applies a force simulation to position nodes and links. ```javascript // set the dimensions and margins of the graph const margin = {top: 10, right: 30, bottom: 30, left: 40}, width = 400 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; // append the svg object to the body of the page const svg = d3.select("#my_dataviz") .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})`); d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/data_network.json").then(function( data) { // Initialize the links const link = svg .selectAll("line") .data(data.links) .join("line") .style("stroke", "#aaa") // Initialize the nodes const node = svg .selectAll("circle") .data(data.nodes) .join("circle") .attr("r", 20) .style("fill", "#69b3a2") // Let's list the force we wanna apply on the network const simulation = d3.forceSimulation(data.nodes) // Force algorithm is applied to data.nodes .force("link", d3.forceLink() // This force provides links between nodes .id(function(d) { return d.id; }) // This provide the id of a node .links(data.links) // and this the list of links ) .force("charge", d3.forceManyBody().strength(-400)) // This adds repulsion between nodes. Play with the -400 for the repulsion strength .force("center", d3.forceCenter(width / 2, height / 2)) // This force attracts nodes to the center of the svg area .on("tick", ticked); // This function is run at each iteration of the force algorithm, updating the nodes position. function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.source.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function (d) { return d.x+6; }) .attr("cy", function(d) { return d.y-6; }); } }); ``` -------------------------------- ### D3 Shape Helper Source: https://github.com/holtzy/d3-graph-gallery/blob/master/DOC/reference.html Example of using D3 shape helpers to draw elements. ```javascript svg.append("circle") ``` -------------------------------- ### Initialize and render chord diagram with tooltips Source: https://github.com/holtzy/d3-graph-gallery/blob/master/graph/chord_interactive.html JavaScript logic to create the SVG, define the data matrix, compute chord layout, and attach mouseover/mouseleave event listeners for tooltips. ```javascript ```