亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

D3.js 向折線圖添加圓點

D3.js 向折線圖添加圓點

一只名叫tom的貓 2022-06-09 19:36:52
我是 D3.js 的新手,我正在嘗試在每個數據點所在的圖表上添加一個圓點。這是原始數據的示例:TIME,GEO,CITIZEN,GENDER,VALUE2011M01,Germany (until 1990 former territory of the FRG),Belgium,Males,0但在代碼中,我將其格式化為一個名為nestedData 的變量,然后使用該圖繪制兩個線圖UK.values 和Germany.values。如何在兩個折線圖的每一個上添加圓點?謝謝。這是我目前擁有的:<html><head>    <script src="https://d3js.org/d3.v5.js"></script>    <title> D3 Tutorial </title>    <style>        body {            margin: 20px;        }        svg {            padding: 50px;        }        .line {            fill: none;            stroke: black;        }        #UKLine {            fill: none;            stroke: red;        }        #germanyLine {            fill: none;            stroke: blue;        }    </style></head><body><h2>D3 Linegraph</h2><script>    var dataPath = "data/migration.csv";    var width = 800;                    //specifies the width, height and margins of our SVG element    var height = 600;    var margin = 100;    var rowConverter = function (d) {        var timeData = d.TIME;        var datum = timeData.split("M");        return {            date: new Date(datum[0], datum[1] - 1),            geo: d.GEO,            value: d.VALUE        }    };    d3.csv(dataPath, rowConverter)        .then(function (data) {            console.log(data);            // console.table(data);     //loads table in a nice format - just to try it out (probably not super practical for this tutorial)            var nestedData = d3.nest()                .key(function (d) {                    return d.geo;                })                .key(function (d) {                    return d.date;                })                .rollup(function (leaves) {                    return d3.sum(leaves, function (d) {                        return parseInt(d.value);                    });                })
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

您可能需要在創建之后再添加兩個步驟paths



// these two selections are adding paths

svg.append("path")

    .datum(germany.values)

    .attr("class","line")

    .attr("id","germanyLine")

    .attr("d",lineGenerator);


svg.append("path")

    .datum(UK.values)

    .attr("class","line")

    .attr("id", "UKLine")

    .attr("d", lineGenerator);


//you want to add circles


svg.selectAll(".circle-germany")

    .data(germany.values)

    .join("circle") // enter append

      .attr("class", "circle-germany")

      .attr("r", "1") // radius

      .attr("cx", d=> margin + xScale(new Date(d.key)))   // center x passing through your xScale

      .attr("cy", d=> yScale(parseInt(d.value)))   // center y through your yScale



svg.selectAll(".circle-uk")

    .data(UK.values)

    .join("circle") // enter append

      .attr("class", "circle-uk")

      .attr("r", "1") // radius

      .attr("cx", d=> margin + xScale(new Date(d.key)))   // center x passing through your xScale

      .attr("cy", d=> yScale(parseInt(d.value)))   // center y through your yScale


查看完整回答
反對 回復 2022-06-09
  • 1 回答
  • 0 關注
  • 339 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號