How to obviate incompatibility of d3.js versions?

If I use d3@7 I have nice labeling but no circle with toltip moves along the line as it supposed by code. A piece of circle is available in the upper left corner.
If I use d3@6 I have absurd labeling and circle and tooltip with strange coordinates in it stick on the middle of the line.
Other versions do not work at all.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatable" content="ie=edge">
<title>Document</title>
</head>
<div id="container"></div>
<body>
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<script type="module">
// Curved line declaration
const data = [{xpoint: 10, ypoint: 0.2},
{xpoint: 4, ypoint: 1},
{xpoint: 3, ypoint: 2},
{xpoint: 2, ypoint: 3},
{xpoint: 1, ypoint: 10}];
// Declare the chart dimensions and margins.
const width = 640;
const height = 400;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 30;
const marginLeft = 40;
// Declare the x (horizontal position) scale.
const x = d3.scaleLog()
.domain([0.2, 60])
.range([marginLeft, width - marginRight]);
// Declare the y (vertical position) scale.
const y = d3.scaleLog()
.domain([0.1, 100])
.range([height - marginBottom, marginTop]);
// Declare the line generator.
const line = d3.line()
.x((d) => x(d.xpoint))
.y((d) => y(d.ypoint))
.curve(d3.curveCatmullRom.alpha(0.5));
// Create the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
// Add the x-axis.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x));
// Add the y-axis.
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y));
// Append a path for the line.
svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", line(data));
// This allows to find the closest X index of the mouse:
const bisect = d3.bisector(function(d) { return d.xpoint; }).left;
// Create the circle that travels along the curve of chart
const focus = svg
.append('g')
.append('circle')
.style("fill", "none")
.attr("stroke", "black")
.attr('r', 8.5)
.style("opacity", 0)
// Create the text that travels along the curve of chart
const focusText = svg
.append('g')
.append('text')
.style("opacity", 0)
.attr("text-anchor", "left")
.attr("alignment-baseline", "middle")
// Create a rect on top of the svg area: this rectangle recovers mouse position
svg.append('rect')
.style("fill", "none")
.style("pointer-events", "all")
.attr('width', width)
.attr('height', height)
.on('mouseover', mouseover)
.on('mousemove', mousemove)
.on('mouseout', mouseout);
// What happens when the mouse move -> show the annotations at the right positions.
function mouseover() {
focus.style("opacity", 1)
focusText.style("opacity",1)
};
function mousemove() {
// recover coordinate we need
const x0 = x.invert(d3.pointer(this)[0]);
// console.log(x0)
const i = bisect(data, x0, 1);
//console.log(i)
const selectedData = data[i]
focus
.attr("cx", x(selectedData.xpoint))
.attr("cy", y(selectedData.ypoint))
focusText
.html("x:" + selectedData.xpoint + " - " + "y:" + selectedData.ypoint)
.attr("x", x(selectedData.xpoint)+15)
.attr("y", y(selectedData.ypoint))
};
function mouseout() {
focus.style("opacity", 0)
focusText.style("opacity", 0)
};
// Append the SVG element.
container.append(svg.node());
</script>
</body>
</html>
</code>
<code><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatable" content="ie=edge"> <title>Document</title> </head> <div id="container"></div> <body> <script src="https://cdn.jsdelivr.net/npm/d3@7"></script> <script type="module"> // Curved line declaration const data = [{xpoint: 10, ypoint: 0.2}, {xpoint: 4, ypoint: 1}, {xpoint: 3, ypoint: 2}, {xpoint: 2, ypoint: 3}, {xpoint: 1, ypoint: 10}]; // Declare the chart dimensions and margins. const width = 640; const height = 400; const marginTop = 20; const marginRight = 20; const marginBottom = 30; const marginLeft = 40; // Declare the x (horizontal position) scale. const x = d3.scaleLog() .domain([0.2, 60]) .range([marginLeft, width - marginRight]); // Declare the y (vertical position) scale. const y = d3.scaleLog() .domain([0.1, 100]) .range([height - marginBottom, marginTop]); // Declare the line generator. const line = d3.line() .x((d) => x(d.xpoint)) .y((d) => y(d.ypoint)) .curve(d3.curveCatmullRom.alpha(0.5)); // Create the SVG container. const svg = d3.create("svg") .attr("width", width) .attr("height", height); // Add the x-axis. svg.append("g") .attr("transform", `translate(0,${height - marginBottom})`) .call(d3.axisBottom(x)); // Add the y-axis. svg.append("g") .attr("transform", `translate(${marginLeft},0)`) .call(d3.axisLeft(y)); // Append a path for the line. svg.append("path") .attr("fill", "none") .attr("stroke", "steelblue") .attr("stroke-width", 1.5) .attr("d", line(data)); // This allows to find the closest X index of the mouse: const bisect = d3.bisector(function(d) { return d.xpoint; }).left; // Create the circle that travels along the curve of chart const focus = svg .append('g') .append('circle') .style("fill", "none") .attr("stroke", "black") .attr('r', 8.5) .style("opacity", 0) // Create the text that travels along the curve of chart const focusText = svg .append('g') .append('text') .style("opacity", 0) .attr("text-anchor", "left") .attr("alignment-baseline", "middle") // Create a rect on top of the svg area: this rectangle recovers mouse position svg.append('rect') .style("fill", "none") .style("pointer-events", "all") .attr('width', width) .attr('height', height) .on('mouseover', mouseover) .on('mousemove', mousemove) .on('mouseout', mouseout); // What happens when the mouse move -> show the annotations at the right positions. function mouseover() { focus.style("opacity", 1) focusText.style("opacity",1) }; function mousemove() { // recover coordinate we need const x0 = x.invert(d3.pointer(this)[0]); // console.log(x0) const i = bisect(data, x0, 1); //console.log(i) const selectedData = data[i] focus .attr("cx", x(selectedData.xpoint)) .attr("cy", y(selectedData.ypoint)) focusText .html("x:" + selectedData.xpoint + " - " + "y:" + selectedData.ypoint) .attr("x", x(selectedData.xpoint)+15) .attr("y", y(selectedData.ypoint)) }; function mouseout() { focus.style("opacity", 0) focusText.style("opacity", 0) }; // Append the SVG element. container.append(svg.node()); </script> </body> </html> </code>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatable" content="ie=edge">
    <title>Document</title>
  </head>
  <div id="container"></div>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
    <script type="module">
      // Curved line declaration
      const data = [{xpoint: 10, ypoint: 0.2},
            {xpoint: 4, ypoint: 1},
            {xpoint: 3, ypoint: 2},
            {xpoint: 2, ypoint: 3},
            {xpoint: 1, ypoint: 10}];
