溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

swiper+echarts如何實現(xiàn)多個儀表盤左右滾動效果

發(fā)布時間:2021-06-30 13:52:36 來源:億速云 閱讀:190 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)swiper+echarts如何實現(xiàn)多個儀表盤左右滾動效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

1、swiper的使用

a.首先加載插件

<!DOCTYPE html>
<html>
<head>
    ...
    <link rel="stylesheet" href="dist/css/swiper.min.css" >
</head>
<body>
    ...
    <script src="dist/js/swiper.min.js"></script>
    ...
</body>
</html>

b.HTML內(nèi)容

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
    <!-- 如果需要分頁器 
    <div class="swiper-pagination"></div>-->
    
    <!-- 如果需要導(dǎo)航按鈕 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    
    <!-- 如果需要滾動條 
    <div class="swiper-scrollbar"></div>-->
</div>

c.你可能想要給Swiper定義一個大小,當(dāng)然不要也行。

.swiper-container {
    width: 600px;
    height: 300px;
}

d.初始化Swiper:最好是挨著</body>標簽

<script>        
  var mySwiper = new Swiper ('.swiper-container', {
    direction: 'vertical', // 垂直切換選項
    loop: true, // 循環(huán)模式選項
    
    // 如果需要分頁器
    pagination: {
      el: '.swiper-pagination',
    },
    
    // 如果需要前進后退按鈕
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
    
    // 如果需要滾動條
    scrollbar: {
      el: '.swiper-scrollbar',
    },
  })        
</script>

下面是我要實現(xiàn)的效果

swiper+echarts如何實現(xiàn)多個儀表盤左右滾動效果

代碼如下

加載插件和樣式

<!DOCTYPE html>
<html>
<head>
    ...
    <link rel="stylesheet" href="dist/css/swiper.min.css" >
<style>
        *{
            margin:0;
            padding:0;
        }
        .swiper-container{
            height:200px;
            width:800px;
            margin:0 auto;
            border:1px solid #ccc;
        }
        .swiper-slide{
            display:flex;
        }
        .swiper-slide .chart{
            flex:1;
        }
 
    </style>
</head>
<body>
    ...
<script src="https://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts.min.js"></script>
    <script src="dist/js/swiper.min.js"></script>
    ...
</body>
</html>

html結(jié)構(gòu)

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">
            <div class="chart" id="chart1">1</div>
            <div class="chart" id="chart2">2</div>
            <div class="chart" id="chart3">3</div>
        </div>
        <div class="swiper-slide">
            <div class="chart" id="chart4">4</div>
            <div class="chart" id="chart5">5</div>
            <div class="chart" id="chart6">6</div>
        </div>
        <div class="swiper-slide">
            <div class="chart" id="chart7">7</div>
            <div class="chart" id="chart8">8</div>
            <div class="chart" id="chart9">9</div>
        </div>
    </div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
</div>

初始化swiper

var mySwiper = new Swiper('.swiper-container', {
        autoplay: {
            delay:5000
        },//可選選項,自動滑動\
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        }
    })

初始化echarts

