D3 散点图中的一个点低于预期值

发布于 2025-01-09 08:08:38 字数 3565 浏览 0 评论 0原文

一切都很好,但那个点应该是 1 和 0 之间的 ~1/4。我相信这与比例有关,数据看起来不错,其他每个点看起来都不错。如果我可以提供任何其他信息,请告诉我。谢谢!

这是一些其他细节,因为如果没有更多细节,我将无法发布。

带断点的散点图

let xScale
let yScale

let xAxis
let yAxis

let width = 800
let height = 500
let padding = 40

let svg = d3.select('svg')
let tooltip = d3.select('#tooltip')

let generateScales = () => {
    let years = []
    let keys = Object.keys(crpdata)
    keys.forEach(element => {
        years.push(Number(element));
    });
    let subs = []
    keys.forEach(element => {
        subs.push(crpdata[element]["commoditySubsidies"]);
    });

    xScale = d3.scaleLinear()
                        .domain([d3.min(years) - 1, d3.max(years) + 1])
                        .range([padding, width-padding])

    yScale = d3.scaleLinear()
                        .domain([d3.max(subs) / 1_000_000, d3.min(subs) / 1_000_000])
                        .range([padding, height-padding])

}

let drawCanvas = () => {
    svg.attr('width', width)
    svg.attr('height', height)
}

let drawPoints = () => {
    let years = Object.keys(crpdata)

    let convertedData = []

    years.forEach(year => {
        let data = {"year": year,
         "conservationSubsidies": crpdata[year]["conservationSubsidies"],
         "disasterSubsidies": crpdata[year]["disasterSubsidies"],
         "commoditySubsidies": crpdata[year]["commoditySubsidies"]}
        convertedData.push(data)
    });

    svg.selectAll('circle')
            .data(convertedData)
            .enter()
            .append('circle')
            .attr('class', 'dot')
            .attr('r', '6')
            .attr('data-xvalue', (item) => {
                console.log(item)
                return item["year"]
            })
            .attr('data-yvalue', (item) => {
                return item["commoditySubsidies"]
            })
            .attr('fill', (item) => {
                return 'lightgreen'
            })
            .attr('cx', (item) => {
              return xScale(item['year'])
            })         
            .attr('cy', (item) => {

                return yScale(item["commoditySubsidies"] / 1_000_000)
            })
            .on('mouseover', (item) => {
                tooltip.transition()
                    .style('visibility', 'visible')
                
                tooltip.text(item['year'] + ' - $' + item['commoditySubsidies'])
                tooltip.attr('data-year', item['Year'])
            })
            .on('mouseout', (item) => {
                tooltip.transition()
                    .style('visibility', 'hidden')
            })
}

let generateAxes = () => {

    xAxis = d3.axisBottom(xScale)
                .tickFormat(d3.format('d'))  

    yAxis = d3.axisLeft(yScale)
                .tickFormat(d3.format('d'))
    svg.append('g')
        .call(xAxis)
        .attr('id', 'x-axis')
        .attr('transform', 'translate(0, ' + (height-padding) +')')

    svg.append('g')
        .call(yAxis)
        .attr('id', 'y-axis')
        .attr('transform','translate(' + padding + ', 0)')
}

async function getCRPData(){
    let returnData
    await fetch(url2, {method: "GET"})
    .then(res => res.json())
    .then(data => returnData = data);
    crpdata = await returnData;
}

let crpPromise = getCRPData()

async function createGraph(){
    await crpPromise;
    console.log(crpdata)
    drawCanvas()
    generateScales()
    generateAxes()
    drawPoints()
}

createGraph()

Everything is working great but the one dot that should be ~1/4 between 1 and 0. I believe it has to do with the scale somehow, the data looks good, and every other dot looks good. Let me know if there is any other information I can provide. Thanks!

Here is some other detail as it won't let me post without more.

Scatterplot with broken dot

let xScale
let yScale

let xAxis
let yAxis

let width = 800
let height = 500
let padding = 40

let svg = d3.select('svg')
let tooltip = d3.select('#tooltip')

let generateScales = () => {
    let years = []
    let keys = Object.keys(crpdata)
    keys.forEach(element => {
        years.push(Number(element));
    });
    let subs = []
    keys.forEach(element => {
        subs.push(crpdata[element]["commoditySubsidies"]);
    });

    xScale = d3.scaleLinear()
                        .domain([d3.min(years) - 1, d3.max(years) + 1])
                        .range([padding, width-padding])

    yScale = d3.scaleLinear()
                        .domain([d3.max(subs) / 1_000_000, d3.min(subs) / 1_000_000])
                        .range([padding, height-padding])

}

let drawCanvas = () => {
    svg.attr('width', width)
    svg.attr('height', height)
}

let drawPoints = () => {
    let years = Object.keys(crpdata)

    let convertedData = []

    years.forEach(year => {
        let data = {"year": year,
         "conservationSubsidies": crpdata[year]["conservationSubsidies"],
         "disasterSubsidies": crpdata[year]["disasterSubsidies"],
         "commoditySubsidies": crpdata[year]["commoditySubsidies"]}
        convertedData.push(data)
    });

    svg.selectAll('circle')
            .data(convertedData)
            .enter()
            .append('circle')
            .attr('class', 'dot')
            .attr('r', '6')
            .attr('data-xvalue', (item) => {
                console.log(item)
                return item["year"]
            })
            .attr('data-yvalue', (item) => {
                return item["commoditySubsidies"]
            })
            .attr('fill', (item) => {
                return 'lightgreen'
            })
            .attr('cx', (item) => {
              return xScale(item['year'])
            })         
            .attr('cy', (item) => {

                return yScale(item["commoditySubsidies"] / 1_000_000)
            })
            .on('mouseover', (item) => {
                tooltip.transition()
                    .style('visibility', 'visible')
                
                tooltip.text(item['year'] + ' - 
 + item['commoditySubsidies'])
                tooltip.attr('data-year', item['Year'])
            })
            .on('mouseout', (item) => {
                tooltip.transition()
                    .style('visibility', 'hidden')
            })
}

let generateAxes = () => {

    xAxis = d3.axisBottom(xScale)
                .tickFormat(d3.format('d'))  

    yAxis = d3.axisLeft(yScale)
                .tickFormat(d3.format('d'))
    svg.append('g')
        .call(xAxis)
        .attr('id', 'x-axis')
        .attr('transform', 'translate(0, ' + (height-padding) +')')

    svg.append('g')
        .call(yAxis)
        .attr('id', 'y-axis')
        .attr('transform','translate(' + padding + ', 0)')
}

async function getCRPData(){
    let returnData
    await fetch(url2, {method: "GET"})
    .then(res => res.json())
    .then(data => returnData = data);
    crpdata = await returnData;
}

let crpPromise = getCRPData()

async function createGraph(){
    await crpPromise;
    console.log(crpdata)
    drawCanvas()
    generateScales()
    generateAxes()
    drawPoints()
}

createGraph()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2025-01-16 08:08:38
yScale = d3.scaleLinear()
    .domain([d3.max(subs) / 1_000_000, 0)
    .range([padding, height-padding])

问题是我将最小值设为最低值,即那个点。我通过使比例从 0 开始来纠正它

yScale = d3.scaleLinear()
    .domain([d3.max(subs) / 1_000_000, 0)
    .range([padding, height-padding])

The problem was I was making the min the lowest value, which was that dot. I corrected it by making the scale start at 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文