d3

2017-05-04 6 views
0

を使用して小さな複数の視覚化にプロットを追加する方法現在の状況:私はすでにデータのための小さな複数の視覚化を持っています。それが表すのは、6日間の異なる時間の応力強度です。グラフを正しくプロットします。今私は、その時点で人が喫煙した場合、既存のグラフに点を追加したいと考えました。私は日付、時間、ストレスレベルと人が喫煙しているかどうかで構成されたcsvファイルを読んでいます(もしそうなら1、そうでなければ-1)。私はd3 v4を使用しています。d3

enter image description here

これは私が現在取得していますが、彼らは、私もデータを持っていないところを見せているので、赤い点が間違った場所に明らかにされているものです。 私が望んだのは、赤い点がグラフ上にあり、ユーザーが喫煙した時間を表すことでした。

コード:csvファイルの

<script> 

    var margin = {top: 8, right: 10, bottom: 2, left: 10}, 
      width = 1160 - margin.left - margin.right, 
      height = 100 - margin.top - margin.bottom; 

    var parseDate = d3.timeParse("%H:%M:%S"); 

    var x = d3.scaleTime() 
      .range([0, width]); 

    var y = d3.scaleLinear() 
      .range([height, 0]); 

    var area = d3.area() 
      .x(function (d) { 
       return x(d.time); 
      }) 
      .y0(height) 
      .y1(function (d) { 
       return y(d.stress); 
      }); 

    var line = d3.line() 
      .x(function (d) { 
       return x(d.time); 
      }) 
      .y(function (d) { 
       return y(d.stress); 
      }); 

    d3.csv("6000smokedData3.csv", type, function (error, data) { 

     // Nest data by date. 
     var dates = d3.nest() 
       .key(function (d) { 
        return d.date; 
       }) 
       .entries(data); 

     // Compute the maximum stress per date, needed for the y-domain. 
     dates.forEach(function (s) { 
      s.maxPrice = d3.max(s.values, function (d) { 
       return d.stress; 
      }); 
     }); 

     // Compute the minimum and maximum time across dates. 
     // We assume values are sorted by time. 
     x.domain([ 
      d3.min(dates, function (s) { 
       return s.values[0].time; 
      }), 
      d3.max(dates, function (s) { 
       return s.values[s.values.length - 1].time; 
      }) 
     ]); 

     // Add an SVG element for each date, with the desired dimensions and margin. 
     var svg = d3.select("body").selectAll("svg") 
       .data(dates) 
       .enter().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 + ")"); 

     //Add the scatterplot 
     svg.selectAll("dot") 
       .data(data) 
       .enter().append("circle") 
       .attr("r", 4) 
       .style("fill", function (d) { 
        return "red"; 
       }) 
       .attr("cx", function (d) { 
        if (d.smoked == 1) { 
         return x(d.time); 
        } 
       }) 
       .attr("cy", function (d) { 
        if (d.smoked == 1) { 
         return y(d.stress); 
        } 
       }); 

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

     // Add the Y Axis 
     svg.append("g") 
       .call(d3.axisLeft(y)); 

     // Add the area path elements. Note: the y-domain is set per element. 
     svg.append("path") 
       .attr("class", "area") 
       .attr("d", function (d) { 
        y.domain([0, d.maxPrice]); 
        return area(d.values); 
       }); 

     // Add the line path elements. Note: the y-domain is set per element. 
     svg.append("path") 
       .attr("class", "line") 
       .attr("d", function (d) { 
        y.domain([0, d.maxPrice]); 
        return line(d.values); 
       }); 

     // Add a small label for the date name. 
     svg.append("text") 
       .attr("x", width - 6) 
       .attr("y", height - 6) 
       .style("text-anchor", "end") 
       .text(function (d) { 
        return d.key; 
       }); 
    }); 

    function type(d) { 
     d.stress = +d.stress; 
     d.time = parseDate(d.time); 
     d.smoked = +d.smoked; 
     return d; 
    } 

</script> 

数行:

date,time,stress,smoked 2014-08-04,11:24:28,0.026191,-1 2014-08-04,11:24:29,0.026183,-1 2014-08-04,11:24:30,0.031845,-1 2014-08-04,11:24:31,0.,-1

あなたはそれぞれのYスケールを設定する前に、ドットを描画しているあなたに

答えて

0

ありがとうございました素子。私は通常small multipleseachのループの内側に作るのが好きです。 y軸もオフになっているように見えます - それぞれのプロットで異なるはずです。