2201_75814951 2023-09-12 11:06 采纳率: 12.5%
浏览 2

折现生成器绘制不出图像

如下代码,折线无法绘制出来,猜测可能是折线生成器那里的问题,请教大家

    <div id="container"></div> 
    <script>
        const width = 928;
        const height = 500;
        const marginTop = 20;
        const marginRight = 30;
        const marginBottom = 30;
        const marginLeft = 40;

        let data=d3.csv("aapl.csv") .then(function(data) {
        console.log(data);

        // Create the SVG container.
        const svg = d3.select("#container")
            .append("svg")
            .attr("width", width)
            .attr("height", height)
            .attr("viewBox", [0, 0, width, height])
            .attr("style", "max-width: 100%; height: auto; height: intrinsic;");

        const x = d3.scaleUtc([new Date('2007-04-23' ),new Date('2012-05-01')], [marginLeft, width - marginRight]);
        const y = d3.scaleLinear([0, d3.max(data, d => d.close)], [height - marginBottom, marginTop]);

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

        // Add the y-axis, remove the domain line, add grid lines and a label.
        svg.append("g")
            .attr("transform", `translate(${marginLeft},0)`)
            .call(d3.axisLeft(y).ticks(height / 40))
            .call(g => g.select(".domain").remove())
            .call(g => g.selectAll(".tick line").clone()
                .attr("x2", width - marginLeft - marginRight)
                .attr("stroke-opacity", 0.1))
            .call(g => g.append("text")
                .attr("x", -marginLeft)
                .attr("y", 10)
                .attr("fill", "currentColor")
                .attr("text-anchor", "start")
                .text("↑ Daily close ($)"));

        const line = d3.line()
            .x(d => x(d.date))
            .y(d => y(d.close));

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

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-12 14:33
    关注

    【以下回答由 GPT 生成】

    问题分析

    根据提供的代码,可以看到绘制折线图的逻辑。但是在其中有一个问题,即数据获取部分。代码中使用了d3.csv来获取数据,但是在vue.js中,直接在script标签中使用d3.csv是行不通的。所以需要使用其他方式获取数据。

    解决方案

    1. 使用Vue.js的生命周期钩子函数mounted,当页面加载完成后,再获取数据并绘制折线图。
    <script>
    const width = 928;
    const height = 500;
    const marginTop = 20;
    const marginRight = 30;
    const marginBottom = 30;
    const marginLeft = 40;
    
    new Vue({
        el: '#container',
        mounted() {
            // 使用axios或fetch等方式获取数据
            axios.get("aapl.csv").then(response => {
                const data = d3.csvParse(response.data);
    
                console.log(data);
    
                // Create the SVG container.
                const svg = d3.select("#container")
                    .append("svg")
                    .attr("width", width)
                    .attr("height", height)
                    .attr("viewBox", [0, 0, width, height])
                    .attr("style", "max-width: 100%; height: auto; height: intrinsic;");
    
                const x = d3.scaleUtc(['2007-04-23','2012-05-01'], [marginLeft, width - marginRight]);
                const y = d3.scaleLinear([0, d3.max(data, d => d.close)], [height - marginBottom, marginTop]);
    
                // Add the x-axis.
                svg.append("g")
                    .attr("transform", `translate(0,${height - marginBottom})`)
                    .call(d3.axisBottom(x));
            }).catch(error => {
                console.log(error);
            });
        }
    });
    </script>
    
    1. 使用axios或fetch等方式获取数据。确保文件路径正确,并在获取到数据后进行处理并绘制折线图。
    <script>
    const width = 928;
    const height = 500;
    const marginTop = 20;
    const marginRight = 30;
    const marginBottom = 30;
    const marginLeft = 40;
    
    // 使用axios或fetch等方式获取数据
    axios.get("aapl.csv").then(response => {
        const data = d3.csvParse(response.data);
    
        console.log(data);
    
        // Create the SVG container.
        const svg = d3.select("#container")
            .append("svg")
            .attr("width", width)
            .attr("height", height)
            .attr("viewBox", [0, 0, width, height])
            .attr("style", "max-width: 100%; height: auto; height: intrinsic;");
    
        const x = d3.scaleUtc(['2007-04-23','2012-05-01'], [marginLeft, width - marginRight]);
        const y = d3.scaleLinear([0, d3.max(data, d => d.close)], [height - marginBottom, marginTop]);
    
        // Add the x-axis.
        svg.append("g")
            .attr("transform", `translate(0,${height - marginBottom})`)
            .call(d3.axisBottom(x));
    }).catch(error => {
        console.log(error);
    });
    </script>
    

    解决结果

    使用以上解决方案获取数据并绘制折线图,即可在页面中正确显示出折线图。请确保文件路径的正确性,并根据具体情况进行相应的调整。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月12日

悬赏问题

  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443
  • ¥20 Qt Quick Android 项目报错及显示问题
  • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
  • ¥15 oracleBIEE analytics
  • ¥15 H.264选择性加密例程
  • ¥50 windows的SFTP服务器如何能批量同步用户信息?
  • ¥15 centos7.9升级python3.0的问题
  • ¥15 安装CentOS6时卡住