zoukankan      html  css  js  c++  java
  • 把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>

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

      

  • 相关阅读:
    WF4.0 Beta1 自定义跟踪
    WF4.0 Beta1 流程设计器与Activity Designer
    新版本工作流平台的 (二) 权限算法(组织结构部分)
    WF4.0 Beta1 WorkflowInvoker
    WF4.0 基础篇 (十) Collection 集合操作
    WF4.0 基础篇 (十五) TransactionScope 事物容器
    WF4.0 基础篇 (六) 数据的传递 Arguments 参数
    WF4B1 的Procedural Activity 之InvokeMethod , InvokeMethod<T> 使用
    WF4.0 Beta1 异常处理
    WF4.0 Beta1 变量 Variables
  • 原文地址:https://www.cnblogs.com/huzhuhua/p/13099115.html
Copyright © 2011-2022 走看看