Explorar o código

完成移动端持仓分析

jwyu %!s(int64=2) %!d(string=hai) anos
pai
achega
1b12ccf33d

+ 21 - 0
src/api/hzyb/positionAnalysis.js

@@ -0,0 +1,21 @@
+// 持仓分析模块
+
+import {get,post} from './http'
+
+/**
+ * 持仓分析详情
+ * @param data_time 查询日期,日期为空时,默认返回最新日期
+ * @param classify_name 品种名称
+ * @param classify_type 合约名称
+ * @param exchange 交易所名称
+ */
+export const apiPositionAnalysisInfo=params=>{
+    return get('/trade/analysis/top',params)
+}
+
+/**
+ * 持仓分析列表
+ */
+export const apiPositionAnalysisList=()=>{
+    return get('/trade/analysis/classify',{})
+}

+ 304 - 4
src/views/hzyb/chart/positionAnalysis/Index.vue

@@ -1,24 +1,324 @@
 <script setup>
+import {reactive, ref,watch} from 'vue'
+import { useRoute, useRouter } from 'vue-router'
 import ChartBox from './components/ChartBox.vue'
+import moment from 'moment'
+import { Popup, Toast,DatetimePicker,PullRefresh } from 'vant';
+import {apiPositionAnalysisInfo,apiPositionAnalysisList} from '@/api/hzyb/positionAnalysis'
+
+const route=useRoute()
+const router=useRouter()
+localStorage.setItem('hzyb-token',route.query.token)
+
+
+// 选择时间
+let showDate=ref(false)
+const minDate=ref(new Date('2000/01/01'))
+let currentDate=ref('')//这是绑定在时间选择器的时间防止用户滑动了时间但是没点确定 这时候又把这个时间改成selectDate
+let selectDate=ref('')
+function confirmSelectDate(e){
+    // 如果选择的是 周六日 提示
+    if([0,6].includes(moment(currentDate.value).weekday())){
+        Toast('今日无数据,请选择其他日期!')
+        return
+    }
+    selectDate.value=e
+    currentDate.value=e
+    showDate.value=false
+    getInfo()
+}
+//选择时间弹窗关闭时更新 currentDate为selectDate
+function handleCloseSelectDate(){
+    // console.log('更新currentDate');
+    currentDate.value=selectDate.value
+}
+//切换 前一天\后一天 如果遇到周六日则跳过
+function handleDateChange(type){
+    let num=1 
+    if(type==='before'){
+        if(moment(selectDate.value).weekday()===1){//向前一天时 当前为周一则 跳到 上周五
+            num=3
+        }
+        currentDate.value=moment(currentDate.value).add(-num,'days')
+        selectDate.value=moment(selectDate.value).add(-num,'days')
+    }else{
+        if(moment(selectDate.value).weekday()===5){//向前一天时 当前为周五则 跳到 下周一
+            num=3
+        }
+        currentDate.value=moment(currentDate.value).add(num,'days')
+        selectDate.value=moment(selectDate.value).add(num,'days')
+    }
+    getInfo()
+}
+
+
+// 切换合约
+const allClassifyTypeList=[] //所有合约的数据
+function getAllClassifyType(){
+    apiPositionAnalysisList().then(res=>{
+        if(res.code===200){
+            const arr=res.data||[]
+            // 将数据展开
+            arr.forEach(item => {
+                item.items&&item.items.forEach(itemC1=>{
+                    itemC1.items&&itemC1.items.forEach(itemC2=>{
+                        allClassifyTypeList.push({
+                            exchange:item.exchange,
+                            classify_name:itemC1.classify_name,
+                            classify_type:itemC2.classify_type
+                        })
+                    })
+                })
+            });
+        }
+    })
+}
+getAllClassifyType()
+function handleClassifyTypeChange(type){
+    const currentExchange=route.query.exchange
+    const currentClassifyName=route.query.classify_name
+    const currentClassifyType=route.query.classify_type
+    // 找index
+    let indexNum=0
+    allClassifyTypeList.forEach((item,index)=>{
+        if(item.exchange===currentExchange&&item.classify_name===currentClassifyName&&item.classify_type===currentClassifyType) indexNum=index
+    })
+    // console.log(indexNum);
+    let obj={}
+    if(type==='before'){
+        obj=allClassifyTypeList[indexNum===0?allClassifyTypeList.length-1:indexNum-1]
+    }else{
+        obj=allClassifyTypeList[indexNum===allClassifyTypeList.length-1?0:indexNum+1]
+    }
+    // console.log(obj);
+
+    router.replace({
+        query:{
+            ...route.query,
+            exchange:obj.exchange,
+            classify_name:obj.classify_name,
+            classify_type:obj.classify_type
+        }
+    })
+    
+}
+watch(
+    ()=>route.query,
+    (n)=>{
+        selectDate.value=''
+        currentDate.value=''
+        getInfo()
+    }
+)
+
+
+// 获取详情
+const chartListState = reactive({
+    buy_list:{
+        name:'多单',
+        labelName:'持多单量',
+    },
+    sold_list:{
+        name:'空单',
+        labelName:'持空单量',
+    },
+    clean_buy_list:{
+        name:'净多单',
+        labelName:'净多单量',
+    },
+    clean_sold_list:{
+        name:'净空单',
+        labelName:'净空单量',
+    }
+})
+async function getInfo(){
+    const res=await apiPositionAnalysisInfo({
+        data_time:selectDate.value?moment(selectDate.value).format('YYYY-MM-DD'):'',
+        classify_name:route.query.classify_name,
+        classify_type:route.query.classify_type,
+        exchange:route.query.exchange
+    })
+    if(res.code===200){
+        const obj=res.data||{}
+        for (let key in chartListState) {
+            chartListState[key]={...chartListState[key],...obj[key]}
+        }
+        if(res.data.data_time){
+            selectDate.value=moment(res.data.data_time).toDate()
+            currentDate.value=moment(res.data.data_time).toDate()
+        }
+        document.title=`${route.query.classify_type} ${moment(selectDate.value).format('YYYYMMDD')}持仓`
+    }else{
+        // 清空数据
+        for (let key in chartListState) {
+            chartListState[key].list=null
+        }
+    }
+}
+getInfo()
+
+
+// 下拉刷新
+let isRefresh=ref(false)
+function onRefresh(){
+    getInfo()
+    setTimeout(()=>{    
+        isRefresh.value=false
+    },1500)
+}
+
+
+// 返回列表
+function handleBackList(){
+    wx.miniProgram.navigateBack()
+}
+
 
 </script>
 
 <template>
