Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多线图d3js上的散点图_Javascript_D3.js - Fatal编程技术网

Javascript 多线图d3js上的散点图

Javascript 多线图d3js上的散点图,javascript,d3.js,Javascript,D3.js,我试图实现一个多折线图,每个观察都有散点图 我的代码是: <!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ body { font: 12px Arial;} path { stroke: steelblue; stroke-width: 2; fill: none; } .axis path, .axi

我试图实现一个多折线图,每个观察都有散点图

我的代码是:

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

    body { font: 12px Arial;}

    path {
        stroke: steelblue;
        stroke-width: 2;
        fill: none;
    }

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

.legend {
    font-size: 12px;
    font-weight: bold;
    text-anchor: middle;
}

    </style>
<body>

    <!-- 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: 70, left: 50},
        width = 1000 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

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

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

        // 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 priceline = d3.svg.line()
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(d.price); })
        .interpolate("basis");


       // var circleAttributes = circles
       // .attr("cx", function (d) { return d.cx; })
       // .attr("cy", function (d) { return d.cy; })
      //  .attr("r", function (d) { return d.radius; })
      //  .style("fill", function (d) { return d.color; });

      // Define the points
     // var point = d3.svg
     //  .cx(function(d) { return x(d.date); })
     //  .cy(function(d) { return y(d.price); })
     //  .r(function(d) { return 5;})


        // 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("stocks.csv", function(error, data) {
                     data.forEach(function(d) {
                                  d.date = parseDate(d.date);
                                  d.price = +d.price;
                                  });

                     // 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.price; })]);

                     // Nest the entries by symbol
                     var dataNest = d3.nest()
                     .key(function(d) {return d.symbol;})
                     .entries(data);

                     var color = d3.scale.category20();   // set the colour scale

                     legendSpace = width/dataNest.length; // spacing for the legend

                     // Loop through each symbol / key
                     dataNest.forEach(function(d,i) {

                                      svg.append("path")
                                      .attr("class", "line")
                                      .style("stroke", function() { // Add the colours dynamically
                                             return d.color = color(d.key); })
                                      .attr("id", 'tag'+d.key.replace(/\s+/g, '')) // assign ID
                                      .attr("stroke-width", 2)
                                      //.style("opacity", 0)
                                      .attr("d", priceline(d.values));

                                     // console.log(d.price)
                                     // console.log(d.price.values)


                                      // Add the scatterplot
                                      svg.selectAll("dot")
                                      .data(d.values)
                                      .enter().append("circle")
                                      .attr("r", 1.5)
                                      .attr("cx", function(d) { return x(d.date); })
                                      .attr("cy", function(d) { return y(d.price); });



                                     // svg.enter()
                                     // .append("circle")
                                     // .attr("cy", function(d) { return y(d.price); })
                                     // .attr("cx", function(d) { return x(d.date); })
                                     // .attr("r", 9)
                                     // .attr("opacity", 0.15)

                                      //svg.append("path")
                                      //.attr("class", "circle")
                                      //.attr("cy", points(d.values))
                                      //.attr("opacity", 0.15)

                                      if(i<10){
                                      // Add the Legend
                                      svg.append("text")
                                      .attr("x",(((i+1)*100-80)))  // space legend
                                      .attr("y", height + (margin.bottom/2)+ 5)
                                      .attr("class", "legend")    // style the legend
                                      .style("fill", function() { // Add the colours dynamically
                                             return d.color = color(d.key); })
                                      .on("click", function(){
                                          // Determine if current line is visible 
                                          var active   = d.active ? false : true,
                                          newOpacity = active ? 1 : 0;
                                          // Hide or show the elements based on the ID
                                          d3.select("#tag"+d.key.replace(/\s+/g, ''))
                                          .transition().duration(100) 
                                          .style("opacity", newOpacity); 
                                          // Update whether or not the elements are active
                                          d.active = active;
                                          })  
                                      .text(d.key);
                                      }
                                      else{
                                      svg.append("text")
                                      .attr("x", ((i-9)*100-80))  // space legend
                                      .attr("y", height + (margin.bottom/2)+30)
                                      .attr("class", "legend")    // style the legend
                                      .style("fill", function() { // Add the colours dynamically
                                             return d.color = color(d.key); })
                                      .on("click", function(){
                                          // Determine if current line is visible
                                          var active   = d.active ? false : true,
                                          newOpacity = active ? 0 : 1;
                                          // Hide or show the elements based on the ID
                                          d3.select("#tag"+d.key.replace(/\s+/g, ''))
                                          .transition().duration(100)
                                          .style("opacity", newOpacity);
                                          // Update whether or not the elements are active
                                          d.active = active;
                                          })
                                      .text(d.key);
                                      }
                                      });

                     // 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);

                     });

        </script>
</body>

/*设置CSS*/
正文{font:12px Arial;}
路径{
笔画:钢蓝;
笔画宽度:2;
填充:无;
}
.轴线路径,
.轴线{
填充:无;
笔画:灰色;
笔画宽度:1;
形状渲染:边缘清晰;
}
.传奇{
字体大小:12px;
字体大小:粗体;
文本锚定:中间;
}
//设置画布/图形的尺寸
var margin={顶部:30,右侧:20,底部:70,左侧:50},
宽度=1000-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
//解析日期/时间
var parseDate=d3.time.format(“%b%d%Y”).parse;
//设定范围
var x=d3.time.scale().range([0,width]);
变量y=d3.scale.linear().范围([0,高度]);
//定义轴
var xAxis=d3.svg.axis().scale(x)
.方向(“底部”)。刻度(5);
var yAxis=d3.svg.axis().scale(y)
.方向(“左”)。勾号(5);
//界定界线
var priceline=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.price);})
.插入(“依据”);
//变量circleAttributes=圆
//.attr(“cx”,函数(d){return d.cx;})
//.attr(“cy”,函数(d){返回d.cy;})
//.attr(“r”,函数(d){返回d.radius;})
//.style(“fill”,函数(d){返回d.color;});
//定义点
//var point=d3.svg
//.cx(函数(d){返回x(d.date);})
//.cy(函数(d){返回y(d.price);})
//.r(函数(d){return 5;})
//添加svg画布
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”);
//获取数据
d3.csv(“stocks.csv”),函数(错误,数据){
data.forEach(函数(d){
d、 日期=解析日期(d.date);
d、 价格=+d.价格;
});
//缩放数据的范围
x、 域(d3.extent(数据,函数(d){返回d.date;}));
y、 域([0,d3.max(数据,函数(d){返回d.price;})];
//按符号嵌套条目
var dataNest=d3.nest()
.key(函数(d){返回d.symbol;})
.条目(数据);
var color=d3.scale.category20();//设置颜色比例
legendSpace=width/dataNest.length;//图例的间距
//循环遍历每个符号/键
forEach(函数(d,i){
追加(“路径”)
.attr(“类”、“行”)
.style(“stroke”,function(){//动态添加颜色
返回d.color=color(d.key);})
.attr(“id”,“tag”+d.key.replace(/\s+/g',)//分配id
.attr(“笔划宽度”,2)
//.style(“不透明度”,0)
.attr(“d”,价格线(d.values));
//console.log(d.price)
//console.log(d.price.values)
//添加散点图
svg.selectAll(“点”)
.数据(d.值)
.enter().append(“圆”)
.attr(“r”,1.5)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){返回y(d.price);});
//svg.enter()
//.附加(“圆圈”)
//.attr(“cy”,函数(d){返回y(d.price);})
//.attr(“cx”,函数(d){返回x(d.date);})
//.attr(“r”,9)
//.attr(“不透明度”,0.15)
//追加(“路径”)
//.attr(“类”、“圈”)
//.attr(“cy”,点(d值))
//.attr(“不透明度”,0.15)

如果(我的代码有效,点没有重复。请检查此小提琴,使用您的代码和虚假数据:。我将尝试猜测这里发生了什么:CSV中的球队(法国联赛?)每轮排名从1到20。因此,对于每个日期,您都有(不一样)点从1到20。这就是为什么点看起来是重复的,但它们不是。如果你删除
插值(“基础”)
,这将更加清楚。你是对的,非常感谢,当我删除插值(“基础”)时,它工作得很好,那么我如何链接插值(“基础”)和散点图?尝试使用不同张力的不同插值器: