2016-04-12 47 views
1

我是D3.js的新手 我已經通過一些教程並直接跳入我的第一個項目。根據我的需要,我希望將以下內容進行微調。目前我有兩個問題D3.js鼠標懸停和焦點+上下文問題

Focus+Context via Brushing

X-Value Mouseover

  1. 鼠標懸停錯誤地顯示。它呈現在圖表的左側。可能是一個非常小的問題,但我似乎無法找到它。
  2. 我似乎無法找到一種方法來在行旁邊的圖表外顯示「安全值」文本。編輯2 - 我已經想通了

任何幫助將不勝感激。

這裏是CSS

body { 
    font: 10px sans-serif; 
} 

svg { 
    font: 10px sans-serif; 
} 

.line { 
    fill: none; 
    stroke: steelBlue; 
    stroke-width: 1.5px; 
    /*clip-path: url(#clip);*/ 
} 

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

.brush .extent { 
    stroke: #fff; 
    fill-opacity: .125; 
    shape-rendering: crispEdges; 
} 

.overlay { 
    fill: none; 
    pointer-events: all; 
} 

.xy circle { 
    fill: steelblue; 
    stroke: black; 

} 

JS

var margin = {top: 10, right: 15, bottom: 100, left: 60}, 
    margin2 = {top: 430, right: 15, bottom: 20, left: 60}, 
    width = 960 - margin.left - margin.right, 
    height = 500 - margin.top - margin.bottom, 
    height2 = 500 - margin2.top - margin2.bottom; 

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse, 
    bisectDate = d3.bisector(function(d) { return d.date; }).left, 
    formatValue = d3.format(",.2f"), 
    formatData = function(d) { return formatValue(d) + " %"; }; 

var x = d3.time.scale().range([0, width]), 
    x2 = d3.time.scale().range([0, width]), 
    y = d3.scale.linear().range([height, 0]), 
    y2 = d3.scale.linear().range([height2, 0]); 

var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(d3.time.months, 1).tickFormat(d3.time.format("%m/%y")), 
    xAxis2 = d3.svg.axis().scale(x2).orient("bottom").ticks(d3.time.months, 1).tickFormat(d3.time.format("%m/%y")), 
    yAxis = d3.svg.axis().scale(y).orient("left"); 


var brush = d3.svg.brush() 
     .x(x2) 
     .on("brush", brushed); 

var line = d3.svg.line() 
     .interpolate("monotone") 
     .x(function(d) { return x(d.date); }) 
     .y(function(d) { return y(d.value); }); 

var line2 = d3.svg.line() 
     .interpolate("monotone") 
     .x(function(d) { return x2(d.date); }) 
     .y(function(d) { return y2(d.value); }); 

var svg = d3.select("body").append("svg") 
     .attr("width", width + margin.left + margin.right) 
     .attr("height", height + margin.top + margin.bottom) 


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

var focus = svg.append("g") 
     .attr("class", "focus") 
     .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 

var context = svg.append("g") 
     .attr("class", "context") 
     .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")"); 

d3.csv("data.csv", function(error, data) { 

    if (error) throw error; 

    data.forEach(function(d) { 
     d.date = parseDate(d.date); 
     d.value = +d.value; 
    }); 

    data.sort(function(a, b) { 
     return a.date - b.date; 
    }); 


    x.domain(d3.extent(data.map(function(d) { return d.date; }))); 
    y.domain([0, d3.max(data.map(function(d) { return d.value; }))]); 
    x2.domain(x.domain()); 
    y2.domain(y.domain()); 

    focus.append("path") 
      .datum(data) 
      .attr("class", "line") 
      .attr("d", line); 

    focus.append("line") 
      .attr("x1",x(data[0].date)) 
      .attr("y1",y(83)) 
      .attr("x2",x(data[data.length - 1].date)) 
      .attr("y2",y(83)) 
      .attr("stroke","orangered"); 

    svg.append("text") 
      .attr("transform", "translate(" + (width+3) + "," + y(83) + ")") 
      .attr("dy", ".35em") 
      .attr("text-anchor", "start") 
      .style("fill", "orangered") 
      .text(function(d) { return "Safe Value = 83" }); 

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

    focus.append("g") 
      .attr("class", "y axis") 
      .call(yAxis) 
      .append("text") 
      .attr("x", 0) 
      .attr("y", 0) 
      .style("text-anchor", "middle") 
      .attr("transform", "translate(-50,"+ height/2 + ") rotate(-90)") 
      .text("Dissolved Oxygen (%)"); 

    context.append("path") 
      .datum(data) 
      .attr("class", "line") 
      .attr("d", line2); 

    context.append("g") 
      .attr("class", "x axis") 
      .attr("transform", "translate(0," + height2 + ")") 
      .call(xAxis2); 

    context.append("g") 
      .attr("class", "x brush") 
      .call(brush) 
      .selectAll("rect") 
      .attr("y", -6) 
      .attr("height", height2 + 7); 

    var xy = svg.append("g") 
      .attr("class", "xy") 
      .style("display", "none"); 

    xy.append("circle") 
      .attr("r", 4.5); 

    xy.append("text") 
      .attr("x", 9) 
      .attr("dy", ".35em"); 

    svg.append("rect") 
      .attr("class", "overlay") 
      .attr("width", width) 
      .attr("height", height) 
      .style("fill", "none") 
      .on("mouseover", function() { xy.style("display", null); }) 
      .on("mouseout", function() { xy.style("display", "none"); }) 
      .on("mousemove", mousemove); 

    function mousemove() { 
     var x0 = x.invert(d3.mouse(this)[0]), 
       i = bisectDate(data, x0, 1), 
       d0 = data[i - 1], 
       d1 = data[i], 
       d = x0 - d0.date > d1.date - x0 ? d1 : d0; 

     console.log(x0); 
     xy.attr("transform", "translate(" + x(d.date) + "," + y(d.value) + ")"); 
     xy.select("text").text(formatData(d.value)); 

    } 

}); 

function brushed() { 
    x.domain(brush.empty() ? x2.domain() : brush.extent()); 
    focus.select(".line").attr("d", line); 
    focus.select(".x.axis").call(xAxis); 
} 

Plunker Code (詳情請參閱在Plunker的代碼,因爲我已經在那裏更新了幾件事。)謝謝

Image1 Image2

+0

你可以發佈jsfiddle嗎? – Rick

+0

剛剛在Plunkr上發佈了代碼。對不起,以前沒有這樣做 –

+0

謝謝大家。但我修正了錯誤。是一個愚蠢的錯誤。如果有人需要解決方案,我可以發佈它。謝謝你 –

回答

0

對於問題#2,文本的代碼將其放在可見區域外。只要調整參數translate爲類似以下內容:

.attr("transform", "translate(" + (width - 35) + ",30" + ")") 

要不然,你喜歡的東西 - 注意在x負。

+0

我已經找出了這個論證的正確值。我也想把文字排除在圖表之外,所以我必須稍微改變右邊距。謝謝 –