+    <PullRefresh v-model="isRefresh" @refresh="onRefresh">
     <div class="chart-position-analysis-page">
+        <div class="top-sticky-wrap">
+            <div class="notice-box">如无法滑动,请在左侧空白处尝试</div>
+            <div class="action-box">
+                <div 
+                    class="select-time-box"
+                    :style="{color:selectDate?'#333':'#999'}" 
+                    @click="showDate=true"
+                >{{selectDate?moment(selectDate).format('YYYY-MM-DD'):'选择日期'}}</div>
+                <span style="color:#E3B377" @click="handleDateChange('before')">前一天</span>
+                <span style="color:#E3B377" @click="handleDateChange('next')">后一天</span>
+            </div>
+        </div>
+        <div class="empty-wrap" v-if="!chartListState.buy_list.list">
+            <span>该日期无数据!</span>
+        </div>
+        <template v-else>
+        <div class="chart-wrap" v-for="(val,key) of chartListState" :key="key">
+            <div class="top-info-box">
+                <span>{{chartListState[key].name}}</span>
+                <span><span style="color:#999;margin-right:2px">总计 </span>{{chartListState[key].total_deal_value}}</span>
+                <span><span style="color:#999;margin-right:2px">较昨日 </span>{{chartListState[key].total_deal_change<0?'-':''}}{{chartListState[key].total_deal_change}}</span>
+            </div>
+            <chart-box :keyVal="key" :data="chartListState[key]"/>
+        </div>
+        </template>
 