function initChart(obj){
          var myChart = echarts.init(document.getElementById(obj));
 
          var option = {
              tooltip : {
                  formatter: "{a} <br/> : {c}%"
              },
              series: [
                  {
                      type : "gauge",
                      center: ["50%", "50%"], // 默認全局居中
                      radius : "90%",
                      startAngle: 200,
                      endAngle: -20,
                      axisLine : {
                          show : true,
                          lineStyle : { // 屬性lineStyle控制線條樣式
                              color : [ //表盤顏色
                                  [ 0.5, "#DA462C" ],//0-50%處的顏色
                                  [ 0.7, "#FF9618" ],//51%-70%處的顏色
                                  [ 0.9, "#FFED44" ],//70%-90%處的顏色
                                  [ 1,"#20AE51" ]//90%-100%處的顏色
                              ],
                              width : 20//表盤寬度
                          }
                      },
                      splitLine : { //分割線樣式(及10、20等長線樣式)
                          length : 10,
                          lineStyle : { // 屬性lineStyle控制線條樣式
                              width : 2
                          }
                      },
                      axisTick : { //刻度線樣式(及短線樣式)
                          length : 20
                      },
                      axisLabel : { //文字樣式(及“10”、“20”等文字樣式)
                          color : "black",
                          distance : 10//文字離表盤的距離
                      },
                      detail: {
                          formatter : "{score|{value}%}",
                          offsetCenter: [0, "40%"],
                          // backgroundColor: '#FFEC45',
                          height:20,
                          rich : {
                              score : {
                                  // color : "#333",
                                  fontFamily : "微軟雅黑",
                                  fontSize :14
                              }
                          }
                      },
                      data: [{
                          value: 56,
                          label: {
                              textStyle: {
                                  fontSize: 12
                              }
                          }
                      }]
                  }
              ]
          }
 
          setInterval(function () {
              option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
              myChart.setOption(option, true);
          },2000);
      }

調(diào)用初始化化echats函數(shù)

initChart('chart1')
initChart('chart2')
initChart('chart3')
initChart('chart4')
initChart('chart5')
initChart('chart6')
initChart('chart7')
initChart('chart8')
initChart('chart9')

插播一個echarts儀表盤的配置函數(shù) 

function initChart(obj){
        var myChart = echarts.init(document.getElementById(obj));
 
        var option = {
            tooltip : {
                formatter: "{a} <br/> : {c}%"
            },
            // toolbox: {
            //     feature: {
            //         restore: {},
            //         saveAsImage: {}
            //     }
            // },
            series: [
                {
                    name: '業(yè)務(wù)指標',
                    type: 'gauge',
                    center: ["50%", "45%"], // 儀表位置
                    radius: "80%", //儀表大小
                    detail: {formatter:'{value}%'},
                    startAngle: 200, //開始角度
                    endAngle: -20, //結(jié)束角度
                    data: [{value: 30, name: '完成率'}],
                    axisLine: {
                        show: false,
                        lineStyle: { // 屬性lineStyle控制線條樣式
                            color: [
                                [ 0.5,  new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                                    offset: 1,
                                    color: "#E75F25" // 50% 處的顏色
                                }, {
                                    offset: 0.8,
                                    color: "#D9452C" // 40% 處的顏色
                                }], false) ], // 100% 處的顏色
                                [ 0.7,  new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                                    offset: 1,
                                    color: "#FFC539" // 70% 處的顏色
                                }, {
                                    offset: 0.8,
                                    color: "#FE951E" // 66% 處的顏色
                                }, {
                                    offset: 0,
                                    color: "#E75F25" // 50% 處的顏色
                                }], false) ],
                                [ 0.9,  new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                    offset: 1,
                                    color: "#C7DD6B" // 90% 處的顏色
                                }, {
                                    offset: 0.8,
                                    color: "#FEEC49" // 86% 處的顏色
                                }, {
                                    offset: 0,
                                    color: "#FFC539" // 70% 處的顏色
                                }], false) ],
                                [1,  new echarts.graphic.LinearGradient(0, 0, 0, 1, [ {
                                    offset: 0.2,
                                    color: "#1CAD52" // 92% 處的顏色
                                }, {
                                    offset: 0,
                                    color: "#C7DD6B" // 90% 處的顏色
                                }], false) ]
                            ],
                            width: 10
                        }
                    },
                    splitLine: {
                                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                                            show: false
                      },
                        pointer : { //指針樣式
                               length: '45%'
                           },
                        detail: {
                                show: false
                           }
                }
            ]
        }
 
        setInterval(function () {
            option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
            myChart.setOption(option, true);
        },2000);
    }

關(guān)于“swiper+echarts如何實現(xiàn)多個儀表盤左右滾動效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI