[前端] CSS极坐标的实例代码

1625 0
Honkers 2022-10-19 15:33:38 | 显示全部楼层 |阅读模式
前言

项目有图表方面的需求,其中有做卫星定位的图形,需要制作极坐标来显示当前北半球或南半球的卫星分布情况。第一时间想到了echarts的极坐标,找到示例,虽然满足了部分需求,但是极坐标是由canvs画的,卫星有自己的编号,所以难以辨析每个点对应的卫星编号。于是就想到了自己去用CSS画极坐标
提示:以下是本篇文章正文内容,下面案例可供参考
一、示例

上面示例,下面echarts示例


二、设计步骤

1.纬度
几个div,设置圆角
2.经度
多条0.5px的边框,通过旋转实现
  1. lines: [
  2.         {
  3.           id: 1,
  4.           transform: "translateX(-50%) rotateZ(0deg) scaleX(0.4)",
  5.           borderStyle: "solid",
  6.           borderColor: "#333",
  7.         },
  8.         {
  9.           id: 2,
  10.           transform: "translateX(-50%) rotateZ(45deg) scaleX(0.4)",
  11.           borderStyle: "dashed",
  12.           borderColor: "#91cc75",
  13.         },
  14.         {
  15.           id: 3,
  16.           transform: "translateX(-50%) rotateZ(90deg) scaleX(0.4)",
  17.           borderStyle: "solid",
  18.           borderColor: "#333",
  19.         },
  20.         {
  21.           id: 4,
  22.           transform: "translateX(-50%) rotateZ(135deg) scaleX(0.4)",
  23.           borderStyle: "dashed",
  24.           borderColor: "#91cc75",
  25.         },
  26.       ],
复制代码
3.卫星(点)
后台的数据只有经度和纬度。纬度很好做,按照90°的比例进行定位。经度用到旋转,注意不是直接在点上旋转,否则只是盒子旋转,需要在点外边套一个div进行旋转,如果需要美化,可以使点反方向旋转该角度达到编号是一个正的效果。
三、代码实现

代码是以vue的组件来写的,satellites就是极坐标的数据接口。
  1. <template>
  2.   <div class="polar">
  3.     <div class="polar-main">
  4.       <div class="polar-1">
  5.         <div class="polar-2">
  6.           <div class="polar-3">
  7.             <p
  8.               v-for="item in latitudes"
  9.               :key="item.id"
  10.               class="latitude"
  11.               :style="{
  12.                 top: item.location.top,
  13.                 transform: item.location.transform,
  14.               }"
  15.             >
  16.               {{ item.value }}
  17.             </p>
  18.             <div class="polar-center">
  19.               <div class="satellites">
  20.                 <div v-for="item in satellites" :key="item.name">
  21.                   <p
  22.                     v-for="ele in item.distribution"
  23.                     :key="ele.id"
  24.                     class="satellite-box"
  25.                     :style="{
  26.                       transform: rotate(ele.long),
  27.                     }"
  28.                   >
  29.                     <el-tooltip
  30.                       class="item"
  31.                       effect="dark"
  32.                       :content="`经度:${ele.long} 纬度:${ele.lati}`"
  33.                       placement="top-start"
  34.                     >
  35.                       <span
  36.                         class="satellite"
  37.                         :style="{
  38.                           backgroundColor: ele.color,
  39.                           top: top(ele.lati),
  40.                           transform: rotate(-1 * ele.long),
  41.                         }"
  42.                         >{{ ele.id }}</span
  43.                       >
  44.                     </el-tooltip>
  45.                   </p>
  46.                 </div>
  47.               </div>
  48.             </div>
  49.           </div>
  50.         </div>
  51.       </div>
  52.       <p
  53.         v-for="item in lines"
  54.         :key="item.id"
  55.         class="line"
  56.         :style="{
  57.           transform: item.transform,
  58.           borderStyle: item.borderStyle,
  59.           borderColor: item.borderColor,
  60.         }"
  61.       ></p>
  62.       <p
  63.         v-for="item in longitudes"
  64.         :key="item.id"
  65.         class="longitude"
  66.         :style="{
  67.           top: item.location.top,
  68.           left: item.location.left,
  69.           transform: item.location.transform,
  70.         }"
  71.       >
  72.         {{ item.value }}
  73.       </p>
  74.     </div>
  75.     <div class="satellite-name"></div>
  76.   </div>
  77. </template>
  78. <script>
  79. export default {
  80.   data() {
  81.     return {
  82.       lines: [
  83.         {
  84.           id: 1,
  85.           transform: "translateX(-50%) rotateZ(0deg) scaleX(0.4)",
  86.           borderStyle: "solid",
  87.           borderColor: "#333",
  88.         },
  89.         {
  90.           id: 2,
  91.           transform: "translateX(-50%) rotateZ(45deg) scaleX(0.4)",
  92.           borderStyle: "dashed",
  93.           borderColor: "#91cc75",
  94.         },
  95.         {
  96.           id: 3,
  97.           transform: "translateX(-50%) rotateZ(90deg) scaleX(0.4)",
  98.           borderStyle: "solid",
  99.           borderColor: "#333",
  100.         },
  101.         {
  102.           id: 4,
  103.           transform: "translateX(-50%) rotateZ(135deg) scaleX(0.4)",
  104.           borderStyle: "dashed",
  105.           borderColor: "#91cc75",
  106.         },
  107.       ],
  108.       longitudes: [
  109.         {
  110.           id: 5,
  111.           value: "90°",
  112.           location: {
  113.             top: "50%",
  114.             left: "100%",
  115.             transform: "translateY(-50%)",
  116.           },
  117.         },
  118.         {
  119.           id: 6,
  120.           value: "180°",
  121.           location: {
  122.             top: "100%",
  123.             left: "50%",
  124.             transform: "translateX(-50%)",
  125.           },
  126.         },
  127.         {
  128.           id: 7,
  129.           value: "270°",
  130.           location: {
  131.             top: "50%",
  132.             left: "0",
  133.             transform: "translateX(-100%) translateY(-50%)",
  134.           },
  135.         },
  136.         {
  137.           id: 8,
  138.           value: "360°",
  139.           location: {
  140.             top: "0",
  141.             left: "50%",
  142.             transform: "translateX(-50%) translateY(-100%)",
  143.           },
  144.         },
  145.       ],
  146.       latitudes: [
  147.         {
  148.           id: 1,
  149.           value: "90°",
  150.           location: {
  151.             top: "50%",
  152.             left: "0",
  153.             transform: "translateY(-50%) translateX(50%)",
  154.           },
  155.         },
  156.         {
  157.           id: 2,
  158.           value: "60°",
  159.           location: {
  160.             top: "0",
  161.             left: "0",
  162.             transform: "translateY(-50%) translateX(50%)",
  163.           },
  164.         },
  165.         {
  166.           id: 3,
  167.           value: "30°",
  168.           location: {
  169.             top: "-50%",
  170.             left: "0",
  171.             transform: "translateY(-50%) translateX(50%)",
  172.           },
  173.         },
  174.       ],
  175.       satellites: [
  176.         {
  177.           name: "Below Mask",
  178.           distribution: [
  179.             {
  180.               id: "10",
  181.               long: 46.397128,
  182.               lati: 56.397128,
  183.               color: "#409eff",
  184.             },
  185.             {
  186.               id: "08",
  187.               long: 76.397128,
  188.               lati: 32.916527,
  189.               color: "#409eff",
  190.             },
  191.           ],
  192.         },
  193.         {
  194.           name: "Unhealthy",
  195.           distribution: [
  196.             {
  197.               id: "25",
  198.               long: 156.397128,
  199.               lati: 62.916527,
  200.               color: "#f56c6c",
  201.             },
  202.             {
  203.               id: "25",
  204.               long: 316.397128,
  205.               lati: 12.916527,
  206.               color: "#f56c6c",
  207.             },
  208.           ],
  209.         },
  210.         {
  211.           name: "Missing",
  212.           distribution: [
  213.             {
  214.               id: "07",
  215.               long: 256.397128,
  216.               lati: 22.916527,
  217.               color: "#118452",
  218.             },
  219.             {
  220.               id: "18",
  221.               long: 56.397128,
  222.               lati: 27.916527,
  223.               color: "#118452",
  224.             },
  225.             {
  226.               id: "12",
  227.               long: 66.397128,
  228.               lati: 27.916527,
  229.               color: "#118452",
  230.             },
  231.             {
  232.               id: "16",
  233.               long: 196.397128,
  234.               lati: 67.916527,
  235.               color: "#118452",
  236.             },
  237.           ],
  238.         },
  239.       ],
  240.     };
  241.   },
  242.   methods: {
  243.     top(lati) {
  244.       return ((90 - lati) / 90) * -90 - 10 + "px";
  245.     },
  246.     rotate(long) {
  247.       let z = (long / 360) * 360;
  248.       return `rotateZ(${z}deg)`;
  249.     },
  250.   },
  251.   //   filters: {},
  252. };
  253. </script>
  254. <style scoped lang='scss'>
  255. $polarPiameter: 180px;
  256. .polar-main {
  257.   width: $polarPiameter;
  258.   height: $polarPiameter;
  259.   position: relative;
  260.   p {
  261.     margin: 0;
  262.   }
  263.   .polar-1 {
  264.     width: $polarPiameter;
  265.     height: $polarPiameter;
  266.     border-style: solid;
  267.     .polar-2 {
  268.       width: $polarPiameter * 2/3;
  269.       height: $polarPiameter * 2/3;
  270.       border-style: dashed;
  271.       .polar-3 {
  272.         width: $polarPiameter/3;
  273.         height: $polarPiameter/3;
  274.         border-style: dashed;
  275.         .polar-center {
  276.           width: 1px;
  277.           height: 1px;
  278.           background-color: #333;
  279.         }
  280.       }
  281.     }
  282.   }
  283.   .line {
  284.     height: $polarPiameter;
  285.     border-right-color: #333;
  286.     border-right-width: 1px;
  287.     border-right-style: solid;
  288.     position: absolute;
  289.     left: 50%;
  290.     cursor: pointer;
  291.   }
  292.   .longitude,
  293.   .latitude {
  294.     height: 14px;
  295.     line-height: 14px;
  296.     font-size: 12px;
  297.     color: #868585;
  298.     position: absolute;
  299.     cursor: pointer;
  300.   }
  301. }
  302. .polar-1,
  303. .polar-2,
  304. .polar-3,
  305. .polar-center {
  306.   border-radius: 50%;
  307.   position: absolute;
  308.   top: 0;
  309.   left: 0;
  310.   right: 0;
  311.   bottom: 0;
  312.   margin: auto;
  313.   border-color: #91cc75;
  314.   border-width: 1px;
  315.   box-sizing: border-box;
  316.   cursor: pointer;
  317. }
  318. .polar-1:hover {
  319.   border-width: 2px;
  320. }
  321. .polar-2:hover{
  322.   border-width: 2px;
  323. }
  324. .satellite-box {
  325.   position: absolute;
  326.   width: 1px;
  327.   height: 1px;
  328.   border-radius: 50%;
  329.   background-color: transparent;
  330.   .satellite {
  331.     position: absolute;
  332.     left: -10px;
  333.     width: 20px;
  334.     height: 20px;
  335.     line-height: 20px;
  336.     text-align: center;
  337.     border-radius: 50%;
  338.     font-size: 14px;
  339.     color: #fff;
  340.     cursor: pointer;
  341.     z-index: 999;
  342.     opacity: 0.6;
  343.     transition: 0.6s;
  344.   }
  345.   .satellite:hover {
  346.     background-color: #333 !important;
  347.   }
  348. }
  349. </style>
复制代码
总结

示例图:


到此这篇关于CSS极坐标的实例代码的文章就介绍到这了,更多相关CSS极坐标内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3141
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行