2015-07-22 33 views
0

我想用d3.js繪製xy多重線圖。沒關係。 但之後我想縮放x軸序數。D3.js xy按順序排列的線圖

所以這就是我的XY線型圖的代碼:

<!DOCTYPE html> 
<meta charset="utf-8"> 
    <style> 
    body { 
    font: 12px Arial; 
} 

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

.axis path, 
.axis line { 
    fill: none; 
    stroke: #000; 
    shape-rendering: crispEdges; 
} 

div.tooltip { 
    position: absolute; 
    text-align: center; 
    padding: 5px; 
    font: 14px sans-serif; 
    background: black; 
    color: white; 
    border: 0px; 
    border-radius: 8px; 
} 

.line { 
    fill: none; 
    stroke: steelblue; 
    stroke-width: 1px; 
} 

    </style> 
    <body> 
    <script src="d3.js"></script> 
    <script src="jquery-2.1.4.js" charset="utf-8"></script> 
    <script> 
     var margin = { 
      top: 20, 
      right: 20, 
      bottom: 20, 
      left: 50 
     }, 
     width = 1180 - margin.left - margin.right, 
     height = 580 - margin.top - margin.bottom; 

var x = d3.scale.linear().rangeRound([0, width]); 
var y = d3.scale.linear().rangeRound([height, 0]); 

var linearScale = d3.scale.linear(); 

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

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

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 line = d3.svg.line() 
.x(function(d) { console.log('x'+x(d.arbeitsgang));return x(d.arbeitsgang); }) 
.y(function(d) { console.log('y'+y(d.koordinaten));return y(d.koordinaten); }); 
*/ 
var line = d3.svg.line() 
     .x(function (d) { 
      return x(d.x); 
     }) 
     .y(function (d) { 
      return y(d.y); 
     }); 

// Define 'div' for tooltips 
var div = d3.select("body") 
     .append("div") // declare the tooltip div 
     .attr("class", "tooltip")    // apply the 'tooltip' class 
     .style("opacity", 0);     // set the opacity to nil 

//d3.json("Arbeitsgang.json", function(error, data) { 
var data = [ 
    { 
     "key": "Paket 1", 
     "values": [ 
      { 
       "x": 0, 
       "y": 40, 
       "arbeitsgang": "A1" 
      }, 
      { 
       "x": 6, 
       "y": 30, 
       "arbeitsgang": "A2" 
      }, 
      { 
       "x": 17, 
       "y": 20, 
       "arbeitsgang": "A3" 
      } 
     ] 
    }, 
    { 
     "key": "Paket 3", 
     "values": [ 
      { 
       "x": 0, 
       "y": 85, 
       "arbeitsgang": "A1" 
      }, 
      { 
       "x": 8, 
       "y": 50, 
       "arbeitsgang": "A2" 
      }, 
      { 
       "x": 17, 
       "y": 89, 
       "arbeitsgang": "A3" 
      } 
     ] 
    }, 
    { 
     "key": "Paket 2", 
     "values": [ 
      { 
       "x": 0, 
       "y": 45, 
       "arbeitsgang": "A1" 
      }, 
      { 
       "x": 6, 
       "y": 145, 
       "arbeitsgang": "A1" 
      }, 
      { 
       "x": 17, 
       "y": 53, 
       "arbeitsgang": "A1" 
      } 
     ] 
    } 
]; 

linearScale.domain(d3.keys(data[0]).filter(function (key) { 
    return key; 
})); 


x.domain([ 
    d3.min(data, function (c) { 
     return d3.min(c.values, function (v) { 
      return v.x; 
     }); 
    }), 
    d3.max(data, function (c) { 
     return d3.max(c.values, function (v) { 
      return v.x; 
     }); 
    }) 
]); 
y.domain([ 
    d3.min(data, function (c) { 
     return d3.min(c.values, function (v) { 
      return v.y; 
     }); 
    }), 
    d3.max(data, function (c) { 
     return d3.max(c.values, function (v) { 
      return v.y; 
     }); 
    }) 
]); 

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

