123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <template>
- <view class="container">
- <div class="moreTime">
- <div v-for="(item,index) in range" :key="index" @click="change(index)" :class="choiceTime===index?'bod':''">
- {{item}}
- </div>
- </div>
- <div class="childTime">
- <div v-for="(item,index) in childTime" v-show="choiceTime===index">
- <template>
- <template v-for="(o,i) in item">
- <div @click="getTime(i,o.value)" :class="choiceChildTime===i?'box':''">{{o.text}}</div>
- </template>
- </template>
- </div>
- </div>
- <view>
- <div id="echart_demo" style="height: 400px;">
- </div>
- </view>
- </view>
- </template>
- <script>
- import {
- nowDate,
- timeToDate
- } from '../../utils/date.js'
- import echart from '../echarts/echarts-uniapp.vue';
- import * as echarts from 'echarts';
- export default {
- components: {
- echart,
- },
- props: {
- //图表类型 bar,line,pie,table
- chartType: {
- type: String,
- default: 'bar'
- },
- //图表高度
- height: {
- type: String,
- default: '400px'
- },
- //后台定义的图表id
- chartId: {
- type: String,
- default: null
- }
- },
- data() {
- return {
- chartData: {},
- option: {},
- range: ["分", "时", "日", "周"],
- childTime: [
- [{
- value: 900000,
- text: "15分"
- },
- {
- value: 1800000,
- text: "30分"
- },
- {
- value: 2700000,
- text: "45分"
- }
- ],
- [{
- value: 3600000,
- text: "1小时"
- },
- {
- value: 21600000,
- text: "6小时"
- },
- {
- value: 43200000,
- text: "12小时"
- },
- {
- value: 64800000,
- text: "18小时"
- }
- ],
- [{
- value: 86400000,
- text: "1日"
- },
- {
- value: 259200000,
- text: "3日"
- },
- {
- value: 432000000,
- text: "5日"
- }
- ],
- [{
- value: 604800000,
- text: "1周"
- },
- {
- value: 1209600000,
- text: "2周"
- },
- {
- value: 1814400000,
- text: "3周"
- },
- {
- value: 2419200000,
- text: "4周"
- }
- ],
- ],
- startTime: null,
- choiceTime: 0,
- choiceChildTime: 0,
- };
- },
- created() {
- this.startTime = timeToDate(new Date().getTime() - 900000)
- this.getServerData()
- },
- mounted() {
- //this.defineEcharts()
- },
- methods: {
- defineEcharts() {
- var chartDom = document.getElementById("echart_demo");
- //console.log(chartDom)
- var chart = echarts.init(chartDom)
- let option = {
- xAxis: {
- type: 'category',
- data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
- },
- yAxis: {
- type: 'value'
- },
- dataZoom: [
- {
- type: 'slider',
- show: true,
- //xAxisIndex: [0],
- start: 90,
- end: 100
- },
-
- ],
- series: [{
- data: [120, 200, 150, 80, 70, 110, 130],
- type: 'bar'
- }]
- };
- chart.setOption(option)
- console.log('------------------------')
- console.log(chart.getOption())
- },
- //分时日周选择
- change(i) {
- this.choiceTime === i ? this.choiceTime = -1 : this.choiceTime = i
- this.choiceChildTime = -1
- },
- //二级时间选择
- getTime(i, val) {
- this.choiceChildTime = i
- this.startTime = timeToDate(new Date().getTime() - val)
- this.getServerData();
- },
- //获得图表数据
- getServerData() {
- let id = this.chartId
- let startTime = this.startTime
- let endTime = timeToDate(new Date().getTime())
- uni.$http.get(`/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`).then(res => {
- const data = res.data.data;
- const chartData = data.chartItemList
- //x轴数据,时间轴
- const timeData = chartData[0].valueTimeList
- let optionData = this.getChartOption(this.chartType)
- optionData.title.text = data.chartName
- //y轴数据,数据轴
- if (this.chartType == 'bar' || this.chartType == 'line') {
- const series = chartData.map(item => {
- return {
- // name: item.describe ? item.describe : item.itemName,
- name: item.describe ? item.describe : item.itemReadName,
- data: item.valueList,
- type: this.chartType
- }
- })
- optionData.xAxis.data = timeData
- optionData.series = series
- } else if (this.chartType == 'pie') {
- }
- this.option = optionData
- console.log(this.option)
- });
- },
- //获得图表option数据
- getChartOption(cType) {
- let option = {}
- if (cType == 'bar') {
- option = {
- title: {
- text: '',
- textStyle: {
- color: '#666666',
- fontWeight: 'normal',
- fontSize: '14'
- }
- },
- xAxis: {
- type: 'category',
- data: [],
- axisLabel: {
- formatter: function(value) {
- return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
- '')
- }
- }
- },
- yAxis: {
- type: 'value'
- },
- legend: {
- type: 'scroll',
- top: 30
- },
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'cross',
- lineStyle: {
- type: 'dashed'
- },
- label: {
- show: false
- }
- },
- textStyle: {
- color: '#ffffff'
- },
- confine: true,
- extraCssText: 'white-space: normal; word-break: break-all;',
- backgroundColor: 'rgba(108,106,120,0.9)',
- position: 'inside',
- formatter: function(params) {
- let result = ''
- for (let i in params) {
- let value = '--'
- if (params[i].data !== null) {
- value = params[i].data
- }
- result += '<br>' + params[i].seriesName + ':' +
- value
- }
- return result
- }
- },
- dataZoom: [
- {
- type: 'slider',
- show: true,
- //xAxisIndex: [0],
- start: 1,
- end: 35
- },
-
- ],
- series: []
- };
- } else if (cType == 'line') {
- option = {
- title: {
- text: '',
- textStyle: {
- color: '#666666',
- fontWeight: 'normal',
- fontSize: '14'
- }
- },
- xAxis: {
- type: 'category',
- data: [],
- axisLabel: {
- formatter: function(value) {
- return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join(
- '')
- }
- }
- },
- yAxis: {
- type: 'value'
- },
- legend: {
- type: 'scroll',
- top: 30
- },
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'cross',
- lineStyle: {
- type: 'dashed'
- },
- label: {
- show: false
- },
- },
- textStyle: {
- color: '#ffffff'
- },
- confine: true,
- extraCssText: 'white-space: normal; word-break: break-all;',
- backgroundColor: 'rgba(108,106,120,0.9)', //通过设置rgba调节背景颜色与透明度
- position: 'inside',
- formatter: function(params) {
- let result = ''
- for (let i in params) {
- let value = '--'
- if (params[i].data !== null) {
- value = params[i].data
- }
- result += '<br>' + params[i].seriesName + ':' +
- value
- }
- return result
- }
- },
- dataZoom: [{
- type: 'inside',
- start: 90,
- end: 100
- },
- {
- type: 'slider',
- show: true,
- start: 90,
- end: 100,
- bottom: 10
- }
- ],
- series: []
- };
- } else if (cType == 'pie') {
- option = {
- title: {
- text: '',
- textStyle: {
- color: '#666666',
- fontWeight: 'normal',
- fontSize: '14'
- }
- },
- legend: {
- type: 'scroll',
- orient: 'vertical',
- left: 'left',
- top: 20
- },
- tooltip: {
- trigger: 'item',
- confine: true,
- extraCssText: 'white-space: normal; word-break: break-all;',
- backgroundColor: 'rgba(108,106,120,0.9)',
- textStyle: {
- color: '#ffffff'
- },
- },
- series: []
- };
- }
- return option
- }
- }
- };
- </script>
- <script module="myecharts" lang="renderjs">
- let myChart
- export default {
- mounted() {
- console.log('bbbbbbbbb')
- this.initEcharts()
- if (typeof window.echarts === 'function') {
-
- this.initEcharts()
- } else {
- // 动态引入较大类库避免影响页面展示
- const script = document.createElement('script')
- // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
- script.src = './echarts.js'
- script.onload = this.initEcharts.bind(this)
- document.head.appendChild(script)
- }
- },
- methods: {
- initEcharts() {
- myChart = myecharts.init(document.getElementById('echart_demo'))
- console.log('aaaaaaaaa')
- // 观测更新的数据在 view 层可以直接访问到
- myChart.setOption(this.option)
- },
- updateEcharts(newValue, oldValue, ownerInstance, instance) {
- // 监听 service 层数据变更
- myChart.setOption(newValue)
- },
- onClick(event, ownerInstance) {
- // 调用 service 层的方法
- ownerInstance.callMethod('onViewClick', {
- test: 'test'
- })
- }
- }
- }
- </script>
- <style scoped>
- .moreTime {
- display: flex;
- justify-content: space-between;
- align-items: center;
- width: 80vw;
- margin: auto
- }
- .bod {
- border-bottom: 3px solid #007AFF;
- }
- .box {
- border: 1px solid #007AFF !important;
- }
- .childTime {
- margin: 16rpx 60rpx 0 60rpx;
- }
- .childTime>div {
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .childTime>div>div {
- width: 140rpx;
- padding: 10rpx;
- border: 1px solid #e6e6e6;
- border-radius: 16rpx;
- color: #7e7e7e;
- font-size: 26rpx;
- text-align: center;
- }
- </style>
|