【问题标题】:d3 path.line stroke-width with IF statement / ternary operatord3 path.line stroke-width with IF 语句/三元运算符
【发布时间】:2018-11-10 16:19:38
【问题描述】:

尝试使用 IF 语句/三元运算符更改线条笔划宽度,例如if d.country === "China" stroke-width: 2. 这必须是 path.line 属性,所以这是我在调用 line 后附加的。

我已将 countryName 添加到排放对象中,我还注意到条件始终为 FALSE,因此笔画宽度为 0.5。为什么它不是 TRUE?

Codepen

//Define line chart with and height 
const width = fullWidth - margin.left - margin.right;
const height = fullHeight - margin.top - margin.bottom;

//Define x and y scale range
let xScale = d3.scaleLinear()
    .range([0, width])

let yScale = d3.scaleLinear()
    .range([0, height])

//Define x and y axis
let xAxis = d3.axisBottom(xScale)
    .ticks(15)

let yAxis = d3.axisLeft(yScale)
    .ticks(10)


//Draw svg
let svg = d3.select("body")
    .append("svg")
    .attr("width", fullWidth)
    .attr("height", fullHeight)
    .append("g")
    .attr("transform", "translate(" + 53 + "," + 0 +")");

d3.json("https://api.myjson.com/bins/izmg6").then(data => {
    console.log(data);



    //Structure data so should be an array of arrays  etc [[x,y], [x,y], [x,y]]

    let years = d3.keys(data[0]).slice(0, 50);
    console.log(years);

    let dataset = [];

    data.forEach((d, i) => {

        let myEmissions = [];

        years.forEach(y => {
            if (d[y]) {

                myEmissions.push({
                    country: d.countryName,
                    year: y,
                    amount: d[y]
                })
            }
        })

        dataset.push({
            country: d.countryName,
            emissions: myEmissions
        });
    })

    console.log(dataset);

    //Define x and y domain
    xScale
        .domain(d3.extent(years, d =>d))

    yScale
        .domain([d3.max(dataset, d =>
        d3.max(d.emissions, d =>
            +d.amount)), 0])


    //Generate line
    let line = d3.line()
                .curve(d3.curveBasis)  
                .x(d => 
                    xScale(d.year))
                .y(d => 
                    yScale(d.amount));


    let groups = svg.selectAll("g")
        .data(dataset)
        .enter()
        .append("g")


    groups.append("title")
        .text(d => d.country)


    groups.selectAll("path")
        .data(d => [d.emissions])
        .enter()
        .append("path").classed("line", true)
        .attr("d", line)
        .style("stroke-width", d => 
            d.country === "China" ? 10 : 0.5
        )


}).catch(error => console.log(error))

【问题讨论】:

  • 如果您以不同方式创建路径,则无需在排放量中添加国家/地区
  • @rioV8 我是 d3 的新手。能不能说的具体点?
  • 仔细阅读selectAll的作用

标签: javascript arrays object d3.js


【解决方案1】:

rioV8 的意思是你已经有了你的组选择,所以你只需要使用groups 来追加新元素。

groups 是您所有g 的选择,它是您要附加路径的地方。与您不再选择添加标题的方式相同。

groups
    .append("path").classed("line", true)
    .attr("d", d=> line(d.emissions))
    .style("stroke-width", d => 
        d.country === "China" ? 5 : 0.5
    )

【讨论】:

  • 谢谢!但是因为已经有一个数据组,所以它必须是线函数 [d.emissions] 的数组?
  • 因为现在有一个对象有 3 个键和值,而不是两个分别代表 x 和 y 值?
  • 因为您的组let groups = svg.selectAll("g") .data(dataset) .enter() .append("g") 是携带您的数据集的g 的选择,那么当您附加并使用绑定数据时,您的数组{country, emissions },那么您只需要传递给你的线路功能.attr("d", d=> line(d.emissions))
猜你喜欢
  • 1970-01-01
  • 2017-10-20
  • 2018-12-26
  • 1970-01-01
  • 2011-05-10
  • 2018-12-08
  • 2017-11-19
  • 2010-12-03
  • 2021-07-09
相关资源
最近更新 更多