Bubble chart label placement algorithm? (preferably in JavaScript)

后端 未结 9 696
时光取名叫无心
时光取名叫无心 2021-01-30 17:15

I\'d like to automatically place 100-200 bubble labels so that the following requirements are met:

  • Labels should not overlap
  • Labels should preferably not
9条回答
  •  星月不相逢
    2021-01-30 17:44

    This can be formulated as a Linear Programming problem. You want to maximize or minimize a function (representing the "weight" or "goodness" of the solution) subject to certain constraints (your requirements). You'll need to formalize your requirements as values. Something like this:

    Variables:
    x1 = 1 if Labels overlap, 0 otherwise
    x2 = some measure of "how much" a label overlaps a bubble
    x3 = distance from label to bubble //Label should be close to bubble
    x4 = distance between ideal and actual label position
    x5 = difference between actual and ideal font size
    
    
    minimize x1 + 10*x2 + x3 + 20*x4 + 5*x5
    
    subject to constraints:
        x1   = 0  // labels can never overlap
        x2   < /* maximum amount a label is allowed to overlap a bubble */
        ...
        x5   < 6 // font size does not vary by more than +/- 6 pts.
    

    I made up the coefficients and the constraints, you can use them to tune the result based on which features are most important to you. Coefficients increase the value of a requirement (in this case, f4 is weighted most so it 'most important'. The constraints are hard limits that cannot be violated.

    Since this is a well-known problem, you can now solve it using a number of methods. The Simplex algorithm is popular. There is a whole chapter in Cormen et. al about these problems.

提交回复
热议问题