D3.js

D3.js คือ JavaScript ไลブラรีที่ใช้สำหรับการปฏิบัติต่อข้อมูล HTML:

D3.js ง่ายที่จะใช้:

แนะนำวิธีใช้ D3.js?

คุณหากต้องการใช้ D3.js ในหน้าเว็บเพราะเช่นนี้ โปรดเพิ่มลิงก์ที่นำไปสู่คลังหน่วยงาน:

<script src="//d3js.org/d3.v3.min.js"></script>

นี่คือสคริปต์ที่เลือกองค์ประกอบ body และเพิ่มจุดตามตารางที่มีข้อความ "Hello World!"

d3.select("body").append("p").text("Hello World!");

ลองด้วยตัวเอง

กราฟจุด

ตัวอย่าง

// ตั้งขอบเขต
const xSize = 500;
const ySize = 500;
const margin = 40;
const xMax = xSize - margin*2;
const yMax = ySize - margin*2;
// สร้างจุดสุ่ม
const numPoints = 100;
const data = [];
for (let i = 0; i < numPoints; i++) {
  data.push([Math.random() * xMax, Math.random() * yMax]);
}
// ใส่ SVG วัตถุลงบนหน้า
const svg = d3.select("#myPlot")
  .append("svg")
  .append("g")
  .attr("transform","translate(" + margin + "," + margin + ")");
// X ภาค
const x = d3.scaleLinear()
  .domain([0, 500])
  .range([0, xMax]);
svg.append("g")
  .attr("transform", "translate(0," + yMax + ")")
  .call(d3.axisBottom(x));
// Y ภาค
const y = d3.scaleLinear()
  .domain([0, 500])
  .range([ yMax, 0]);
svg.append("g")
  .call(d3.axisLeft(y));
// 點
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");

ลองด้วยตัวเอง