D3.js 鼠标悬停和焦点 + 上下文问题

D3.js Mouseover and Focus + Context issue

我是 D3.js 的新手 我已经完成了一些教程并直接进入了我的第一个项目。我希望根据我的需要将以下内容与轻微的调整结合起来。目前我有两个问题

Focus+Context via Brushing

X-Value Mouseover

  1. 鼠标悬停显示错误。它呈现在图表的左侧。可能是一个非常小的问题,但我似乎找不到它。
  2. 我似乎想不出一种方法来在图表外的行旁边显示 "Safe Value" 文本。编辑 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

对于您的问题 #2,文本代码将其放置在可见区域之外。只需将 translate 的参数调整为如下所示:

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

或您喜欢的其他内容 - 请注意 x 上的减号。