2015-04-22 21 views
5

Tôi đang làm bản đồ nhiệt có chức năng phóng to và thu phóng, và nhận ra rằng các điểm dữ liệu đang hiển thị ở phía bên trái trục y khi thu phóng và xoay, sau khi tôi tăng dung lượng lên bên trái của bản đồ nhiệt, để tạo không gian cho trục y (Xem hình). Làm thế nào tôi có thể tránh điều này? Một mẫu mã được cung cấp ở bên dưới.Chỉ định chế độ xem cho bản đồ nhiệt có thể phóng to ở D3

enter image description here

var zoom = d3.behavior.zoom() 
    .scaleExtent([dotWidth, dotHeight]) 
    .x(xScale) 
    .on("zoom", zoomHandler); 

var svg = d3.select("body") 
    .append("svg") 
     .attr("width", width + margin.left + margin.right) 
     .attr("height", height + margin.top + margin.bottom) 
     .call(zoom) 
    .append("g") 
     .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 

function zoomHandler() { 
    var t = zoom.translate(), 
     tx = t[0], 
     ty = t[1]; 

    tx = Math.min(tx, 0); // tx < 0 
    tx = Math.max(tx, -1000); // 
    zoom.translate([tx, ty]); 

    svg.select(".x.axis").call(xAxis); 
    svg.selectAll("ellipse") 
     .attr("cx", function(d) { return xScale(d.day); }) 
     .attr("cy", function(d) { return yScale(d.hour); }) 
     .attr("rx", function(d) { return (dotWidth * d3.event.scale); }); 
} 

svg.selectAll("ellipse") 
    .data(dataset) 
    .enter() 
    .append("ellipse") 
    .attr("cx", function(d) { return xScale(d.day); }) 
    .attr("cy", function(d) { return yScale(d.hour); }) 
    .attr("rx", dotWidth) 
    .attr("ry", dotHeight) 
    .attr("fill", function(d) { return "rgba(100, 200, 200, " + colorScale(d.tOutC) + ")"; }); 

Trả lời

0

tôi đã tìm ra rằng giải pháp là tạo ra một con đường cắt. Tôi đã sử dụng phương pháp cắt từ ví dụ này: http://bl.ocks.org/mbostock/4248145. Về cơ bản tôi đã thêm mã sau đây:

svg.append("clipPath") 
    .attr("id", "clip") 
    .append("rect") 
    .attr("class", "mesh") 
    .attr("width", width) 
    .attr("height", height); 

svg.append("g") 
    .attr("clip-path", "url(#clip)") 
    .selectAll(".hexagon") 
    .data(hexbin(points)) 
    .enter().append("path") 
    .attr("class", "hexagon") 
    .attr("d", hexbin.hexagon()) 
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) 
    .style("fill", function(d) { return color(d.length); }); 

Mã hoạt động tốt với tính năng thu phóng. Chỉ cần gọi chức năng zoom khi tạo canvas svg của bạn. Như thế này:

// SVG canvas 
var svg = d3.select("#chart") 
    .append("svg") 
    .attr("width", width + margin.left + margin.right) 
    .attr("height", height + margin.top + margin.bottom) 
    .call(zoom) 
    .append("g") 
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 
Các vấn đề liên quan