简体   繁体   English

创建由线连接到中间主圆的CSS圆

[英]Creating CSS circles connected by lines to middle main circle

创建由直线连接到中间主圆的css圆

I need to create a page something like this. 我需要创建一个类似这样的页面。 The blue circle is the main circle and green circles should place around the main circle. 蓝色圆圈是主圆圈,绿色圆圈应围绕主圆圈。 The green circles count is random (around 0 - 10). 绿色圆圈计数是随机的(大约0-10)。 All green circles are connected to blue circle with a line. 所有绿色圆圈都用一条线连接到蓝色圆圈。

I know to draw circle in CSS. 我知道在CSS中画圆。 I need to know, 我需要知道,

  1. How to place green circles around the blue circle 如何在蓝色圆圈周围放置绿色圆圈
  2. How to connect green circles to the blue circle 如何将绿色圆圈连接到蓝色圆圈

Is it possible to do with CSS. 是否有可能与CSS。 If not what is the way? 如果不是,那怎么办?

Thank you. 谢谢。

What you will need is a position: relative; 您将需要一个position: relative; container with child elements positioned absolute 子元素位于absolute位置的容器

Demo 演示

Demo 2 (Using transform ) 演示2 (使用transform

Explanation: What am doing here is using position: relative; 说明:此处使用的是position: relative; on the parent element which is .ball_wrap , than am using position: absolute; 在父元素.ball_wrap ,比使用position: absolute; for the child elements AS WELL AS the :after pseudo elements to connect the child elements with the parent. 对于子元素,以及:after伪元素,用于将子元素与父元素连接。 If you are not aware of the pseudo elements than take them as a virtual element, these elements do not exist literally in the DOM but they do render graphically. 如果您不知道伪元素而是将它们当作虚拟元素,则这些元素在DOM中并不存在,但它们确实以图形方式呈现。 So am using display: block; 所以我正在使用display: block; as they are inline by default..along with content: ""; 因为默认情况下它们是inline的。.以及content: ""; ... Rest, set them accordingly using top , right , bottom and left properties. ...休息,使用toprightbottomleft属性相应地设置它们。

.ball_wrap {
    position: relative;
    margin: 150px;
    width: 90px;
}

.green_ball {
    background: #00C762;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    border: 3px solid #ccc;
    position: absolute;
}

.blue_ball {
    background: #2F9BC1;
    height: 80px;
    width: 80px;
    border-radius: 50%;
    border: 3px solid #ccc;
}

.ball_wrap div:nth-of-type(2) {
    top: 20px;
    left: -100px;
}

.ball_wrap div:nth-of-type(2):after {
    content: "";
    display: block;
    border-bottom: 1px solid #000;
    position: absolute;
    width: 50px;
    right: -50px;
    top: 50%;
}

.ball_wrap div:nth-of-type(3) {
    top: 20px;
    right: -100px;
}

.ball_wrap div:nth-of-type(3):after {
    content: "";
    display: block;
    border-bottom: 1px solid #000;
    position: absolute;
    width: 50px;
    left: -52px;
    top: 50%;
}

.ball_wrap div:nth-of-type(4) {
    right: 20px;
    bottom: -100px;
}

.ball_wrap div:nth-of-type(4):after {
    content: "";
    display: block;
    border-left: 1px solid #000;
    position: absolute;
    height: 50px;
    left: 50%;
    top: -52px;
}

Also you might take a look at these types of charts using jQuery 您也可以使用jQuery来查看这些类型的图表

You can create the round shapes just with plain CSS: 您可以使用纯CSS创建圆形:

html: HTML:

<div id="ball" class="border"> 
    <div class="blue ball"> </div>
</div>
<div id="ball1" class="border"> 
    <div class="green ball"> </div>
</div>

css: CSS:

.border
{
    position: relative;
    width: 115px;
    height: 115px;
    background: #e7e9e9;
    border-radius: 100px;
    border: 2px solid #d1d1d1;
}

.ball
{
    position: absolute;
    left: 9%;    
    top: 9%;

    width: 90px;
    height: 90px;

    border-radius: 100px;
}

.blue
{
    background: #2f9bc1;
    border: 2px solid #266a8e;
}

.green
{
   background: #00c762; 
   border: 2px solid #00be58;
}

#ball
{
    top: 200px;   
    left: 300px;
}

Where you place each shape at the right position with position: relative; 将每个形状放置在正确位置的position: relative; offset. 偏移。

For the lines you could use HTML 5 canvas : 对于这些行,您可以使用HTML 5 canvas

html: HTML:

<canvas id="myCanvas" class="line"></canvas>

javascript canvas: JavaScript画布:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

context.beginPath();
context.moveTo(350, 150);
context.lineTo(50, 50);
context.stroke();

Where i use a position: absolute; 我在哪里使用position: absolute; for the line, so it doesn't push the shapes away and a z-index so it is beneath the shapes: 对于该线,因此它不会将形状推开,并且不会将z-index推到形状下方:

.line
{
    position: absolute;
    width: 320px;
    z-index: -1;
}

jsFiddle 的jsfiddle

Here is a rough sample html in using canvas. 这是使用canvas的一个大致示例html。
--- html --- -html-

<div style="border:solid thick #000">
    <canvas id="canvas"></canvas>
</div>


--- javascript --- --- JavaScript ---

<script>
            var ctx;
            window.onload = function() {
                canvas = document.getElementById('canvas');
                if (!canvas || !canvas.getContext) {
                    return false;
                }
                canvas.width = 600;
                canvas.height = 600;
                ctx = canvas.getContext('2d');           
                ctx.strokeStyle = '#000';
                ctx.fillStyle = "green";
                ctx.translate(300, 300);
                drawChildCircles(5);
                ctx.arc(0, 0, 20, 0, Math.PI * 2);//center circle
                ctx.stroke();
                ctx.fill();                 
            }

            function drawChildCircles(n) {
                var ang_unit = Math.PI * 2 / n;
                ctx.save();
                for (var i = 0; i < n; i++) {
                    ctx.rotate(ang_unit);
                    ctx.beginPath();
                    ctx.moveTo(0,0);
                    ctx.lineTo(100,0);
                    ctx.arc(100, 0, 20, 0, Math.PI * 2);
                    ctx.stroke();
                    ctx.fill();
                }
                ctx.restore();
            }
        </script>

However, I believe that the best way is to use svg with d3.js, 但是,我认为最好的方法是在d3.js中使用svg,
especially if you want to draw some data visualization or relation map. 特别是如果您想绘制一些数据可视化或关系图。

Here's another example using svg elements. 这是另一个使用svg元素的示例 SVG elements are very good for these kinda cases. SVG元素对于这些情况非常有用。

You'll get more info over here . 您将在此处获得更多信息。

If you're trying to do some visualizations out of it. 如果您要进行一些可视化处理。 I'd suggest you to get along with d3 . 我建议您与d3相处。 A javascript library that used svg elements to create amazing visualization. 一个使用svg元素创建令人惊叹的可视化效果的JavaScript库。

HTML: HTML:

 <div id="container">
   <svg id="red">
        <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
    </svg>
    <svg id="red-line">
        <line x1="130" y1="50" x2="300" y2="50" style="stroke:rgb(255,0,0);stroke-width:2"/>
    </svg>
    <svg id="blue">
        <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue"/>
    </svg>
    <svg id="blue-line">
        <line x1="130" y1="50" x2="260" y2="50" style="stroke:rgb(255,255,0);stroke-width:2"/>
    </svg>
    <svg id="green">
        <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="green"/>
    </svg>
    <svg id="green-line">
        <line x1="100" y1="0" x2="100" y2="70" style="stroke:rgb(255,0,255);stroke-width:2"/>
    </svg>
    <svg id="yellow">
        <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="yellow"/>
    </svg>

CSS : CSS:

#container {
      position: relative;
      margin: 150px 0 0 250px;
    }
    #container svg {
      position: absolute;
    }
    #blue {
      top: -150px;
    }
    #green {
      left: -200px;
    }
    #yellow {
      left: 200px;
    }
    #blue-line {
      margin-left: -200px;
    }
    #green-line {
      margin-top: -60px;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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