svg.append("g") 
     .attr("class", "y axis") 
     .call(yAxis); 

var graphen = svg.selectAll(".graphen") 
     .data(data) 
     .enter().append("g") 
     .attr("class", "graphen"); 

var graph = graphen.append("path") 
     .attr("class", "line") 
     .attr("d", function (d) { 
      return line(d.values); 
     }); 

graph.on("mouseover", function (d) { 
    d3.select(this).style("stroke-width", 7); 

    div.transition() 
      .duration(200) 
      .style("opacity", .9); 
    div.html(d.key) 
      .style("left", (d3.event.pageX) + "px") 
      .style("top", (d3.event.pageY - 28) + "px"); 

    div.style("visibility", "visible"); 

    var selectthegraphs = $('.line').not(this);  //select all the rest of the lines, except the one you are hovering on and drop their opacity 
    d3.selectAll(selectthegraphs) 
      .style("opacity", 0.2); 

    var selectcircles = $('.circle'); 
    d3.selectAll(selectcircles) 
      .style("opacity", 0.2); 
}) 
     .on("mouseout", function() { 
      d3.select(this).style("stroke-width", 1); 
      div.transition() 
        .duration(500) 
        .style("opacity", 0.01); 

      div.style("visibility", "hidden"); 

      var selectthegraphs = $('.line');  //select all the rest of the lines, except the one you are hovering on and drop their opacity 
      d3.selectAll(selectthegraphs) 
        .style("opacity",1); 

      var selectcircles = $('.circle'); 
      d3.selectAll(selectcircles) 
        .style("opacity", 1); 
     }); 

graphen.each(function (p, j) { 
    d3.select(this).selectAll("circle") 
      .data(p.values) 
      .enter().append("circle") 
      .style("stroke", "black") 
      .style("fill", "white") 
      .attr("class","circle") 
      .attr("r", 5) 
      .attr("cx", function (d) { 
       return x(d.x); 
      }) 
      .attr("cy", function (d) { 
       return y(d.y); 
      }) 
      .on("mouseover", function (d) { 
       d3.select(this).transition().duration(500) 
         .attr("r", 10); 
       div.transition() 
         .duration(500) 
         .style("opacity", 0.9); 
       div.style("visibility", "visible"); 

       div.html("X: " + d.x + "<br/>" + "Y: " + d.y + "<br/>" + "Arbeitsgang: " + d.arbeitsgang) 
         .style("left", (d3.event.pageX) + "px") 
         .style("top", (d3.event.pageY - 28) + "px"); 
      }) 
      .on("mouseout", function (d) { 
       d3.select(this).transition().duration(500) 
         .attr("r", 5); 
       div.transition() 
         .duration(500) 
         .style("opacity", 0); 
       div.style("visibility", "hidden"); 
      }) 
}); 

確定。但現在我想要一個具有序數比例的同一個圖。 蜱應爲 「data.values.arbeitsgang」

「arbeitsgang」: 「A1」 例如。

請幫幫我

回答

0

有序秤並不那麼難。

var x = d3.scale.ordinal() 
        .domain(["A1","A2","A3"]) 
        .rangeRoundPoints([0, width]); 

比例尺現在應該將範圍從0到你的寬度分成3個同樣大的部分。我選擇了rangeRoundPoints,因爲tick的值被四捨五入爲整數,我喜歡整數。

我相信你可以自定義x軸的文本(當然使用你的序號比例來縮放軸),例如顯示「Arbeitsgang:A1」,但我不知道如何在我的頭頂。

有關序數刻度的更多信息,請查看this link。除非我完全誤解了這個問題,否則我認爲這是你唯一需要的。

我意識到我已經對域的值進行了硬編碼,但是包含這些值的數組也可以。

+0

謝謝。但這不是我所期望的。請幫助我更多。 –