123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <template>
- <view class="content">
- <!-- #ifdef APP-PLUS || H5 -->
- <view class="title"
- :style="`background-color: ${isYellow ? fuzhuColor50 : themeColor25};`">
- {{title}}
- </view>
- <view class="count">
- {{count}}
- </view>
- <!-- <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId" class="echarts"></view> -->
- <view :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId" class="echarts" v-show="fuzhuColor50!=null && dataList.length > 0"></view>
- <!-- #endif -->
- <!-- #ifndef APP-PLUS || H5 -->
- <view>非 APP、H5 环境不支持</view>
- <!-- #endif -->
- </view>
- </template>
- <script>
- import echarts from '../../static/echarts.js'
-
- export default {
- props: {
- chartId: String,
- title: String,
- count: Number,
- dataList: Array,
- // dataList: {
- // type: Array,
- // default:()=> {
- // return [1, 2, 1, 1]
- // }
- // },
- isYellow: Boolean,
- themeColor25: String,
- fuzhuColor50: String,
- },
- data() {
- return {
- option: {
- xAxis: {
- show: false,
- type: 'category',
- // boundaryGap: false
- boundaryGap: ['20%', '20%']
- },
- yAxis: {
- show: false,
- type: 'value',
- boundaryGap: ['70%', '70%']
- // boundaryGap: false
- },
- series: [
- {
- type: 'line',
- smooth: 0.5,
- symbol: 'none',
- lineStyle: {
- color: null,
- // opacity: 0.6,
- width: 3
- },
- data: []
- }
- ]
- }
- }
- },
-
- created() {
- // this.option.series[0].data = []
- },
-
- methods: {
- getDataAction() {
- this.option.series[0].data = this.dataList
- // // console.log('图表数据打印:', this.chartId, this.dataList)
- this.option.series[0].lineStyle.color = this.isYellow ? this.fuzhuColor50 : this.themeColor25
- // // console.log('数据刷新', this.option.series)
- },
- clickAction() {
- // // console.log("clickAction -> this.pageName", this.pageName)
- this.$emit('homeChartItemAction', this.title)
- },
- changeOption() {
- const data = this.option.series[0].data
- // 随机更新示例数据
- data.forEach((item, index) => {
- data.splice(index, 1, Math.random() * 40)
- })
- },
- onViewClick(options) {
- // console.log(options)
- }
- },
- watch: {
- chartId: {
- handler(e1, e2) {
- if (e1 != e2) {
- this.getDataAction()
- }
- },
- immediate: true
- },
- dataList: {
- handler(e1, e2) {
- var e1Str = JSON.stringify(e1)
- var e2Str = JSON.stringify(e2)
- if (e1Str && e1Str != e2Str) {
- this.getDataAction()
- }
- },
- deep: true,
- immediate: true
- },
- themeColor25: {
- handler(e) {
- if (e) {
- this.option.series[0].lineStyle.color = this.isYellow ? this.fuzhuColor50 : this.themeColor25
- }
- },
- immediate: true
- }
- }
-
- }
- </script>
- <script module="echarts" lang="renderjs">
- let myChart
- export default {
- // props: {
- // chartId: String
- // },
- created() {
- this.initAction()
- },
- mounted() {
- // this.initAction()
- },
- methods: {
- initAction() {
- if (typeof window.echarts === 'function') {
- this.initEcharts()
- } else {
- // 动态引入较大类库避免影响页面展示
- const script = document.createElement('script')
- // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
- script.src = 'static/echarts.js'
- script.onload = this.initEcharts.bind(this)
- document.head.appendChild(script)
- }
- },
- initEcharts() {
- // console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id)
- if (document.getElementById(this.$refs.chartId.$el.id)) {
- myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id))
- // 观测更新的数据在 view 层可以直接访问到
- if (myChart) {
- myChart.setOption(this.option)
- }
- else {
- // // console.log("无chart11111 -> this.$refs", this.$refs.chartId.$el.id)
- this.initAction()
- }
- }
- },
- updateEcharts(newValue, oldValue, ownerInstance, instance) {
- if (myChart) {
- // 监听 service 层数据变更
- myChart.setOption(newValue)
- }
- else {
- // // console.log("无chart11111 -> this.$refs", this.$refs.chartId.$el.id)
- this.initAction()
- }
- },
- onClick(event, ownerInstance) {
- // 调用 service 层的方法
- ownerInstance.callMethod('onViewClick', {
- test: 'test'
- })
- }
- }
- }
- </script>
- <style>
- .content {
- width: 100%;
- height: 100%;
- }
-
- .title {
- font-size: 32rpx;
- font-family: Verdana;
- text-align: center;
- width: 176rpx;
- margin-left: calc((100% - 176rpx) / 2);
- line-height: 46rpx;
- /* background-color: #E1F4F4; */
- color: rgba(0, 0, 0, 0.65);
- margin-top: 28rpx;
- margin-left: 20rpx;
- border-radius: 8rpx;
- }
-
- .count {
- width: 100%;
- margin-top: 24rpx;
- height: 48rpx;
- line-height: 48rpx;
- text-align: center;
- font-family: Verdana;
- font-size: 40rpx;
- color: rgba(32, 38, 56, 1);
- }
-
- .echarts {
- margin-top: 10rpx;
- width: 100%;
- height: 80rpx;
- }
- </style>
|