Chart.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, useCallback, useMemo } from 'react'
  21. import { Button, Icon, Dropdown, Menu } from 'antd'
  22. const ButtonGroup = Button.Group
  23. import IconFont from 'components/IconFont'
  24. import { DisplayToolbarContext } from './util'
  25. import { GraphTypes, SecondaryGraphTypes } from '../constants'
  26. import { ClickParam } from 'antd/lib/menu'
  27. const SecondaryGraphIcons = [
  28. {
  29. name: '矩形',
  30. icon: 'icon-rounded-rect',
  31. type: SecondaryGraphTypes.Rectangle
  32. },
  33. {
  34. name: '标签',
  35. icon: 'icon-rect-text',
  36. type: SecondaryGraphTypes.Label
  37. },
  38. {
  39. name: '视频',
  40. icon: 'icon-video',
  41. type: SecondaryGraphTypes.Video
  42. },
  43. {
  44. name: '时间器',
  45. icon: 'icon-clock',
  46. type: SecondaryGraphTypes.Timer
  47. }
  48. ]
  49. interface IChartProps {
  50. onAdd: (type: GraphTypes, subType?: SecondaryGraphTypes) => void
  51. }
  52. const Chart: React.FC<IChartProps> = (props) => {
  53. const { size, comment } = useContext(DisplayToolbarContext)
  54. const { onAdd } = props
  55. const addChart = useCallback(() => {
  56. onAdd(GraphTypes.Chart)
  57. }, [onAdd])
  58. const addSecondary = useCallback(
  59. (param: ClickParam) => {
  60. onAdd(GraphTypes.Secondary, +param.key as SecondaryGraphTypes)
  61. },
  62. [onAdd]
  63. )
  64. const overlay = (
  65. <Menu onClick={addSecondary}>
  66. {SecondaryGraphIcons.map(({ name, icon, type }) => (
  67. <Menu.Item key={type}>
  68. <IconFont type={icon} />{name}
  69. </Menu.Item>
  70. ))}
  71. </Menu>
  72. )
  73. return (
  74. <ButtonGroup size={size}>
  75. <Button type="ghost" onClick={addChart}>
  76. <Icon type="bar-chart" />
  77. {comment && '图表'}
  78. </Button>
  79. <Dropdown overlay={overlay} placement="bottomRight" trigger={['click']}>
  80. <Button type="ghost">
  81. <Icon type="appstore" />
  82. {comment && '辅助图形'}
  83. </Button>
  84. </Dropdown>
  85. </ButtonGroup>
  86. )
  87. }
  88. export default Chart