-        <div class="chart-wrap">
-            <chart-box/>
+        <div class="bot-fixed-wrap">
+            <div class="btn black-btn" @click="handleClassifyTypeChange('before')">上个合约</div>
+            <div class="btn black-btn" @click="handleClassifyTypeChange('next')">下个合约</div>
+            <div class="btn grey-btn" @click="handleBackList">返回列表</div>
         </div>
 
     </div>
+    </PullRefresh>
+    <!-- 选择时间弹窗 -->
+    <Popup
+        v-model:show="showDate"
+        position="bottom"
+        round
+        @close="handleCloseSelectDate"
+    >
+        <DatetimePicker
+            v-model="currentDate"
+            type="date"
+            title=""
+            :min-date="minDate"
+            @confirm="confirmSelectDate"
+            @cancel="showDate=false"
+        />
+    </Popup>
+
 </template>
 
 <style lang="scss" scoped>
 .chart-position-analysis-page{
-    padding: 32px;
+    padding-bottom: calc(160px + constant(safe-area-inset-bottom));
+    padding-bottom: calc(160px + env(safe-area-inset-bottom));
+}
+.top-sticky-wrap{
+    position: sticky;
+    top: 0;
+    z-index: 99;
+    background-color: #fff;
+    .notice-box{
+        font-size: 24px;
+        background: rgba(207, 192, 159, 0.1);
+        padding: 15px 34px;
+    }
+    .action-box{
+        padding: 40px 60px 40px 34px;
+        display: flex;
+        align-items: center;
+        justify-content: space-between;
+        .select-time-box{
+            width: 360px;
+            height: 70px;
+            background: #F6F6F6;
+            border: 1px solid #E5E5E5;
+            border-radius: 35px;
+            position: relative;
+            padding-left: 80px;
+            line-height: 70px;
+            &::before{
+                content: '';
+                display: block;
+                width: 28px;
+                height: 28px;
+                background-image: url('@/assets/hzyb/chart/calendar.png');
+                background-size: cover;
+                position: absolute;
+                top: 20px;
+                left: 25px;
+            }
+        }
+    }
+}
+.bot-fixed-wrap{
+    position: fixed;
+    left: 0;
+    right: 0;
+    bottom: 0;
+    z-index: 99;
+    filter: drop-shadow(0px -2px 12px rgba(0, 0, 0, 0.08));
+    background-color: #fff;
+    padding-bottom: constant(safe-area-inset-bottom);
+    padding-bottom: env(safe-area-inset-bottom);
+    display: flex;
+    justify-content: space-between;
+    padding: 20px 34px;
+    .btn{
+        width: 30%;
+        height: 80px;
+        line-height: 80px;
+        text-align: center;
+        font-size: 28px;
+        border-radius: 40px;
+    }
+    .black-btn{
+        color: #E3B377;
+        background-color: #333333;
+    }
+    .grey-btn{
+        color: #666666;
+        background-color: #F5F5F5;
+    }
 }
 .chart-wrap{
     width: 100%;
-    height: 90vh;
+    margin-bottom: 60px;
+    .top-info-box{
+        padding: 0 34px 30px 34px;
+        text-align: center;
+        span{
+            display: inline-block;
+            margin-right: 50px;
+        }
+    }
+}
+.empty-wrap{
+    text-align: center;
+    padding-top: 300px;
 }
 </style>

+ 198 - 37
src/views/hzyb/chart/positionAnalysis/components/ChartBox.vue

@@ -1,11 +1,24 @@
 <script setup>
-import {ref,onMounted,watch,toRefs } from 'vue'
+import {ref,onMounted,watch } from 'vue'
+import { Popup } from 'vant';
 import Highcharts from 'highcharts';
 import HighchartsMore from 'highcharts/highcharts-more'
 import HighchartszhCN  from '../../../utils/highcahrts-zh_CN'
 HighchartszhCN(Highcharts)
 HighchartsMore(Highcharts)
 
