欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

在面積圖頂部繪制折線圖

錢艷冰2年前8瀏覽0評論

我正在使用D3js v5.12.0構建一個圖表。

我已經做了一個面積圖,在X軸上有變量year,在Y軸上有變量earth_footprint。

數據在這個鏈接里:https://raw . githubusercontent . com/cvrnogueira/CODWorkData/master/database/final _ data _ set . JSON

我希望在面積圖的上面畫一個線形圖。該折線圖應該在X軸上顯示變量year,在Y軸上顯示pop_total。 pop_total是數據中的另一個變量。

但是我不能管理如何,我看到一些教程,如何在條形圖中畫線,但當我適應我的代碼,這是一個面積圖,它不工作。

半鑄鋼?鋼性鑄鐵(Cast Semi-Steel)

#area-chart {
  text-align: center;
  margin-top: 40px;
}

.selection {
  fill: none;
}

超文本標記語言

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="area-chart"></div>
</body>
</html>

射流研究…

var url = "http://raw.githubusercontent.com/cvrnogueira/CODWorkData/master/database/final_data_set.json";


  d3.json(url)
      .then(function(data) {
      
      data =  data.filter(dataPoint => dataPoint.country_code == 'BRA');
      data =  data.filter(element =>  element.hasOwnProperty("earth_footprint"));

const heightValue = 300;
const widthValue = 600;

// Create SVG and padding for the chart
const svg = d3
  .select("#area-chart")
  .append("svg")
  .attr("viewBox", `0 0 ${widthValue} ${heightValue}`)
;

const strokeWidth = 1.5;
const margin = { top: 0, bottom: 20, left: 30, right: 20 };
const chart = svg.append("g").attr("transform", `translate(${margin.left},0)`);
const width = 600 - margin.left - margin.right - (strokeWidth * 2);
const height = 300 - margin.top - margin.bottom;
const grp = chart
  .append("g")
  .attr("transform", `translate(-${margin.left - strokeWidth},-${margin.top})`);

// Create scales
const yScale = d3
  .scaleLinear()
  .range([height, 0])
  .domain([0, d3.max(data, dataPoint => dataPoint.earth_footprint)]);

const xScale = d3
  .scaleLinear()
  .range([0, width])
  .domain(d3.extent(data, dataPoint => dataPoint.year));

const area = d3
  .area()
  .x(dataPoint => xScale(dataPoint.year))
  .y0(height)
  .y1(dataPoint => yScale(dataPoint.earth_footprint));

// Add area
grp
  .append("path")
  .attr("transform", `translate(${margin.left},0)`)
  .datum(data)
  .style("fill", "lightblue")
  .attr("stroke", "steelblue")
  .attr("stroke-linejoin", "round")
  .attr("stroke-linecap", "round")
  .attr("stroke-width", strokeWidth)
  .attr("d", area);

// Add the X Axis
chart
  .append("g")
  .attr("transform", `translate(0,${height})`)
  .call(d3.axisBottom(xScale).ticks(data.length));

// Add the Y Axis
chart
  .append("g")
  .attr("transform", `translate(0, 0)`)
  .call(d3.axisLeft(yScale)); 


chart.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 0 - margin.left)
      .attr("x",0 - (height / 2))
      .attr("dy", "1em")
      .style("text-anchor", "middle")
      .text("Number of Earths");

chart.append("text")             
      .attr("transform",
            "translate(" + (width/2) + " ," + 
                           (height + margin.top + 20) + ")")
      .style("text-anchor", "middle")
      .text("Year");

});

為了顯示該行,您需要一個行生成器:

const line = d3.area()
    .x(dataPoint => xScale(dataPoint.year))
    .y(dataPoint => yScale(dataPoint.pop_total));

但是,您的yScale獲得了earth_footprint的最大值,而pop_total的值將超出范圍。因此,您需要為線發生器準備另一個比例:

