javascript - D3.js fiddle 图未显示

标签 javascript d3.js violin-plot

提前抱歉,我对 javascript 很陌生。我正在尝试使用此代码 https://www.d3-graph-gallery.com/graph/violin_basicDens.html用我自己的数据。

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

<script>

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 40},
    width = 1200 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

// Read the data and compute summary statistics for each specie
d3.csv("violinsummary.csv", function(data) {

    // Show the X scale
    var x = d3.scaleBand()
        .range([0, width])
        .domain(["2017-09", "2017-10","2018-02","2018-03"])
        .paddingInner(1)
        .paddingOuter(.5);
    svg.append("g")
        .attr("transform", "translate(0," + height + ")")
        .call(d3.axisBottom(x));

    // Show the Y scale
    var y = d3.scaleLinear()
        .domain([80, 100])
        .range([height, 0]);
    svg.append("g").call(d3.axisLeft(y));

  // Features of density estimate
  var kde = kernelDensityEstimator(kernelEpanechnikov(.2), y.ticks(50));


  // Compute the binning for each group of the dataset
  var sumstat = d3.nest()  // nest function allows to group the calculation per level of a factor
    .key(function(d) {
        return d.DATE;})
    .rollup(function(d) {   // For each key..
      input = d.map(function(g) {
          return g.Power;});
      density = kde(input);   // And compute the binning on it.
      return(density);
    })
    .entries(data);
  console.log(input);
  console.log(density);

  // What is the biggest value that the density estimate reach?
  var maxNum = 0;
  for ( i in sumstat ){
    allBins = sumstat[i].value;
    kdeValues = allBins.map(function(a){return a[1]});
    biggest = d3.max(kdeValues);
    if (biggest > maxNum) { maxNum = biggest }
  }
  console.log(allBins);
  console.log(kdeValues);
  console.log(biggest);


  // The maximum width of a violin must be x.bandwidth = the width dedicated to a group
  var xNum = d3.scaleLinear()
    .range([0, x.bandwidth()])
    .domain([-maxNum,maxNum]);

  console.log(sumstat);

  // Add the shape to this svg!
  svg
    .selectAll("myViolin")
    .data(sumstat)
    .enter()        // So now we are working group per group
    .append("g")
      .attr("transform", function(d){ return("translate(" + x(d.key) +" ,0)") } ) // Translation on the right to be at the group position
    .append("path")
        .datum(function(d){ return(d.value)})     // So now we are working density per density
        .style("stroke", "none")
        .style("fill","#69b3a2")
        .attr("d", d3.area()
            .x0(function(d){ return(xNum(-d[1])) } )
            .x1(function(d){ return(xNum(d[1])) } )
            .y(function(d){ return(y(d[0])) } )
            .curve(d3.curveCatmullRom)    // This makes the line smoother to give the violin appearance. Try d3.curveStep to see the difference
        )

    });



// 2 functions needed for kernel density estimate
function kernelDensityEstimator(kernel, X) {
  return function(V) {
    return X.map(function(x) {
      return [x, d3.mean(V, function(v) { return kernel(x - v); })];
    });
  };
}
function kernelEpanechnikov(k) {
  return function(v) {
    return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0;
  };
}


</script>

我相信将形状添加到 svg 的代码部分是不正确的。我的所有控制台日志输出都显示正确的数据。我还为示例运行了控制台日志输出,并且我的数据和示例的数据始终具有相同的数据类型。

数据(violinsummary.csv):

Power,DATE
89.29,2017-09
89.9,2017-09
91.69,2017-09
89.23,2017-09
91.54,2017-09
88.49,2017-09
89.15,2017-09
90.85,2017-09
89.59,2017-09
93.38,2017-10
92.41,2017-10
90.65,2017-10
91.07,2017-10
90.13,2017-10
91.73,2017-10
91.09,2017-10
93.21,2017-10
91.62,2017-10
89.58,2017-10
90.59,2017-10
92.57,2017-10
89.99,2017-10
90.59,2017-10
88.12,2017-10
91.3,2017-10
89.59,2018-02
91.9,2018-02
87.83,2018-02
90.36,2018-02
91.38,2018-02
91.56,2018-02
91.89,2018-02
90.95,2018-02
90.15,2018-02
90.24,2018-02
94.04,2018-02
85.4,2018-02
88.47,2018-02
92.3,2018-02
92.46,2018-02
92.26,2018-02
88.78,2018-02
90.13,2018-03
89.95,2018-03
92.98,2018-03
91.94,2018-03
90.29,2018-03
91.2,2018-03
94.22,2018-03
90.71,2018-03
93.03,2018-03
91.89,2018-03

最佳答案

x.paddingInner 太大,因此带宽太窄,无法显示 fiddle 。值 1 表示带宽为零。

如果x.paddingInner设置为较低的值,例如0.1,则x刻度的带宽将更宽,因此xNum刻度的范围将更宽并且可以看到 fiddle .

关于javascript - D3.js fiddle 图未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58844029/

相关文章:

r - 仅绘制 fiddle 图的一侧/一半

javascript - vb.net 中的 GeckoWebBrowser

javascript - 在窗口大小调整时重新加载 JS 文件

javascript - 如何通过从文件读取坐标自动画线?

d3.js - Typescript 中未定义的 d3.scale

d3.js - d3.js 中力图链接上的节点

javascript - 我怎样才能 stub 有问题的代码[单元测试]

javascript - 如何从 Ant design 的 Select/Option 组件中获取值

python - seaborn violinplot 中的四分位数线属性

r - fiddle 图错误-提供给连续刻度的离散值