D3.js
- Page précédente Graphes Google JS
- Page suivante Exemples JS
D3.js est une bibliothèque JavaScript pour manipuler des données HTML.
D3.js est facile à utiliser.
Comment utiliser D3.js ?
Pour utiliser D3.js sur une page web, ajoutez un lien vers la bibliothèque :
<script src="//d3js.org/d3.v3.min.js"></script>
Ce script sélectionne l'élément body et ajoute un paragraphe avec le texte "Hello World!":
d3.select("body").append("p").text("Hello World!");
Graphique de dispersion
Instance
// Définissez les dimensions const xSize = 500; const ySize = 500; const margin = 40; const xMax = xSize - margin*2; const yMax = ySize - margin*2; // Créez des points aléatoires const numPoints = 100; const data = []; for (let i = 0; i < numPoints; i++) { data.push([Math.random() * xMax, Math.random() * yMax]); } // Ajoutez l'objet SVG à la page const svg = d3.select("#myPlot") .append("svg") .append("g") .attr("transform","translate(\" + margin + \",\" + margin + \")"); // Axe X const x = d3.scaleLinear() .domain([0, 500]) .range([0, xMax]); svg.append("g") .attr("transform", "translate(0," + yMax + ")") .call(d3.axisBottom(x)); // Axe Y const y = d3.scaleLinear() .domain([0, 500]) .range([ yMax, 0]); svg.append("g") .call(d3.axisLeft(y)); // Point svg.append('g') .selectAll("dot") .data(data).enter() .append("circle") .attr("cx", function (d) { return d[0] } ) .attr("cy", function (d) { return d[1] } ) .attr("r", 3) .style("fill", "Red");
- Page précédente Graphes Google JS
- Page suivante Exemples JS