// Declare the chart dimensions and margins.
const width = 640;
const height = 400;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 30;
const marginLeft = 40;

// Declare the x (horizontal position) scale.
      const x = d3.scaleLog()
        .domain([0.2, 60])
    .range([marginLeft, width - marginRight]);

// Declare the y (vertical position) scale.
const y = d3.scaleLog()
    .domain([0.1, 100])
    .range([height - marginBottom, marginTop]);

  // Declare the line generator.
      const line = d3.line()
    .x((d) => x(d.xpoint))
    .y((d) => y(d.ypoint))
        .curve(d3.curveCatmullRom.alpha(0.5));
      
// Create the SVG container.
const svg = d3.create("svg")
     .attr("width", width)
      .attr("height", height);
      

// Add the x-axis.
svg.append("g")
    .attr("transform", `translate(0,${height - marginBottom})`)
    .call(d3.axisBottom(x));

// Add the y-axis.
svg.append("g")
    .attr("transform", `translate(${marginLeft},0)`)
    .call(d3.axisLeft(y));

  // Append a path for the line.
  svg.append("path")
    .attr("fill", "none")
      .attr("stroke", "steelblue")
      .attr("stroke-width", 1.5)
    .attr("d", line(data));

 // This allows to find the closest X index of the mouse:
  const bisect = d3.bisector(function(d) { return d.xpoint; }).left;

  // Create the circle that travels along the curve of chart
  const focus = svg
    .append('g')
    .append('circle')
      .style("fill", "none")
      .attr("stroke", "black")
      .attr('r', 8.5)
      .style("opacity", 0)

 // Create the text that travels along the curve of chart
  const focusText = svg
    .append('g')
    .append('text')
      .style("opacity", 0)
      .attr("text-anchor", "left")
      .attr("alignment-baseline", "middle")

 // Create a rect on top of the svg area: this rectangle recovers mouse position
  svg.append('rect')
    .style("fill", "none")
    .style("pointer-events", "all")
    .attr('width', width)
    .attr('height', height)
    .on('mouseover', mouseover)
    .on('mousemove', mousemove)
    .on('mouseout', mouseout);
      
// What happens when the mouse move -> show the annotations at the right positions.
  function mouseover() {
      focus.style("opacity", 1)
      focusText.style("opacity",1)
  };

function mousemove() {
    // recover coordinate we need
    const x0 = x.invert(d3.pointer(this)[0]);
    // console.log(x0)
    const i = bisect(data, x0, 1);
    //console.log(i)
    const selectedData = data[i]
    focus
      .attr("cx", x(selectedData.xpoint))
      .attr("cy", y(selectedData.ypoint))
    focusText
      .html("x:" + selectedData.xpoint + "  -  " + "y:" + selectedData.ypoint)
      .attr("x", x(selectedData.xpoint)+15)
    .attr("y", y(selectedData.ypoint))
};
      function mouseout() {
    focus.style("opacity", 0)
      focusText.style("opacity", 0)
      };

    
// Append the SVG element.
      container.append(svg.node());

</script>
</body>
</html>

New contributor

Igor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật