简体   繁体   English

将D3代码放入函数并调用

[英]Put D3 code into a function and call

Is it possible to put D3 code into a function and then call the function? 是否可以将D3代码放入函数中然后调用该函数?

For example, I am interested in using this histogram code http://bl.ocks.org/3048450 例如,我对使用此直方图代码http://bl.ocks.org/3048450感兴趣

If I put code in a function and call like 如果我将代码放在函数中并调用



function hist(bin, data) {
    //the D3 histogram plotting code
    // Generate an Irwin–Hall distribution of 10 random variables.
    var values = d3.range(1000).map(d3.random.irwinHall(10));

    // A formatter for counts.
    var formatCount = d3.format(",.0f");

    var margin = {top: 10, right: 30, bottom: 30, left: 30},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

    var x = d3.scale.linear()
    .domain([0, 1])
    .range([0, width]);

    // Generate a histogram using twenty uniformly-spaced bins.
    var data = d3.layout.histogram()
    .bins(x.ticks(20))
    (values);

    var y = d3.scale.linear()
    .domain([0, d3.max(data, function(d) { return d.y; })])
    .range([height, 0]);

    var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

    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 + ")");

    var bar = svg.selectAll(".bar")
    .data(data)
    .enter().append("g")
    .attr("class", "bar")
    .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });

     bar.append("rect")
    .attr("x", 1)
    .attr("width", x(data[0].dx) - 1)
    .attr("height", function(d) { return height - y(d.y); });

     bar.append("text")
    .attr("dy", ".75em")
    .attr("y", 6)
    .attr("x", x(data[0].dx) / 2)
    .attr("text-anchor", "middle")
    .text(function(d) { return formatCount(d.y); });

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

}

hist(...); //call the function


it doesn't plot. 它不会绘图。 Why is that? 这是为什么?

I found the cause of the bug. 我找到了错误的原因。 I misspell "function" as "function" 我将“功能”拼写为“功能”

Where are you including and running that code? 您在哪里包含和运行该代码? If you're including and runningit in the <head> element, then when the script executes the browser will not be aware of the <body> element. 如果要在<head>元素中包含并运行它,那么在脚本执行时,浏览器将不会意识到<body>元素。 d3.select("body") will return an empty selection, and therefore there will be nothing to which to append and svg element. d3.select("body")将返回一个空选择,因此将没有附加和svg元素的内容。

Try putting the script within the <body> or use a library like jQuery (ie, $(document).ready() ) to ensure that the document has been loaded before executing your script. 尝试将脚本放入<body>或使用jQuery之类的库(即$(document).ready() )来确保在执行脚本之前已加载文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM