12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <template>
- <div>
- <h4 :style="{ marginBottom: '20px' }" v-if="title">{{ title }}</h4>
- <v-chart v-if="dataSource.length>3" key="2" :forceFit="true" :onClick="handleClick" :height="height" :data="data">
- <v-tooltip ref="tooltip" style="display:none;" />
- <v-axis/>
- <v-legend/>
- <v-stack-bar :position="position" :color="this.xField" />
- </v-chart>
- <v-chart v-else key="1" :forceFit="true" :onClick="handleClick" :height="height" :data="data">
- <v-tooltip ref="tooltip" style="display:none;" />
- <v-axis/>
- <v-legend/>
- <v-stack-bar :position="position" :color="this.xField" :size="40" />
- </v-chart>
- </div>
- </template>
- <script>
- import { DataSet } from '@antv/data-set'
- import { ChartEventMixins } from './mixins/ChartMixins'
- export default {
- name: 'BarStack',
- mixins: [ChartEventMixins],
- props: {
- title: {
- type: String,
- default: ''
- },
- dataSource: {
- type: Array,
- default: () => [
- { type: 'Jeecg', 'Jan.': 18.9, 'Feb.': 28.8, 'Mar.': 39.3, 'Apr.': 81.4, 'May': 47, 'Jun.': 20.3, 'Jul.': 24, 'Aug.': 35.6 },
- { type: 'Jeebt', 'Jan.': 12.4, 'Feb.': 23.2, 'Mar.': 34.5, 'Apr.': 99.7, 'May': 52.6, 'Jun.': 35.5, 'Jul.': 37.4, 'Aug.': 42.4 }
- ]
- },
- fields: {
- type: Array,
- default: () => ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.']
- },
- // 别名,需要的格式:[{field:'name',alias:'姓名'}, {field:'sex',alias:'性别'}]
- aliases: {
- type: Array,
- default: () => []
- },
- height: {
- type: Number,
- default: 254
- },
- xField: {
- type: String,
- default: 'type'
- },
- yField: {
- type: String,
- default: ''
- }
- },
- data() {
- return {
- position: 'x*' + (this.yField ? this.yField : 'y')
- }
- },
- computed: {
- data() {
- if (this.yField) {
- return this.dataSource
- }
- const dv = new DataSet.View().source(this.dataSource)
- dv.transform({
- type: 'fold',
- fields: this.fields,
- key: 'x',
- value: 'y'
- })
- // bar 使用不了 - 和 / 所以替换下
- const rows = dv.rows.map(row => {
- if (typeof row.x === 'string') {
- row.x = row.x.replace(/[-/]/g, '_')
- }
- return row
- })
- // 替换别名
- rows.forEach(row => {
- for (const item of this.aliases) {
- if (item.field === row.x) {
- row.x = item.alias
- break
- }
- }
- })
- return rows
- }
- }
- }
- </script>
|