【问题标题】:D3-After Loading different data, clicking the brush makes line-graph disappearD3-加载不同数据后,点击画笔使折线图消失
【发布时间】:2015-09-11 15:17:21
【问题描述】:

我用画笔制作了一个线图来放大。第一次加载 csv 文件时,画笔可以正常工作。问题是,在我从下拉菜单中加载不同的 csv 文件后,只需单击画笔即可使线条消失。我还没有看到任何显示类似情况的示例,所以有人可以帮忙吗? 这是结果plunker

这是图表的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>D3 Test</title>
        <script type="text/javascript" src="../d3/d3.js"></script>
        <script type="text/javascript" src="../d3/d3-tip.js"></script>
        <style type="text/css">
            body{
                font: 16px Calibri;
            }

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

            .brushLine{
                fill: none;
                stroke: steelblue;
                stroke-width: 2px;
            }

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

            .axis path,
            .axis line{
                fill:none;
                stroke: black;
                stroke-width: 1px;
                shape-rendering: crispEdges;
            }

            .axis text{
                font-family: sans-serif;
                font-size: 14px;
                stroke: black;
                stroke-width: 0.5px;
            }

        </style>
        <!--...this code will be used on an external html file and instered-->
        <?php
            include('../dropdownMetrics.php');
        ?>
        <!--...............................................................-->
    </head>
    <body>
        <script type="text/javascript">


var margin = {top: 60, right: 20, bottom: 40, left: 40},
    margin2 = {top: 430, right: 20, bottom: 20, left: 40},
    width = 800 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    height2 =900 - margin2.top,
    height3= 500 - margin2.top - margin2.bottom;

var x = d3.scale.linear()
    .range([0, width]);

var x1 = d3.scale.linear()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var y1 = d3.scale.linear()
    .range([height3, 0]);

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

var x1Axis = d3.svg.axis()
    .scale(x1)
    .orient("bottom");

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

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

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

//-------------------------defining focus and context------------------------
var focus = svg.append("g")
            .attr("class","focus");

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

//-------------------------defining the focus and brush lines----------------           
var line = d3.svg.line()
        .x(function(d) { return x(d.trID);})
        .y(function (d) {return y(d.newT);})
        .interpolate("basis");

var brushLine = d3.svg.line()
        .x(function(d) { return x(d.trID);})
        .y(function (d) {return y1(d.newT);})
        .interpolate("basis");

//---------------------------------------------------------------------------

var dsv = d3.dsv(";", "text/plain");    //setting the delimiter
var dataset = []                        //defining the data array
var datapath="../CSV/atlas/results/metrics.csv";
    dsv(datapath, function(data){   //------------select the file to load the csv------------

        var label = document.getElementById('opts')[document.getElementById('opts').selectedIndex].innerHTML;//takes the name of the f
        console.log(label);

        dataset= data.map(function(d){      //parse
            return {                        //insert parsed data in the array
                trID: +d["trID"],
                newT: +d["#newT"]
            };
        });

        console.log(dataset);
        x.domain(d3.extent(dataset, function(d) { return d.trID; }));
        x1.domain(x.domain());
        y.domain(d3.extent(dataset, function(d) { return d.newT; }));
        y1.domain(y.domain());

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


        focus.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis)
            .append("text")
            .attr("class", "label")
            .attr("x", width)
            .attr("y", -6)
            .style("text-anchor", "end")
            .text("trID");

        focus.append("g")
            .attr("class", "y axis")
            .call(yAxis)
            .append("text")
            .attr("class", "label")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", ".71em")
            .style("text-anchor", "end")
            .text("num of tables");

        context.append("path")
            .datum(dataset)
            .attr("class", "brushLine")
            .attr("d", brushLine);


        context.append("g")
            .attr("class", "x1 axis")
            .attr("transform", "translate(0," + height3  + ")")
            .call(x1Axis)
            .append("text")
            .attr("class", "label")
            .attr("x", width)
            .attr("y", -6)
            .style("text-anchor", "end")
            .text("trID");

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

        svg.append("text")
            .attr("class","simpletext")
            .attr("x", (width/2))
            .attr("y", 0 - (margin.top/2))
            .attr("text-anchor", "middle")
            .style("font-size", "20px")
            .style("text-decoration", "underline")
            .text(label);
    });

    d3.select('#opts')
        .on('change', function(){
            var dataset=[]
            var datapath = eval(d3.select(this).property('value'));
            label = document.getElementById('opts')[document.getElementById('opts').selectedIndex].innerHTML;


            dsv(datapath, function(data){   //------------select the file to load the csv------------
                dataset= data.map(function(d){      //parse
                    return {                        //insert parsed data in the array
                    trID: +d["trID"],
                    newT: +d["#newT"]
                    };
                });


            x.domain(d3.extent(dataset, function(d) { return d.trID; }));
            x1.domain(x.domain());
            y.domain(d3.extent(dataset, function(d) { return d.newT; }));
            y1.domain(y.domain());

            d3.selectAll(".line")
                .transition()
                .duration(1000)
                .attr("d", line(dataset));

            //Update Axis
            //Update X axis
            focus.select(".x.axis")
                .transition()
                .duration(1000)
                .call(xAxis);

            //Update Y axis
            focus.select(".y.axis")
                .transition()
                .duration(1000)
                .call(yAxis);

            focus.selectAll("path")
                .data(dataset)
                .exit()
                .remove();
                console.log(label);

            d3.selectAll(".brushLine")
                .transition()
                .duration(1000)
                .attr("d", brushLine(dataset));

            context.select(".x1.axis")
                .transition()
                .duration(1000)
                .call(x1Axis);

            context.select(".x.brush")
                .call(brush);

            svg.selectAll(".simpletext")
                .transition()
                .text(label);


        });
    });

function brushed(){
        x.domain(brush.empty()? x1.domain() : brush.extent());
        focus.select(".line")
            .attr("d", line);
        focus.select(".x.axis").call(xAxis);
}
        </script>
    </body>
</html>   

【问题讨论】:

    标签: d3.js transition brush


    【解决方案1】:

    您将.on('change' 中的数据错误地绑定到您的焦点path。在这种情况下你想使用datum:

    focus.selectAll("path")
      .datum(dataset);
    

    更新code

    【讨论】:

    • 非常感谢您的解决方案。我无法理解我是如何错过的。现在一切正常,但我在控制台日志中收到消息“focus.selectAll.........datum(”不是一个函数。它可以工作但由于某种原因我只是收到错误消息。
    • @JohnM.,对我不好。你实际上根本不需要.exit().remove()
    • 再次感谢。惊人的帮助和建议。可悲的是,我需要更多的声誉才能对您的解决方案表示支持,但还是表示支持。最好的一天,我的朋友。
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多