Chart.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 React, { useContext, useMemo, useCallback, useEffect } from 'react'
  21. import Widget, { IPaginationParams } from 'containers/Widget/components/Widget'
  22. import { LayerListContext, LayerContext } from '../util'
  23. import { SlideContext } from 'containers/Display/components/Container'
  24. const Chart: React.FC = () => {
  25. const { slideId } = useContext(SlideContext)
  26. const {
  27. currentDisplayWidgets,
  28. getWidgetViewModel,
  29. getChartData
  30. } = useContext(LayerListContext)
  31. const {
  32. layer: {
  33. id: layerId,
  34. widgetId,
  35. params: { polling, frequency }
  36. },
  37. layerInfo,
  38. operationInfo
  39. } = useContext(LayerContext)
  40. const widget = currentDisplayWidgets[widgetId]
  41. const { datasource, renderType, queryConditions, loading } = layerInfo
  42. const data = datasource.resultList || []
  43. const nativeQuery = useMemo(() => {
  44. let noAggregators = false
  45. const { chartStyles } = widget.config
  46. const { table } = chartStyles
  47. if (table) {
  48. noAggregators = table.withNoAggregators
  49. }
  50. return noAggregators
  51. }, [widget.config])
  52. const pagination = useMemo(() => {
  53. const { chartStyles } = widget.config
  54. const { table } = chartStyles
  55. if (!table) {
  56. return null
  57. }
  58. const { withPaging, pageSize } = table
  59. const pagination: IPaginationParams = {
  60. withPaging,
  61. pageSize: 0,
  62. pageNo: 0,
  63. totalCount: datasource.totalCount || 0
  64. }
  65. if (pagination.withPaging) {
  66. pagination.pageSize = datasource.pageSize || +pageSize
  67. pagination.pageNo = datasource.pageNo || 1
  68. }
  69. return pagination
  70. }, [widget.config, datasource.totalCount])
  71. useEffect(() => {
  72. if (renderType === 'rerender') {
  73. getChartData(
  74. 'clear',
  75. slideId,
  76. layerId,
  77. widget,
  78. queryConditions,
  79. {
  80. pagination,
  81. nativeQuery
  82. }
  83. )
  84. }
  85. }, [])
  86. useEffect(() => {
  87. let timer: number
  88. if (!operationInfo && polling === 'true' && +frequency > 0) {
  89. timer = window.setInterval(() => {
  90. getChartData(
  91. 'refresh',
  92. slideId,
  93. layerId,
  94. widget,
  95. queryConditions,
  96. {
  97. pagination,
  98. nativeQuery
  99. }
  100. )
  101. }, +frequency * 1000)
  102. }
  103. return () => {
  104. if (timer) {
  105. clearInterval(timer)
  106. }
  107. }
  108. }, [
  109. operationInfo,
  110. polling,
  111. frequency,
  112. getChartData,
  113. slideId,
  114. layerId,
  115. widgetId,
  116. queryConditions,
  117. pagination,
  118. nativeQuery
  119. ])
  120. const paginationChange = useCallback(
  121. (pageNo: number, pageSize: number, orders) => {
  122. const newPagination = {
  123. ...pagination,
  124. pageNo,
  125. pageSize
  126. }
  127. getChartData(
  128. 'clear',
  129. slideId,
  130. layerId,
  131. widget,
  132. queryConditions,
  133. {
  134. pagination: newPagination,
  135. nativeQuery,
  136. orders
  137. }
  138. )
  139. },
  140. [
  141. slideId,
  142. layerId,
  143. widgetId,
  144. queryConditions,
  145. pagination,
  146. nativeQuery,
  147. getChartData
  148. ]
  149. )
  150. return (
  151. <Widget
  152. {...widget.config}
  153. data={data}
  154. loading={loading}
  155. pagination={pagination}
  156. renderType={renderType}
  157. model={getWidgetViewModel(widget.viewId, widgetId)}
  158. onPaginationChange={paginationChange}
  159. />
  160. )
  161. }
  162. export default Chart