javascript - D3 Graph - 将图表插入 div

标签 javascript d3.js

我有一些运行良好的 D3 javascript。如果有帮助的话,这是代码。问题是,它加载代替了 javascript,这意味着我必须将 javascript 放在 HTML 中的正确位置来定位图表。

我想要做的是:

<div class="graph"></div>

并让 javascript 将图表注入(inject)到该 div 中。

任何人都可以帮助我了解如何执行此操作。

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });

// Adds the svg canvas
var svg = d3.select("body")
    .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
    .append("g")
        .attr("transform", 
              "translate(" + margin.left + "," + margin.top + ")");

// Get the data
d3.csv("test3.csv", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.close; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data));

    // Add the X Axis
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);

});

var inter = setInterval(function() {
                updateData();
        }, 5000); 

// ** Update data section (Called from the onclick)
function updateData() {

    // Get the data again
    d3.csv("test3.csv", function(error, data) {
        data.forEach(function(d) {
            d.date = parseDate(d.date);
            d.close = +d.close;
        });

        // Scale the range of the data again 
        x.domain(d3.extent(data, function(d) { return d.date; }));
        y.domain([0, d3.max(data, function(d) { return d.close; })]);

    // Select the section we want to apply our changes to
    var svg = d3.select("body").transition();

    // Make the changes
        svg.select(".line")   // change the line
            .duration(750)
            .attr("d", valueline(data));
        svg.select(".x.axis") // change the x axis
            .duration(750)
            .call(xAxis);
        svg.select(".y.axis") // change the y axis
            .duration(750)
            .call(yAxis);

    });
}

</script>

最佳答案

这一行:

d3.select("body")
  .append("svg")

告诉d3将svg附加到DOM的主体中。尝试:

d3.select(".graph")
  .append("svg")

它会通过类名找到您的 div 并将 svg 附加到其中。

关于javascript - D3 Graph - 将图表插入 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51860294/

相关文章:

javascript - 使用 React 提交时获取选择选项值

javascript - 检查所有 - 仅过滤 Angular 数据表中的数据

javascript - 如何从对象数组的属性返回数组

javascript - d3 的动态图表给出数组中重复元素的问题

javascript - 3d 鱼眼 - 如何在鼠标悬停时使条形变宽?

javascript - 当渲染返回另一个组件时,React 不会调用 componentWillMount

javascript - 如何衡量 promise 的执行时间?

javascript - d3.next.rollup 返回未定义

d3.js - d3 可以在形状内构建形状吗?

javascript - d3.js 中的渲染顺序