| | |
| | | import asyncIconComponent from '@/utils/asyncIconComponent' |
| | | |
| | | import Utils from '@/utils/utils.js' |
| | | import { chartColors } from '@/utils/option.js' |
| | | import zhCN from '@/locales/zh-CN/model.js' |
| | | import enUS from '@/locales/en-US/model.js' |
| | | import './index.scss' |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 折线图 |
| | | */ |
| | | linerender = () => { |
| | | const { card } = this.state |
| | | let plot = {...card.plot, height: card.plot.height - 80} // 去除title所占空间 |
| | | let color = plot.color || 'rgba(0, 0, 0, 0.85)' |
| | | |
| | | let transfield = {} |
| | | card.columns.forEach(col => { |
| | | if (col.field) { |
| | | transfield[col.field] = col.label |
| | | } |
| | | }) |
| | | |
| | | let X_axis = plot.Xaxis || 'x' |
| | | let Y_axis = plot.Yaxis || ['y'] |
| | | |
| | |
| | | if (plot.enabled !== 'true') { |
| | | const ds = new DataSet() |
| | | const dv = ds.createView().source(data) |
| | | let transfield = {} |
| | | card.columns.forEach(col => { |
| | | if (col.field) { |
| | | transfield[col.field] = col.label |
| | | } |
| | | }) |
| | | |
| | | dv.transform({ |
| | | type: 'fold', |
| | |
| | | |
| | | chart.data(dv.rows) |
| | | |
| | | chart.axis(X_axis, { |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | line: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis('value', { |
| | | grid: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis(X_axis, { label: { style: { fill: color } }, line: { style: { fill: color } } }) |
| | | chart.axis('value', { grid: { style: { fill: color } }, label: { style: { fill: color } } }) |
| | | |
| | | if (plot.coordinate !== 'polar') { |
| | | chart.scale(X_axis, { |
| | |
| | | }) |
| | | } |
| | | chart.scale('value', { |
| | | nice: true |
| | | nice: true, |
| | | range: [0, 0.93] |
| | | }) |
| | | |
| | | if (!plot.legend || plot.legend === 'hidden') { |
| | |
| | | } else { |
| | | chart.legend({ |
| | | position: plot.legend, |
| | | itemName: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | itemName: { style: { fill: color } } |
| | | }) |
| | | } |
| | | |
| | |
| | | radius: 0.8 |
| | | }) |
| | | } |
| | | |
| | | let colors = new Map() |
| | | let colorIndex = 0 |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | plot.colors.forEach(item => { |
| | | if (!colors.has(transfield[item.type])) { |
| | | colors.set(transfield[item.type], item.color) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | let _chart = chart |
| | | .line() |
| | | .position(`${X_axis}*value`) |
| | | .color('key') |
| | | .shape(plot.shape || 'smooth') |
| | | .tooltip(`${X_axis}*value`, (name, value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | name: name, |
| | | value: value |
| | | } |
| | | }) |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | let limit = chartColors.length |
| | | _chart.color('key', (key) => { |
| | | if (colors.get(key)) { |
| | | return colors.get(key) |
| | | } else { |
| | | colors.set(key, chartColors[colorIndex % limit]) |
| | | colorIndex++ |
| | | } |
| | | }) |
| | | } else { |
| | | _chart.color('key') |
| | | } |
| | | if (plot.label === 'true') { |
| | | _chart.label('value') |
| | | _chart.label('value', (value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | content: value, |
| | | style: { |
| | | fill: color |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | if (plot.point === 'true') { |
| | |
| | | } |
| | | chart.render() |
| | | } else { |
| | | this.customrender(data, transfield) |
| | | this.customrender(data) |
| | | } |
| | | } |
| | | |
| | | customrender = (data, transfield) => { |
| | | const { card } = this.state |
| | | /** |
| | | * @description 自定义图 |
| | | */ |
| | | customrender = (data) => { |
| | | let card = fromJS(this.state.card).toJS() |
| | | let plot = {...card.plot, height: card.plot.height - 80} // 去除title所占空间 |
| | | let color = plot.color || 'rgba(0, 0, 0, 0.85)' |
| | | |
| | | let barfields = [] |
| | | let fields = [] |
| | | let legends = [] |
| | | let transfield = {} |
| | | |
| | | card.columns.forEach(col => { |
| | | if (col.field) { |
| | | transfield[col.field] = col.label |
| | | } |
| | | }) |
| | | |
| | | let colors = new Map() |
| | | let colorIndex = 0 |
| | | let limit = chartColors.length |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | plot.colors.forEach(item => { |
| | | if (!colors.has(item.type)) { |
| | | colors.set(item.type, item.color) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | let axisIndex = 0 |
| | | let hasBar = false |
| | | |
| | | plot.customs.forEach(item => { |
| | | item.name = transfield[item.field] || item.field |
| | | if (item.axis === 'left') { |
| | | item.index = 0 |
| | | } else if (item.axis === 'right') { |
| | | item.index = 1 |
| | | item.name = transfield[item.type] || item.type |
| | | item.chartType = item.shape ? (item.shape[0] || 'bar') : 'bar' |
| | | item.shape = item.shape ? (item.shape[1] || '') : '' |
| | | |
| | | if (colors.get(item.type)) { |
| | | item.color = colors.get(item.type) |
| | | } else { |
| | | item.index = 2 |
| | | item.color = chartColors[colorIndex % limit] |
| | | colorIndex++ |
| | | } |
| | | |
| | | if (item.chartType === 'bar') { |
| | | barfields.push(item.field) |
| | | fields.unshift(item) |
| | | if (item.chartType === 'bar' && !hasBar) { |
| | | hasBar = true |
| | | } else if (item.chartType === 'bar') { |
| | | item.chartType = 'line' |
| | | item.shape = 'smooth' |
| | | } |
| | | |
| | | if (item.axis === 'true' && axisIndex < 2) { |
| | | if (axisIndex === 0) { |
| | | item.axis = { grid: {style: { fill: color }}, title: { style: { fill: color } }, label: {style: { fill: color }} } |
| | | fields.unshift(item) |
| | | } else { |
| | | item.axis = { grid: null, title: {style: { fill: color }}, label: {style: { fill: color }} } |
| | | fields.splice(1, 0, item) |
| | | } |
| | | axisIndex++ |
| | | } else { |
| | | item.axis = { grid: null, title: null, label: null } |
| | | fields.push(item) |
| | | } |
| | | |
| | | |
| | | legends.push({ |
| | | value: item.name, |
| | | name: item.name, |
| | |
| | | }) |
| | | }) |
| | | |
| | | fields.sort((a, b) => a.index - b.index) |
| | | |
| | | const ds = new DataSet() |
| | | const dv = ds.createView().source(data) |
| | | dv.transform({ |
| | | type: 'map', |
| | | callback(row) { |
| | | fields.forEach(line => { |
| | | row[line.name] = row[line.field] |
| | | row[line.name] = row[line.type] |
| | | }) |
| | | return row |
| | | } |
| | | }) |
| | | |
| | | const chart = new Chart({ |
| | | container: plot.uuid, |
| | | container: card.uuid, |
| | | autoFit: true, |
| | | height: plot.height || 400 |
| | | }) |
| | | |
| | | chart.data(dv.rows) |
| | | |
| | | chart.axis(plot.Xaxis, { |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | line: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis('value', { |
| | | grid: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis(plot.Xaxis, { label: { style: { fill: color } }, line: { style: { fill: color } } }) |
| | | |
| | | if (plot.coordinate !== 'polar' && barfields.length === 0) { |
| | | if (!hasBar) { |
| | | chart.scale(plot.Xaxis, { |
| | | range: [0, 1] |
| | | }) |
| | |
| | | custom: true, |
| | | position: plot.legend, |
| | | items: legends, |
| | | itemName: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | itemName: { style: { fill: color } } |
| | | }) |
| | | } |
| | | |
| | |
| | | }) |
| | | } |
| | | |
| | | if (plot.transpose === 'true') { |
| | | chart.coordinate().transpose() |
| | | } |
| | | |
| | | if (plot.coordinate === 'polar') { |
| | | chart.coordinate('polar', { |
| | | innerRadius: 0.1, |
| | | radius: 0.8 |
| | | }) |
| | | } |
| | | |
| | | chart.scale({ |
| | | nice: true |
| | | }) |
| | | |
| | | fields.forEach((item, i) => { |
| | | if (i === 0) { |
| | | chart.axis(item.name, { |
| | | grid: {}, |
| | | title: {}, |
| | | label: {} |
| | | }) |
| | | } else if (i === 1 && item.axis !== 'unset') { |
| | | chart.axis(item.name, { |
| | | grid: null, |
| | | title: {}, |
| | | label: {} |
| | | }) |
| | | } else { |
| | | chart.axis(item.name, { |
| | | grid: null, |
| | | title: null, |
| | | label: null |
| | | }) |
| | | } |
| | | fields.forEach(item => { |
| | | chart.axis(item.name, item.axis) |
| | | |
| | | chart.scale(item.name, { |
| | | nice: true, |
| | | range: [0, 0.93] |
| | | }) |
| | | |
| | | if (item.chartType === 'bar') { |
| | | let _chart = chart |
| | | .interval() |
| | | .position(`${plot.Xaxis}*${item.name}`) |
| | | .color(item.color) |
| | | .shape(item.shape) |
| | | .tooltip(`${plot.Xaxis}*${item.name}`, (name, value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | name: name, |
| | | value: value |
| | | } |
| | | }) |
| | | |
| | | if (plot.barSize) { |
| | | _chart.size(plot.barSize || 35) |
| | | } |
| | | if (item.label === 'true') { |
| | | _chart.label(item.name) |
| | | _chart.label(item.name, (value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | content: value, |
| | | style: { |
| | | fill: color |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | } else if (item.chartType === 'line') { |
| | | let _chart = chart |
| | |
| | | .position(`${plot.Xaxis}*${item.name}`) |
| | | .color(item.color) |
| | | .shape(item.shape) |
| | | .tooltip(`${plot.Xaxis}*${item.name}`, (name, value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | name: name, |
| | | value: value |
| | | } |
| | | }) |
| | | |
| | | if (item.label === 'true') { |
| | | _chart.label(item.name) |
| | | _chart.label(item.name, (value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | content: value, |
| | | style: { |
| | | fill: color |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | if (plot.point === 'true') { |
| | |
| | | chart.render() |
| | | } |
| | | |
| | | /** |
| | | * @description 柱形图 |
| | | */ |
| | | barrender = () => { |
| | | const { card } = this.state |
| | | let plot = {...card.plot, height: card.plot.height - 80} |
| | | let color = plot.color || 'rgba(0, 0, 0, 0.85)' |
| | | |
| | | let transfield = {} |
| | | card.columns.forEach(col => { |
| | | if (col.field) { |
| | | transfield[col.field] = col.label |
| | | } |
| | | }) |
| | | let X_axis = plot.Xaxis || 'x' |
| | | let Y_axis = plot.Yaxis || ['y'] |
| | | |
| | |
| | | if (plot.enabled !== 'true') { |
| | | const ds = new DataSet() |
| | | const dv = ds.createView().source(data) |
| | | let transfield = {} |
| | | |
| | | card.columns.forEach(col => { |
| | | if (col.field) { |
| | | transfield[col.field] = col.label |
| | | } |
| | | }) |
| | | |
| | | dv.transform({ |
| | | type: 'fold', |
| | |
| | | |
| | | chart.data(dv.rows) |
| | | |
| | | chart.axis(X_axis, { |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | line: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis('value', { |
| | | grid: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | }, |
| | | label: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | }) |
| | | chart.axis(X_axis, { label: { style: { fill: color } }, line: { style: { fill: color } } }) |
| | | chart.axis('value', { grid: { style: { fill: color } }, label: { style: { fill: color } } }) |
| | | |
| | | chart.scale('value', { |
| | | nice: true |
| | | nice: true, |
| | | range: [0, 0.93] |
| | | }) |
| | | |
| | | if (!plot.legend || plot.legend === 'hidden') { |
| | |
| | | } else { |
| | | chart.legend({ |
| | | position: plot.legend, |
| | | itemName: { |
| | | style: { |
| | | fill: color, |
| | | } |
| | | } |
| | | itemName: { style: { fill: color } } |
| | | }) |
| | | } |
| | | |
| | |
| | | radius: 0.8 |
| | | }) |
| | | } |
| | | |
| | | let colors = new Map() |
| | | let colorIndex = 0 |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | plot.colors.forEach(item => { |
| | | if (!colors.has(transfield[item.type])) { |
| | | colors.set(transfield[item.type], item.color) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | if (plot.adjust !== 'stack') { |
| | | let _chart = chart |
| | | .interval() |
| | | .position(`${X_axis}*value`) |
| | | .color('key') |
| | | .adjust([ |
| | | { |
| | | type: 'dodge', |
| | |
| | | } |
| | | ]) |
| | | .shape(plot.shape || 'rect') |
| | | |
| | | .tooltip(`${X_axis}*value`, (name, value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | name: name, |
| | | value: value |
| | | } |
| | | }) |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | let limit = chartColors.length |
| | | _chart.color('key', (key) => { |
| | | if (colors.get(key)) { |
| | | return colors.get(key) |
| | | } else { |
| | | colors.set(key, chartColors[colorIndex % limit]) |
| | | colorIndex++ |
| | | } |
| | | }) |
| | | } else { |
| | | _chart.color('key') |
| | | } |
| | | if (plot.label === 'true') { |
| | | _chart.label('value') |
| | | _chart.label('value', (value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | content: value, |
| | | style: { |
| | | fill: color |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | if (plot.barSize || plot.correction) { |
| | |
| | | let _chart = chart |
| | | .interval() |
| | | .position(`${X_axis}*value`) |
| | | .color('key') |
| | | .adjust('stack') |
| | | .shape(plot.shape || 'rect') |
| | | .tooltip(`${X_axis}*value`, (name, value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | name: name, |
| | | value: value |
| | | } |
| | | }) |
| | | |
| | | if (plot.colors && plot.colors.length > 0) { |
| | | let limit = chartColors.length |
| | | _chart.color('key', (key) => { |
| | | if (colors.get(key)) { |
| | | return colors.get(key) |
| | | } else { |
| | | colors.set(key, chartColors[colorIndex % limit]) |
| | | colorIndex++ |
| | | } |
| | | }) |
| | | } else { |
| | | _chart.color('key') |
| | | } |
| | | if (plot.label === 'true') { |
| | | _chart.label('value') |
| | | _chart.label('value', (value) => { |
| | | if (plot.show === 'percent') { |
| | | value = value + '%' |
| | | } |
| | | return { |
| | | content: value, |
| | | style: { |
| | | fill: color |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | if (plot.barSize || plot.correction) { |
| | |
| | | |
| | | chart.render() |
| | | } else { |
| | | this.customrender(data, transfield) |
| | | this.customrender(data) |
| | | } |
| | | } |
| | | |