+const props=defineProps({
+    keyVal:String,
+    data:Object
+})
+
+//配色表
+const colorMap=new Map([
+    ['buy_list',['#FFD600','#F32F2F','#52D424']],
+    ['sold_list',['#C6DDFF','#363EFF','#52D424']],
+    ['clean_buy_list',['#FFD600','#F32F2F','#52D424']],
+    ['clean_sold_list',['#C6DDFF','#363EFF','#52D424']],
+])
 //图表默认配置项
 const chartDefaultOpts={
     chart: {
@@ -20,22 +33,23 @@ const chartDefaultOpts={
     plotOptions:{
         columnrange: {
             grouping: false,//不分组 这样就能叠起来
+            pointPadding: 0,
             dataLabels: {
                 enabled: true,
                 inside:true,
-                // overflow:'allow',
-                // crop:false,
-                // y:-100,
+                align: 'left',
+                color: '#333',
                 formatter:function(e){
                     return this.point.options.isLabel
                 }
             },
             events: {
                 click: function (event) {
-                    console.log(event);
+                    const name=event.point.category
+                    showDetail(name)
                 }
             }
-        }
+        },
     },
     legend: {
 		enabled: false,//关闭图例
@@ -50,15 +64,57 @@ const chartDefaultOpts={
 //绘图
 function chartRender(){
     let options={}
+    let categories=[]
+    let series=[
+        {
+            name: '总数',
+            data: [],
+        },
+        {
+            name: '增长',
+            data: [],
+        },
+        {
+            name:'减少',
+            data:[]
+        }
+    ]
+    let totalArr=[],increaseArr=[],reduceArr=[];
+    props.data.list.forEach(item=>{
+        categories.push(item.deal_short_name)
+
+        // 处理series
+        totalArr.push({low:0,high:item.deal_value})
+        if(item.deal_change<0){//减少
+            increaseArr.push({low:0,high:0})
+            reduceArr.push({
+                low:item.deal_value,
+                high:item.deal_value-item.deal_change,
+                isLabel:`${item.deal_value}/${item.deal_change}`
+            })
+        }else{
+            reduceArr.push({low:0,high:0})
+            increaseArr.push({
+                low:item.deal_value-item.deal_change,
+                high:item.deal_value,
+                isLabel:`${item.deal_value}/+${item.deal_change}`
+            })
+        }
+        
+    })
+    series[0].data=totalArr
+    series[1].data=increaseArr
+    series[2].data=reduceArr
     let xAxis={
-        categories: ['国泰君安', '中信期货', '华泰期货', '中财期货', '广发期货', '方正中期', '富国期货', '永安期货', '中信建投', '国投安信'],
+        categories: categories,
         tickWidth:1,
         tickPosition:'outside',
-
     }
     let yAxis={
         opposite:true,
         gridLineWidth: 1,
+        gridLineColor:'#E4E4E4',
+        gridLineDashStyle: 'longdash',
         endOnTick: false,
         showLastLabel: true,
         lineWidth: 1,
@@ -68,38 +124,45 @@ function chartRender(){
           text: '',
         },
     }
-    let series=[
-        {
-            name: '总数',
-            data: [
-                {low:0, high:4890},
-                {low:0, high:0}
-            ],
-        },
-        {
-            name: '增长',
-            data: [
-                {low:4500, high:4890,isLabel:'4890 / +390'},
-                {low:0, high:0},
-            ],
-        },
-        {
-            name:'减少',
-            data:[
-                {low:0, high:0},
-                {low:0, high:497,isLabel:'0 / - 497'},
-            ]
-        }
-    ]
+    
+    options.colors=colorMap.get(props.keyVal)
     options.xAxis=xAxis
     options.yAxis=yAxis
     options.series=series
     options={...chartDefaultOpts,...options}
-    console.log(options);
-    Highcharts.chart("chart-box",options)
+    console.log('渲染',options);
+    Highcharts.chart(`chart-box-${props.keyVal}`,options)
 }
+
+
+//点击柱子显示弹窗
+let showDetailPop=ref(false)
+let showDetailData=ref(null)
+function showDetail(name){
+    let data={}
+    props.data.list.forEach(item=>{
+        if(item.deal_short_name===name){
+            data=item
+        }
+    })
+    showDetailData.value=data
+    showDetailPop.value=true
+}
+
+
 onMounted(()=>{
-    chartRender()
+    watch(
+        ()=>props.data,
+        (n)=>{
+            if(n.list){
+                chartRender()
+            }
+        },
+        {
+            deep:true,
+            immediate:true
+        }
+    )
 })
 
 
@@ -107,21 +170,85 @@ onMounted(()=>{
 
 <template>
     <div class="chart-render-box">
+        <div class="label-box">
+            <div>
+                <span class="color-box" :style="{background:colorMap.get(keyVal)[0]}"></span>
+                <span>{{data.labelName}}</span>
+            </div>
+            <div>
+                <span class="color-box" :style="{background:colorMap.get(keyVal)[1]}"></span>
+                <span>增</span>
+            </div>
+            <div>
+                <span class="color-box" :style="{background:colorMap.get(keyVal)[2]}"></span>
+                <span>减</span>
+            </div>
+        </div>
         <div class="chart-content">
 			<img class="mark-img" src="../../../../../assets/hzyb/chart/mark.png" alt="">
-			<div class="chart-box" id="chart-box"></div>
+			<div class="chart-box" :id="'chart-box-'+keyVal"></div>
 		</div>
+        
     </div>
+    <!-- 详情弹窗 -->
+    <Popup 
+        v-model:show="showDetailPop"
+        round
+    >
+        <div class="mobile-detail-pop-wrap">
+            <h2 class="title">{{showDetailData.deal_short_name}}</h2>
+            <div class="table-box">
+                <div class="item">
+                    <div>持{{data.name}}</div>
+                    <div>{{showDetailData.deal_value}}</div>
+                </div>
+                <div class="item">
+                    <div>增减</div>
+                    <div>{{showDetailData.deal_change}}</div>
+                </div>
+                <div class="item">
+                    <div>占比</div>
+                    <div>{{showDetailData.rate*100}}%</div>
+                </div>
+                <div class="item">
+                    <div>前6名持净多单</div>
+                    <div>{{showDetailData.before_all_value}}</div>
+                </div>
+                <div class="item">
+                    <div>增减</div>
+                    <div>{{showDetailData.before_all_change}}</div>
+                </div>
+                <div class="item">
+                    <div>占比</div>
+                    <div>{{showDetailData.before_all_rate*100}}%</div>
+                </div>
+            </div>
+            <div class="close-btn" @click="showDetailPop=false">关闭</div>
+        </div>
+    </Popup>
 </template>
 
 <style lang="scss" scoped>
 .chart-render-box{
     width: 100%;
-    height: 100%;
     overflow: hidden;
+    .label-box{
+        display: flex;
+        justify-content: center;
+        div{
+            margin-right: 30px;
+        }
+        .color-box{
+            display: inline-block;
+            width: 22px;
+            height: 22px;
+            margin-right: 10px;
+            border-radius: 4px;
+        }
+    }
     .chart-content{
         width: 100%;
-        height: 100%;
+        height: 70vh;
         position: relative;
 		.mark-img{
 			position: absolute;
@@ -138,4 +265,38 @@ onMounted(()=>{
         }
     }
 }
+.mobile-detail-pop-wrap{
+    width: 90vw;
+    .title{
+        text-align: center;
+        font-size: 32px;
+        padding: 30px;
+    }
+    .table-box{
+        display: flex;
+        flex-wrap: wrap;
+        .item{
+            width: 33.33%;
+            text-align: center;
+            padding: 30px 20px;
+            border-right: 1px solid #E5E5E5;
+            border-bottom: 1px solid #E5E5E5;
+            div:first-child{
+                color: #999999;
+                font-size: 24px;
+            }
+            div:last-child{
+                font-size: 32px;
+                font-weight: 600;
+            }
+        }
+        
+
+    }
+    .close-btn{
+        text-align: center;
+        line-height: 96px;
+        color: #E3B377;
+    }
+}
 </style>