line.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { IChartProps } from '../../components/Chart'
  21. import { DEFAULT_SPLITER } from 'app/globalConstants'
  22. import {
  23. decodeMetricName,
  24. getChartTooltipLabel
  25. } from '../../components/util'
  26. import {
  27. getDimetionAxisOption,
  28. getMetricAxisOption,
  29. getLabelOption,
  30. getLegendOption,
  31. getGridPositions,
  32. makeGrouped,
  33. getGroupedXaxis,
  34. getCartesianChartMetrics,
  35. getCartesianChartReferenceOptions
  36. } from './util'
  37. import { getFormattedValue } from '../../components/Config/Format'
  38. import ChartTypes from '../../config/chart/ChartTypes'
  39. const defaultTheme = require('assets/json/echartsThemes/default.project.json')
  40. const defaultThemeColors = defaultTheme.theme.color
  41. export default function (chartProps: IChartProps, drillOptions?: any) {
  42. const { data, cols, chartStyles, color, tip, references } = chartProps
  43. const metrics = getCartesianChartMetrics(chartProps.metrics)
  44. const { spec, xAxis, yAxis, splitLine, label, legend } = chartStyles
  45. const {
  46. showVerticalLine,
  47. verticalLineColor,
  48. verticalLineSize,
  49. verticalLineStyle,
  50. showHorizontalLine,
  51. horizontalLineColor,
  52. horizontalLineSize,
  53. horizontalLineStyle
  54. } = splitLine
  55. const { smooth, step } = spec
  56. const { selectedItems } = drillOptions
  57. const labelOption = {
  58. label: getLabelOption('line', label, metrics)
  59. }
  60. const referenceOptions = getCartesianChartReferenceOptions(references, ChartTypes.Line, metrics, data)
  61. const xAxisColumnName = cols[0].name
  62. let xAxisData = []
  63. let grouped = {}
  64. if (color.items.length) {
  65. xAxisData = getGroupedXaxis(data, xAxisColumnName, metrics)
  66. grouped = makeGrouped(
  67. data,
  68. color.items.map((c) => c.name),
  69. xAxisColumnName,
  70. metrics,
  71. xAxisData
  72. )
  73. } else {
  74. xAxisData = data.map((d) => d[xAxisColumnName] || '')
  75. }
  76. const series = []
  77. const seriesData = []
  78. metrics.forEach((m, i) => {
  79. const decodedMetricName = decodeMetricName(m.name)
  80. if (color.items.length) {
  81. const groupedEntries = Object.entries(grouped)
  82. groupedEntries.forEach(([k, v]: [string, any[]], gIndex) => {
  83. const serieObj = {
  84. id: `${m.name}${DEFAULT_SPLITER}${DEFAULT_SPLITER}${k}`,
  85. name: `${k}${metrics.length > 1 ? ` ${m.displayName}` : ''}`,
  86. type: 'line',
  87. sampling: 'average',
  88. data: v.map((g, index) => {
  89. const itemStyleObj =
  90. selectedItems &&
  91. selectedItems.length &&
  92. selectedItems.some((item) => item === index)
  93. ? {
  94. itemStyle: {
  95. normal: {
  96. opacity: 1,
  97. borderWidth: 6
  98. }
  99. }
  100. }
  101. : {}
  102. // if (index === interactIndex) {
  103. // return {
  104. // value: g[m],
  105. // itemStyle: {
  106. // normal: {
  107. // opacity: 1
  108. // }
  109. // }
  110. // }
  111. // } else {
  112. // return g[`${m.agg}(${decodedMetricName})`]
  113. return {
  114. value: g[`${m.agg}(${decodedMetricName})`],
  115. ...itemStyleObj
  116. }
  117. // }
  118. }),
  119. itemStyle: {
  120. normal: {
  121. // opacity: interactIndex === undefined ? 1 : 0.25
  122. color: color.items[0].config.values[k],
  123. opacity: selectedItems && selectedItems.length > 0 ? 0.7 : 1
  124. }
  125. },
  126. smooth,
  127. step,
  128. ...labelOption,
  129. ...(gIndex === groupedEntries.length - 1 &&
  130. i === metrics.length - 1 &&
  131. referenceOptions)
  132. }
  133. series.push(serieObj)
  134. seriesData.push(grouped[k])
  135. })
  136. } else {
  137. const serieObj = {
  138. id: m.name,
  139. name: m.displayName,
  140. type: 'line',
  141. sampling: 'average',
  142. data: data.map((g, index) => {
  143. const itemStyleObj =
  144. selectedItems &&
  145. selectedItems.length &&
  146. selectedItems.some((item) => item === index)
  147. ? {
  148. itemStyle: {
  149. normal: {
  150. opacity: 1,
  151. borderWidth: 8
  152. }
  153. }
  154. }
  155. : {}
  156. // if (index === interactIndex) {
  157. // return {
  158. // value: d[m],
  159. // lineStyle: {
  160. // normal: {
  161. // opacity: 1
  162. // }
  163. // },
  164. // itemStyle: {
  165. // normal: {
  166. // opacity: 1
  167. // }
  168. // }
  169. // }
  170. // } else {
  171. return {
  172. value: g[`${m.agg}(${decodedMetricName})`],
  173. ...itemStyleObj
  174. }
  175. // }
  176. }),
  177. // lineStyle: {
  178. // normal: {
  179. // opacity: interactIndex === undefined ? 1 : 0.25
  180. // }
  181. // },
  182. itemStyle: {
  183. normal: {
  184. // opacity: interactIndex === undefined ? 1 : 0.25
  185. color: color.value[m.name] || defaultThemeColors[i],
  186. opacity: selectedItems && selectedItems.length > 0 ? 0.7 : 1
  187. }
  188. },
  189. smooth,
  190. step,
  191. ...labelOption,
  192. ...(i === metrics.length - 1 && referenceOptions)
  193. }
  194. series.push(serieObj)
  195. seriesData.push([...data])
  196. }
  197. })
  198. const seriesNames = series.map((s) => s.name)
  199. // dataZoomOptions = dataZoomThreshold > 0 && dataZoomThreshold < dataSource.length && {
  200. // dataZoom: [{
  201. // type: 'inside',
  202. // start: Math.round((1 - dataZoomThreshold / dataSource.length) * 100),
  203. // end: 100
  204. // }, {
  205. // start: Math.round((1 - dataZoomThreshold / dataSource.length) * 100),
  206. // end: 100,
  207. // handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
  208. // handleSize: '80%',
  209. // handleStyle: {
  210. // color: '#fff',
  211. // shadowBlur: 3,
  212. // shadowColor: 'rgba(0, 0, 0, 0.6)',
  213. // shadowOffsetX: 2,
  214. // shadowOffsetY: 2
  215. // }
  216. // }]
  217. // }
  218. const xAxisSplitLineConfig = {
  219. showLine: showVerticalLine,
  220. lineColor: verticalLineColor,
  221. lineSize: verticalLineSize,
  222. lineStyle: verticalLineStyle
  223. }
  224. const yAxisSplitLineConfig = {
  225. showLine: showHorizontalLine,
  226. lineColor: horizontalLineColor,
  227. lineSize: horizontalLineSize,
  228. lineStyle: horizontalLineStyle
  229. }
  230. const options = {
  231. xAxis: getDimetionAxisOption(xAxis, xAxisSplitLineConfig, xAxisData),
  232. yAxis: getMetricAxisOption(
  233. yAxis,
  234. yAxisSplitLineConfig,
  235. metrics.map((m) => decodeMetricName(m.name)).join(` / `)
  236. ),
  237. series,
  238. tooltip: {
  239. formatter: getChartTooltipLabel('line', seriesData, {
  240. cols,
  241. metrics,
  242. color,
  243. tip
  244. })
  245. },
  246. legend: getLegendOption(legend, seriesNames),
  247. grid: getGridPositions(
  248. legend,
  249. seriesNames,
  250. '',
  251. false,
  252. yAxis,
  253. xAxis,
  254. xAxisData
  255. )
  256. }
  257. return options
  258. }