import Magix from 'magix' import * as View from '../../../../view' Magix.applyStyle('@./crowd-trend.less') export default View.extend({ tmpl: '@crowd-trend.html', init (options) { this.options = options }, render () { const { indicators, isModal, noChannels, inputKey, inputType } = this.options const selectedKey = indicators[0].key const selectedKeyVs = indicators[1].key this.updater.digest({ loading: true, inputType, inputKey, indicators, selectedKey, selectedKeyVs, showTrend: !noChannels, isModal }) this.renderTrend([selectedKey, selectedKeyVs]) }, async renderTrend (keys) { this.updater.digest({ loadingTrend: true }) const { indicators } = this.options const { Accounting } = this.updater.get() const chartOptions = this.getChartOptions(16) const indicatorsMap = Magix.toMap(indicators, 'key') const indicatorsNameMap = Magix.toMap(indicators, 'name') const trendData = await this.getTrendData(keys) const selfInd = indicatorsMap[keys[0]] // 当前指标 const peerInd = indicatorsMap[keys[1]] // 对比指标 const selfName = `${selfInd.name}` const peerName = `${peerInd.name}` for (const trend of trendData) { trend[selfName] = trend[selfInd.key] trend[peerName] = trend[peerInd.key] } // chartOptions.legend.activeEnabled = false chartOptions.graphs[0].field = selfName chartOptions.graphs[1].field = peerName chartOptions.tips = { content: (data, e) => { return `
${data.title}
${data.nodes.map(node => { const valueType = indicatorsNameMap[node.field].valueType let value = valueType == 2 ? `${Accounting.formatNumber(node.value * 100, 2)}%` : `${Accounting.formatNumber(node.value)}` if (valueType == 1) { value = Accounting.formatNumber(node.value, 2) } return `
${node.field}:${value}
` }).join('')} ` } } this.updater.digest({ chartOptions, trendData, loadingTrend: false }) }, 'showHideTrend' (e) { this.updater.digest({ showTrend: !this.updater.get('showTrend') }) }, async getTrendData (keys) { const { beginDate, endDate, id, deliverType, inputType, crowd } = this.options try { const params = { beginDate, endDate, indicators: keys, crowdId: id, deliverType } // 沉淀人群的特殊入参 if (inputType) { Object.assign(params, { reportType: 1, deliverType: crowd.channelId, crowdId: crowd.destTypeId }) } const models = [{ name: 'api_report_indicator_trend_get', params }] const [model] = await this.fetch(models) const data = model.get('data', []) return data } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, 'changeKey' (e) { const { selectedKey, selectedKeyVs } = this.updater.get() this.renderTrend([selectedKey, selectedKeyVs]) }, check: async () => { return { ok: true } } })