const yScale2 = d3.scaleLinear()
    .range([height, 0])
    .domain([0, d3.max(data, dataPoint => dataPoint.pop_total)]);

之后,只需追加路徑:

grp.append("path")
    .attr("d", line);

現在最大的問題是你有兩種不同比例的視覺編碼(區域和線條)。因此,您需要為這條線添加一個軸。我將把那項工作留給你。

以下是生成的代碼:

var url = "https://raw.githubusercontent.com/cvrnogueira/CODWorkData/master/database/final_data_set.json";


d3.json(url)
  .then(function(data) {

    data = data.filter(dataPoint => dataPoint.country_code == 'BRA');
    data = data.filter(element => element.hasOwnProperty("earth_footprint"));

    const heightValue = 300;
    const widthValue = 600;

    // Create SVG and padding for the chart
    const svg = d3
      .select("#area-chart")
      .append("svg")
      .attr("viewBox", `0 0 ${widthValue} ${heightValue}`);

    const strokeWidth = 1.5;
    const margin = {
      top: 0,
      bottom: 20,
      left: 30,
      right: 20
    };
    const chart = svg.append("g").attr("transform", `translate(${margin.left},0)`);
    const width = 600 - margin.left - margin.right - (strokeWidth * 2);
    const height = 300 - margin.top - margin.bottom;
    const grp = chart
      .append("g")
      .attr("transform", `translate(-${margin.left - strokeWidth},-${margin.top})`);

    // Create scales
    const yScale = d3
      .scaleLinear()
      .range([height, 0])
      .domain([0, d3.max(data, dataPoint => dataPoint.earth_footprint)]);

    const yScale2 = d3
      .scaleLinear()
      .range([height, 0])
      .domain([0, d3.max(data, dataPoint => dataPoint.pop_total)]);

    const xScale = d3
      .scaleLinear()
      .range([0, width])
      .domain(d3.extent(data, dataPoint => dataPoint.year));

    const area = d3
      .area()
      .x(dataPoint => xScale(dataPoint.year))
      .y0(height)
      .y1(dataPoint => yScale(dataPoint.earth_footprint));

    const line = d3.area()
      .x(dataPoint => xScale(dataPoint.year))
      .y(dataPoint => yScale2(dataPoint.pop_total));

    // Add area
    grp
      .append("path")
      .attr("transform", `translate(${margin.left},0)`)
      .datum(data)
      .style("fill", "lightblue")
      .attr("stroke", "steelblue")
      .attr("stroke-linejoin", "round")
      .attr("stroke-linecap", "round")
      .attr("stroke-width", strokeWidth)
      .attr("d", area);

    grp
      .append("path")
      .attr("transform", `translate(${margin.left},0)`)
      .datum(data)
      .style("fill", "none")
      .attr("stroke", "red")
      .attr("stroke-linejoin", "round")
      .attr("stroke-linecap", "round")
      .attr("stroke-width", strokeWidth)
      .attr("d", line);

    // Add the X Axis
    chart
      .append("g")
      .attr("transform", `translate(0,${height})`)
      .call(d3.axisBottom(xScale).ticks(data.length));

    // Add the Y Axis
    chart
      .append("g")
      .attr("transform", `translate(0, 0)`)
      .call(d3.axisLeft(yScale));


    chart.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 0 - margin.left)
      .attr("x", 0 - (height / 2))
      .attr("dy", "1em")
      .style("text-anchor", "middle")
      .text("Number of Earths");

    chart.append("text")
      .attr("transform",
        "translate(" + (width / 2) + " ," +
        (height + margin.top + 20) + ")")
      .style("text-anchor", "middle")
      .text("Year");

  });

#area-chart {
  text-align: center;
  margin-top: 40px;
}

.selection {
  fill: none;
}

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <script src="https://d3js.org/d3.v5.min.js"></script>
    <title>JS Bin</title>
  </head>

  <body>
    <div id="area-chart"></div>
  </body>

</html>