zhaojs
2023-06-21 dd13f704cd41f0f4d84bb48f5d216112250fe1a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
    <canvas v-if="canvasId" :id="canvasId" :canvasId="canvasId" :style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}"
     @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error">
    </canvas>
</template>
 
<script>
    import uCharts from './u-charts.js';
    var canvases = {};
    
    export default {
        props: {
            chartType: {
                required: true,
                type: String,
                default: 'column'
            },
            opts: {
                required: true,
                type: Object,
                default () {
                    return null;
                },
            },
            canvasId: {
                type: String,
                default: 'u-canvas',
            },
            cWidth: {
                default: 375,
            },
            cHeight: {
                default: 250,
            },
            pixelRatio: {
                type: Number,
                default: 1,
            },
        },
        mounted() {
            this.init();
        },
        methods: {
            init() {
                switch (this.chartType) {
                    case 'column':
                        this.initColumnChart();
                        break;
                    case 'line':
                        this.initLineChart();
                        break;
                    default:
                        break;
                }
            },
            initColumnChart() {
                canvases[this.canvasId] = new uCharts({
                    $this: this,
                    canvasId: this.canvasId,
                    type: 'column',
                    legend: true,
                    fontSize: 11,
                    background: '#FFFFFF',
                    pixelRatio: this.pixelRatio,
                    animation: true,
                    categories: this.opts.categories,
                    series: this.opts.series,
                    enableScroll: true,
                    xAxis: {
                        disableGrid: true,
                        itemCount: 4,
                        scrollShow: true
                    },
                    yAxis: {
                        //disabled:true
                    },
                    dataLabel: true,
                    width: this.cWidth * this.pixelRatio,
                    height: this.cHeight * this.pixelRatio,
                    extra: {
                        column: {
                            type: 'group',
                        }
                    }
                });
            },
            initLineChart() {
                canvases[this.canvasId] = new uCharts({
                    $this: this,
                    canvasId: this.canvasId,
                    type: 'line',
                    fontSize: 11,
                    legend: true,
                    dataLabel: false,
                    dataPointShape: true,
                    background: '#FFFFFF',
                    pixelRatio: this.pixelRatio,
                    categories: this.opts.categories,
                    series: this.opts.series,
                    animation: true,
                    enableScroll: true,
                    xAxis: {
                        type: 'grid',
                        gridColor: '#CCCCCC',
                        gridType: 'dash',
                        dashLength: 8,
                        itemCount: 4,
                        scrollShow: true
                    },
                    yAxis: {
                        gridType: 'dash',
                        gridColor: '#CCCCCC',
                        dashLength: 8,
                        splitNumber: 5,
                        min: 10,
                        max: 180,
                        format: (val) => {
                            return val.toFixed(0) + '元'
                        }
                    },
                    width: this.cWidth * this.pixelRatio,
                    height: this.cHeight * this.pixelRatio,
                    extra: {
                        line: {
                            type: 'straight'
                        }
                    }
                });
            },
            // 这里仅作为示例传入两个参数,cid为canvas-id,newdata为更新的数据,需要更多参数请自行修改
            changeData(cid,newdata) {
                canvases[cid].updateData({
                    series: newdata.series,
                    categories: newdata.categories
                });
            },
            touchStart(e) {
                canvases[this.canvasId].showToolTip(e, {
                    format: function(item, category) {
                        return category + ' ' + item.name + ':' + item.data
                    }
                });
                canvases[this.canvasId].scrollStart(e);
            },
            touchMove(e) {
                canvases[this.canvasId].scroll(e);
            },
            touchEnd(e) {
                canvases[this.canvasId].scrollEnd(e);
            },
            error(e) {
                console.log(e)
            }
        },
    };
</script>
 
<style scoped>
    .charts {
        width: 100%;
        height: 100%;
        flex: 1;
        background-color: #FFFFFF;
    }
</style>