123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- /*
- * <<
- * Davinci
- * ==
- * Copyright (C) 2016 - 2017 EDP
- * ==
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * >>
- */
- import { IChartProps } from '../../components/Chart'
- import { DEFAULT_SPLITER } from 'app/globalConstants'
- import {
- IFieldFormatConfig,
- getFormattedValue,
- FieldFormatTypes
- } from 'containers/Widget/components/Config/Format'
- import { decodeMetricName, getChartTooltipLabel } from '../../components/util'
- import {
- getDimetionAxisOption,
- getMetricAxisOption,
- getLabelOption,
- getLegendOption,
- getGridPositions,
- makeGrouped,
- getGroupedXaxis,
- getCartesianChartMetrics,
- getCartesianChartReferenceOptions
- } from './util'
- import {
- getStackName,
- EmptyStack
- } from 'containers/Widget/components/Config/Stack'
- const defaultTheme = require('assets/json/echartsThemes/default.project.json')
- const defaultThemeColors = defaultTheme.theme.color
- import { inGroupColorSort } from '../../components/Config/Sort/util'
- import { FieldSortTypes } from '../../components/Config/Sort'
- import ChartTypes from '../../config/chart/ChartTypes'
- export default function (chartProps: IChartProps, drillOptions) {
- const { data, cols, chartStyles, color, tip, references } = chartProps
- const {
- isDrilling,
- getDataDrillDetail,
- instance,
- selectedItems,
- callback
- } = drillOptions
- const metrics = getCartesianChartMetrics(chartProps.metrics)
- const { bar, label, legend, xAxis, yAxis, splitLine } = chartStyles
- const {
- barChart,
- border: barBorder,
- gap: barGap,
- width: barWidth,
- stack: stackConfig
- } = bar
- const {
- color: borderColor,
- width: borderWidth,
- type: borderType,
- radius: barBorderRadius
- } = barBorder
- const { on: turnOnStack, percentage } = stackConfig || EmptyStack
- const {
- showVerticalLine,
- verticalLineColor,
- verticalLineSize,
- verticalLineStyle,
- showHorizontalLine,
- horizontalLineColor,
- horizontalLineSize,
- horizontalLineStyle
- } = splitLine
- const labelOption = {
- label: {
- ...getLabelOption('bar', label, metrics, false, {
- formatter: (params) => {
- const { value, seriesId } = params
- const m = metrics.find(
- (m) =>
- m.name ===
- seriesId.split(`${DEFAULT_SPLITER}${DEFAULT_SPLITER}`)[0]
- )
- let format: IFieldFormatConfig = m.format
- let formattedValue = value
- if (percentage) {
- format = {
- formatType: FieldFormatTypes.Percentage,
- [FieldFormatTypes.Percentage]: {
- decimalPlaces: 0
- }
- }
- formattedValue /= 100
- }
- const formatted = getFormattedValue(formattedValue, format)
- return formatted
- }
- })
- }
- }
- const referenceOptions = getCartesianChartReferenceOptions(
- references,
- ChartTypes.Bar,
- metrics,
- data,
- barChart
- )
- const xAxisColumnName = cols.length ? cols[0].name : ''
- let xAxisData = []
- let grouped = {}
- let percentGrouped = {}
- if (color.items.length) {
- xAxisData = getGroupedXaxis(data, xAxisColumnName, metrics)
- grouped = makeGrouped(
- data,
- color.items.map((c) => c.name),
- xAxisColumnName,
- metrics,
- xAxisData
- )
- const configValue = color.items[0].config.values
- const configKeys = []
- Object.entries(configValue).forEach(([k, v]: [string, string]) => {
- configKeys.push(k)
- })
- percentGrouped = makeGrouped(
- data,
- cols.map((c) => c.name),
- color.items[0].name,
- metrics,
- configKeys
- )
- } else {
- xAxisData = data.map((d) => d[xAxisColumnName] || '')
- }
- const series = []
- const seriesData = []
- metrics.forEach((m, i) => {
- const decodedMetricName = decodeMetricName(m.name)
- const stackOption = turnOnStack
- ? { stack: getStackName(m.name, stackConfig) }
- : null
- if (color.items.length) {
- const sumArr = []
- Object.entries(percentGrouped).forEach(([k, v]: [string, any[]]) => {
- sumArr.push(getColorDataSum(v, metrics))
- })
- const groupEntries = Object.entries(grouped)
- const customColorSort = color.items
- .filter(({ sort }) => sort && sort.sortType === FieldSortTypes.Custom)
- .map(({ name, sort }) => ({
- name,
- list: sort[FieldSortTypes.Custom].sortList
- }))
- if (customColorSort.length) {
- inGroupColorSort(groupEntries, customColorSort[0])
- }
- groupEntries.forEach(([k, v]: [string, any[]], gIndex) => {
- const serieObj = {
- id: `${m.name}${DEFAULT_SPLITER}${DEFAULT_SPLITER}${k}`,
- name: `${k}${metrics.length > 1 ? ` ${m.displayName}` : ''}`,
- type: 'bar',
- ...stackOption,
- sampling: 'average',
- data: v.map((g, index) => {
- if (
- selectedItems &&
- selectedItems.length &&
- selectedItems.some((item) => item === index)
- ) {
- return {
- value: percentage
- ? (g[`${m.agg}(${decodedMetricName})`] / sumArr[index]) * 100
- : g[`${m.agg}(${decodedMetricName})`],
- itemStyle: {
- normal: {
- opacity: 1
- }
- }
- }
- } else {
- if (percentage) {
- return (
- (g[`${m.agg}(${decodedMetricName})`] / sumArr[index]) * 100
- )
- } else {
- return g[`${m.agg}(${decodedMetricName})`]
- }
- }
- }),
- itemStyle: {
- normal: {
- opacity: selectedItems && selectedItems.length > 0 ? 0.25 : 1,
- color: color.items[0].config.values[k]
- }
- },
- ...labelOption,
- ...(gIndex === groupEntries.length - 1 &&
- i === metrics.length - 1 &&
- referenceOptions)
- }
- series.push(serieObj)
- seriesData.push(grouped[k])
- })
- } else {
- const serieObj = {
- id: m.name,
- name: m.displayName,
- type: 'bar',
- ...stackOption,
- sampling: 'average',
- data: data.map((d, index) => {
- if (
- selectedItems &&
- selectedItems.length &&
- selectedItems.some((item) => item === index)
- ) {
- return {
- value: percentage
- ? (d[`${m.agg}(${decodedMetricName})`] /
- getDataSum(data, metrics)[index]) *
- 100
- : d[`${m.agg}(${decodedMetricName})`],
- itemStyle: {
- normal: {
- opacity: 1
- }
- }
- }
- } else {
- if (percentage) {
- return (
- (d[`${m.agg}(${decodedMetricName})`] /
- getDataSum(data, metrics)[index]) *
- 100
- )
- } else {
- return d[`${m.agg}(${decodedMetricName})`]
- }
- }
- }),
- itemStyle: {
- normal: {
- opacity: selectedItems && selectedItems.length > 0 ? 0.25 : 1,
- borderColor,
- borderWidth,
- borderType,
- barBorderRadius,
- color:
- color.value[m.name] ||
- defaultThemeColors[i % defaultThemeColors.length]
- }
- },
- barGap: `${barGap}%`,
- barWidth: barWidth ? `${barWidth}%` : undefined,
- // lineStyle: {
- // normal: {
- // opacity: interactIndex === undefined ? 1 : 0.25
- // }
- // },
- // itemStyle: {
- // normal: {
- // opacity: interactIndex === undefined ? 1 : 0.25
- // color: color.value[m.name] || defaultThemeColors[i]
- // }
- // },
- ...labelOption,
- ...(i === metrics.length - 1 && referenceOptions)
- }
- series.push(serieObj)
- seriesData.push([...data])
- }
- })
- const seriesNames = series.map((s) => s.name)
- if (turnOnStack && stackConfig.sum.show) {
- const {
- fontFamily,
- fontStyle,
- fontColor,
- fontSize,
- fontWeight
- } = stackConfig.sum.font
- const sumSeries = series.reduce((acc, serie, serieIdx) => {
- const stackName = serie.stack
- if (acc[stackName]) {
- return acc
- }
- acc[stackName] = {
- name: stackName,
- type: 'bar',
- stack: stackName,
- label: {
- normal: {
- show: true,
- color: fontColor,
- fontStyle,
- fontWeight,
- fontFamily,
- fontSize,
- position: barChart ? 'right' : 'top',
- formatter: (params) => {
- let val = series
- .filter((s) => s.stack === stackName)
- .reduce((acc, s) => {
- const dataIndex = params.dataIndex
- if (typeof s.data[dataIndex] === 'number') {
- return acc + s.data[params.dataIndex]
- } else {
- const { value } = s.data[dataIndex]
- return acc + value
- }
- }, 0)
- let format = metrics[serieIdx].format
- if (percentage) {
- format = {
- formatType: FieldFormatTypes.Percentage,
- [FieldFormatTypes.Percentage]: {
- decimalPlaces: 0
- }
- }
- val /= 100
- }
- const formattedValue = getFormattedValue(val, format)
- return formattedValue
- }
- }
- },
- data: Array.from(xAxisData).fill(0)
- }
- return acc
- }, {})
- series.push(...Object.values(sumSeries))
- }
- const brushedOptions =
- isDrilling === true
- ? {
- brush: {
- toolbox: ['rect', 'polygon', 'keep', 'clear'],
- throttleType: 'debounce',
- throttleDelay: 300,
- brushStyle: {
- borderWidth: 1,
- color: 'rgba(255,255,255,0.2)',
- borderColor: 'rgba(120,140,180,0.6)'
- }
- }
- }
- : null
- // if (isDrilling) {
- // // instance.off('brushselected')
- // instance.on('brushselected', brushselected)
- // setTimeout(() => {
- // instance.dispatchAction({
- // type: 'takeGlobalCursor',
- // key: 'brush',
- // brushOption: {
- // brushType: 'rect',
- // brushMode: 'multiple'
- // }
- // })
- // }, 0)
- // }
- if (callback) {
- callback.call(null, seriesData)
- }
- function brushselected(params) {
- const brushComponent = params.batch[0]
- const brushed = []
- const sourceData = seriesData[0]
- let range: any[] = []
- if (brushComponent && brushComponent.areas && brushComponent.areas.length) {
- brushComponent.areas.forEach((area) => {
- range = range.concat(area.range)
- })
- }
- if (
- brushComponent &&
- brushComponent.selected &&
- brushComponent.selected.length
- ) {
- for (let i = 0; i < brushComponent.selected.length; i++) {
- const rawIndices = brushComponent.selected[i].dataIndex
- const seriesIndex = brushComponent.selected[i].seriesIndex
- brushed.push({ [i]: rawIndices })
- }
- }
- if (getDataDrillDetail) {
- getDataDrillDetail(JSON.stringify({ range, brushed, sourceData }))
- }
- }
- const xAxisSplitLineConfig = {
- showLine: showVerticalLine,
- lineColor: verticalLineColor,
- lineSize: verticalLineSize,
- lineStyle: verticalLineStyle
- }
- const yAxisSplitLineConfig = {
- showLine: showHorizontalLine,
- lineColor: horizontalLineColor,
- lineSize: horizontalLineSize,
- lineStyle: horizontalLineStyle
- }
- const dimetionAxisOption = getDimetionAxisOption(
- xAxis,
- xAxisSplitLineConfig,
- xAxisData
- )
- const metricAxisOption = getMetricAxisOption(
- yAxis,
- yAxisSplitLineConfig,
- metrics.map((m) => decodeMetricName(m.name)).join(` / `),
- 'x',
- percentage
- )
- return {
- xAxis: barChart ? metricAxisOption : dimetionAxisOption,
- yAxis: barChart ? dimetionAxisOption : metricAxisOption,
- series,
- tooltip: {
- formatter: getChartTooltipLabel('bar', seriesData, {
- cols,
- metrics,
- color,
- tip
- })
- },
- legend: getLegendOption(legend, seriesNames),
- grid: getGridPositions(
- legend,
- seriesNames,
- '',
- barChart,
- yAxis,
- xAxis,
- xAxisData
- )
- // ...brushedOptions
- }
- }
- export function getDataSum(data, metrics) {
- const dataSum = data.map((d, index) => {
- const metricArr = []
- let maSum = 0
- metrics.forEach((m, i) => {
- const decodedMetricName = decodeMetricName(m.name)
- const metricName = d[`${m.agg}(${decodedMetricName})`]
- metricArr.push(metricName)
- if (metricArr.length === metrics.length) {
- metricArr.forEach((mr) => {
- maSum += mr
- })
- }
- })
- return maSum
- })
- return dataSum
- }
- export function getColorDataSum(data, metrics) {
- let maSum = 0
- const dataSum = data.map((d, index) => {
- let metricArr = 0
- metrics.forEach((m, i) => {
- const decodedMetricName = decodeMetricName(m.name)
- const metricName = d[`${m.agg}(${decodedMetricName})`]
- metricArr += metricName
- })
- return metricArr
- })
- dataSum.forEach((mr) => {
- maSum += mr
- })
- return maSum
- }
|