dc.js icon indicating copy to clipboard operation
dc.js copied to clipboard

The label for Y axle in bar chart and the label for row chart are not located properly

Open Smmw opened this issue 9 years ago • 1 comments

The label for Y axle in bar chart and the label for row chart are not located properly. First time loading the page the label for the Y axle and the label for the row chart is the following image: image image

After I drag the filter away from the Y axle, the location of the label will back to normal. image when I drag the filter back to the Y axle the label will also move to improperly location.

After I drag the filter several times, every thing will back to normal.

The code for the barchart and row chart:

var totalAmountPerDayBarChart = dc.barChart('#totalAmountPerDay-barchart');
var tranTypeRowChart = dc.rowChart("#tranType-rowchart");


totalAmountPerDayBarChart
    .width(1200)
    .height(300)
    .margins({top: 10, right: 50, bottom: 30, left: 40})
    .dimension(dateDim)
    .group(totalAmountPerDayGroup)
    .elasticY(true)
    .centerBar(true)
    .gap(0)
    .x(d3.time.scale().domain([minDate, maxDate]))
    .renderHorizontalGridLines(true)
    .xUnits(d3.time.days)				
    .filterPrinter(function (filters) {
        return filters[0][0].toISOString().substring(0, 10) + ' -> ' + filters[0]
                   [1].toISOString().substring(0, 10);
    });

tranTypeRowChart
    .width(400)
    .height(300)
    .margins({top: 20, left: 10, right: 10, bottom: 20})
    .dimension(tranTypeDim)
    .group(tranTypeGroup)
    .label(function (d) {
        if (tranTypeRowChart.hasFilter() && !tranTypeRowChart.hasFilter(d.key)) {
            return d.key + '(0%)';
        }
        var label = d.key;
        if (all.value()) {
            label += '(' + (d.value / all.value() * 100).toFixed(2) + '%)';
        }
        return label;
    })
    .title(function (d) {
        return d.value;
    })
    .elasticX(true)
    .rowsCap(10)
    .ordering(function(d) { return -d.value })
    .xAxis().ticks(5);

I found the reason why this happened. Because I add the d3 force layout. image When I comment the d3-force-layout code, every thing works fine. This is the code of d3-force-layout:

///d3 force lay out function drawForceLayout(jsonFile){ //d3 force layout var width = 300, height = 300; var color = d3.scale.category20(); //Set up the force layout var force = d3.layout.force() // .charge(-120) .linkDistance(200) .size([width, height]);

//remove the existing svg
$("#selectedNode").html("");
$("#forcelayouttittle").html("Money flowing");
if($('#forcelayout svg').length) d3.select("#forcelayout").select('svg').remove();

