简体   繁体   中英

detect overlapping of circle and change xaxis domain from (0,max) to (min,max)

I created a scatter plot bubble chart. these are two scenarios

first because the difference between total customer is very less both circle are overlapped

首先是因为总客户之间的差异非常小,两个圆圈重叠

second domain is from 0,max 第二个域从0,max开始

I want a way so that it automatically reads if any two circle are overlapping then domain of xaxis should be min,max else it should be 0,max

I am creating the bubble chart like this

var gnodes = svg.selectAll('g.node')
                    .data(this.options.data.events)
                    .enter()
                    .append('g')
                    .classed('gnode', true)
                    .attr("transform", function(d) { return "translate(" +  x(d.Total_Customers) + "," +  y(d.TotalSales) + ")"; })
                    .on('mouseover', tip.show)
                    .on('mouseout', tip.hide);
        var circles = gnodes.append("circle").transition().duration(transTime).style("filter","url(#drop-shadow)")
                        .attr("class", "node").style("fill", function(d) { return color(d.Total_visits);})
                        .attr("r", function(d,i){ return r(d.Total_visits);});

here is my xaxis

getX: function(){
        var temp = 0//this.getMinX();
        return d3.scale.linear().range([0, this.options.width]).domain([temp,this.getMaxX()]);
    }

!! NOTE: my bubble chart implementation is not based on cx and cy. its g being translated. in this implementation I want to know the way to find out overlapping.

https://stackoverflow.com/a/8367547/3513695

The above algorithm will solve your problem.The following is a function that will find the min value of the domain required as per your requirements

var getDomainMinValue = function(data){
   var radiiDiff, radiiSum, centreDist; 
    for (var i =0 ; i < data.length ; i++) {
       for (var j = i+1; j < data.length; j++) {
           radiiDiff = Math.pow(r(data[i].Total_visits) - r(data[j].Total_visits),2)
           radiiSum = Math.pow(r(data[i].Total_visits) + r(data[j].Total_visits),2)
           centreDist = Math.pow(x(data[i].Total_Customers) - x(data[j].Total_Customers),2) + Math.pow(y(data[i].TotalSales) - y(data[j].TotalSales),2)
           if((radiiDiff <= centreDist) && (centreDist <= radiiSum)) {
              //return the min value
              return d3.min(data.map(function(d) { return d.TotalSales }));
            }
        }
     }
     // return 0 otherwise
     return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM