• 把ucharts 封装成组件


    最后的转Uni-app项目开发,要用图表,一个页面上有好几个图表,单个写的话,代码量大也麻烦。没办法。只能封装组件了;(本人的项目只用到柱状图和饼图)

    组件页面:

    <style lang="scss" scoped>
    #template {
         100%;
        height: 700rpx;
        .charts {
             100%;
            height: 100%;
        }
    }
    </style>
    <template>
        <view id="template"><canvas :canvas-id="canvasData.id" :id="canvasData.id" class="charts" @touchstart="touchColumn"></canvas></view>
    </template>
    <script>
    import uCharts from '@/components/u-charts/u-charts.js';
    var _self;//只要一个_self;
    export default {
        data() {
            return {
                cWidth: '',
                pixelRatio: 1,
                cHeight: '',
            };
        },
        props: ['canvasData'],
        name: 'mycanvas',
        methods: {
            //
            // 生成图表
            showColumn(chartData) {
                // 用props回的data添加一个属性用来生成canvas,以用来控制对应tooltip
                this.canvasData.canvas = new uCharts({
                    $this: _self,
                    canvasId: chartData.id,
                    type: chartData.type,
                    fontSize: 12,
                    background: '#FFFFFF',
                    pixelRatio: this.pixelRatio,
                    animation: true,
                    categories: chartData.categories,
                    series: chartData.series,
                    colors: ['#0b974e', '#ffcc00', '#2fce77', '#7030a0', '#ff0000', '#004a96'],
                    xAxis: {
                        disableGrid: true
                    },
                    yAxis: {
                        min: 0,
                        format: val => {
                            return val.toFixed(0);
                        },
                        titleFontColor: '#ff0000',
                        title: '日期'
                    },
                    legend: {
                        show: true,
                        position: 'top',
                        float: 'center',
                        itemGap: 10,
                        padding: 5,
                        lineHeight: 26,
                        margin: 5,
                        borderWidth: 1
                    },
                    padding: [5, 35, 5, 5],
                    dataLabel: true,
                     _self.cWidth,
                    height: _self.cHeight,
                    extra: {
                        column: {
                            type: 'group',
                             (_self.cWidth * _self.pixelRatio * 0.5) / chartData.series.length
                        },
                        pie: {
                            labelWidth: 15
                        }
                    }
                });
                
            },
            //
            // 点击图表显示tooltip
            touchColumn(e) {
                if (this.canvasData.type != 'pie') {
                    this.canvasData.canvas.showToolTip(e, {
                        textList: [{ text: '日期:' }, { text: 'OEE:' }],
                        format: function(item, category) {
                            this.textList = [
                                {
                                    text: item.name + ': ' + item.data.value + (item.name == 'OEE' ? '%' : '')
                                },
                                { text: '日期 : ' + item.data.day }
                            ];
                        }
                    });
                } else {
                    this.canvasData.canvas.showToolTip(e, {
                        textList: [],
                        format: function(item, category) {
                            let p = Math.round(item._proportion_ * 10000) / 100;
                            this.textList = [{ text: item.name + ' : ' + item.data + '分钟' }, { text: '占比 : ' + p + '%' }];
                        }
                    });
                }
            }
        },
        mounted() {
            _self = this;
            this.cWidth = uni.upx2px(750);
            this.cHeight = uni.upx2px(600);
        },
        watch: {
            'canvasData.day': {
                // 监听数据变化后重新生成图表
                handler(newName, oldName) {
                    setTimeout(() => {
                        this.showColumn(this.canvasData);
                    }, 100);
                },
                immediate: true,
                deep: true
            }
        }
    };
    </script>

    在页面引用

    template

    <u-canvas :canvasData="metreData"></u-canvas>

    JS里面

    import uCanvas from './canvas';
    export default {
        data() {
            return {
                metreData: { type: 'column', categories: [], series: [], id: 'metre', canvas: null, day: 0 },
            };
        },
        components: {
            uCanvas
        },
        methods: {
        },
        onLoad(item) {
    
        }
    };
    </script>

    这样把获取到的数据再赋值就可以了。还有如果 过多图表,建议加延迟。要不然容易 卡

      

  • 相关阅读:
    20162307 实验二实验报告
    20162307 第六周学习总结
    20162307 第5周学习总结
    20162306 2016-2017-2《程序设计与数据结构》第八周学习总结
    20162306 2016-2017-2《程序设计与数据结构》第七周学习总结
    第二次实验报告
    20162306 2016-2017-2《程序设计与数据结构》第六周学习总结
    20162306 2016-2017-2《程序设计与数据结构》第五周学习总结
    第一次实验报告
    20162306陈是奇 2016-2017-2《程序设计与数据结构》第四周学习总结
  • 原文地址:https://www.cnblogs.com/huzhuhua/p/13099115.html
Copyright © 2020-2023  润新知