var svg = d3.select("#forcelayout").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json(jsonFile, function (err, forceData){
    if (err) throw err;
    //Creates the graph data structure out of the json data
    force
        .nodes(forceData.nodes)
        .links(forceData.links)
        .start();

    //Create all the line svgs but without locations yet
    var link = svg.selectAll(".link")
        .data(forceData.links)
        .enter().append("line")
        .attr("class", "link")
        .style("marker-end", "url(#suit)");

    //Do the same with the circles for the nodes - no 
    var node = svg.selectAll(".node")
        .data(forceData.nodes)
        .enter().append("g")
        .attr("class", "node")
        // .call(node_drag)
        // .call(force.drag)
        // .on('dblclick', releasenode)
        .on('click', connectedNodes);
    
    node.append("circle")
        .attr("r", 10)
        .style("fill", function (d) { return color(d.group); })
    
    node.append("text")
        .attr("dx", 10)
        .attr("dy", ".35em")
        .text(function(d) { return d.name })
        .style("stroke", "gray");

    // node.append("title")
    //     .text(function(d) { return d.name; });

    //Now we are giving the SVGs co-ordinates - the force layout is generating the co-ordinates which this code is using to update the attributes of the SVG elements
    force.on("tick", function () {
        link.attr("x1", function (d) { return d.source.x; })
            .attr("y1", function (d) { return d.source.y; })
            .attr("x2", function (d) { return d.target.x; })
            .attr("y2", function (d) { return d.target.y; });

        d3.selectAll("circle")
            .attr("cx", function (d) { return d.x; })
            .attr("cy", function (d) { return d.y; });

        d3.selectAll("text")
            .attr("x", function (d) { return d.x; })
            .attr("y", function (d) { return d.y; });

        // node.attr("cx", function (d) { return d.x; })
        //     .attr("cy", function (d) { return d.y; });
    });
    
    //Node click function:
    //Toggle stores whether the highlighting is on
    var toggle = 0;
    var preNode;
    var resetHTML = "<a href='javascript:drawForceLayout(" + '"data.json"' + ");'>reset</a>";

    //Create an array logging what is connected to what
    var linkedByIndex = {};
    
    for (i = 0; i < forceData.nodes.length; i++) {
        linkedByIndex[i + "," + i] = 1;
    };

    forceData.links.forEach(function (d) {
        linkedByIndex[d.source.index + "," + d.target.index] = 1;
    });

    function showSelectedNode(current, pre){
        var name;
        if(typeof current === 'undefined' && typeof pre === 'undefined')
            name = "no node selected"
        if(typeof current !== 'undefined' && typeof pre === 'undefined')
            name = current.name;
        if(typeof pre !== 'undefined')
            name = pre.name;

        $("#selectedNode").html("Selected: " + name + " " + resetHTML);
    }

    //This function looks up whether a pair are neighbours
    function neighboring(a, b) {
        return linkedByIndex[a.index + "," + b.index];
    }

    function connectedNodes() {
        d = d3.select(this).node().__data__;
        if (typeof preNode !== 'undefined' && preNode.name != d.name) {
            var countLinks = 0;
            node.style("opacity", function (o) { 
                return o.name == d.name || (o.name == preNode.name && neighboring(o, d)) ? 1 : 0.1;
            });
            link.style("opacity", function (o) {
                var value = preNode.index == o.source.index && d.index == o.target.index ? 1 : 0.1;
                if(value == 1)
                    countLinks ++;
                return value;
            });
            //no links between preNode and currentNode. change the preNode pointer.
            if(countLinks == 0) preNode = d;

            showSelectedNode(d, preNode);
            return;
        }
        if (toggle == 0) {
            node.style("opacity", function (o) { return neighboring(d, o) ? 1 : 0.1; });
            link.style("opacity", function (o) { return d.index == o.source.index ? 1 : 0.1; });
            toggle = 1;
        }else{
            if(toggle == 1){
                node.style("opacity", function (o) { return neighboring(o, d) ? 1 : 0.1; });
                link.style("opacity", function (o) { return d.index == o.target.index ? 1 : 0.1; });
                toggle = 2;
            }else{
                //Put them back to opacity=1
                node.style("opacity", 1);
                link.style("opacity", 1);
                preNode = undefined;
                toggle = 0;
                showSelectedNode(undefined, undefined);
                return;
            }
        }
        showSelectedNode(d, preNode);
        preNode = d;
    }
    //arrows
    svg.append("defs").selectAll("marker")
        .data(["suit", "licensing", "resolved"])
        .enter().append("marker")
        .attr("id", function(d) { return d; })
        .attr("viewBox", "0 -5 10 10")
        .attr("refX", 25)
        .attr("refY", 0)
        .attr("markerWidth", 8)
        .attr("markerHeight", 8)
        .attr("orient", "auto")
        .append("path")
        .attr("d", "M0,-5L10,0L0,5 L10,0 L0, -5")
        .style("stroke", "#4679BD")
        .style("opacity", "0.6");
    
})

}

Smmw avatar Sep 28 '17 04:09 Smmw

I solved this problem.

Smmw avatar Sep 28 '17 06:09 Smmw