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

时间:2016-04-12 17:00:05

标签: javascript css d3.js charts linechart

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

Focus+Context via Brushing

X-Value Mouseover

  1. 错误地显示了鼠标悬停。它呈现在图表的左侧。可能是一个非常小的问题,但我似乎无法找到它。
  2. 我似乎无法找到一种方法来显示该行旁边的图表外的“安全值”文本。编辑2 - 我已经想到了这个
  3. 非常感谢任何帮助。

    这是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

1 个答案:

答案 0 :(得分:0)

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

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

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