|
1 | | -"use client" |
2 | | -import React from 'react'; |
3 | | -import * as PropTypes from 'prop-types'; |
| 1 | +"use client"; |
| 2 | +import React from "react"; |
| 3 | +import * as PropTypes from "prop-types"; |
4 | 4 |
|
5 | 5 | import { |
6 | | - Chart as KendoChart, |
7 | | - ChartSeries, |
8 | | - ChartSeriesItem, |
9 | | - ChartCategoryAxis, |
10 | | - ChartCategoryAxisItem, |
11 | | - ChartValueAxis, |
12 | | - ChartValueAxisItem, |
13 | | - ChartLegend, |
14 | | - ChartTooltip |
15 | | -} from '@progress/kendo-react-charts'; |
16 | | -import { groupBy, filterBy } from '@progress/kendo-data-query'; |
17 | | -import { useInternationalization } from '@progress/kendo-react-intl'; |
18 | | -import { locales } from './../resources/locales'; |
| 6 | + Chart as KendoChart, |
| 7 | + ChartSeries, |
| 8 | + ChartSeriesItem, |
| 9 | + ChartCategoryAxis, |
| 10 | + ChartCategoryAxisItem, |
| 11 | + ChartValueAxis, |
| 12 | + ChartValueAxisItem, |
| 13 | + ChartLegend, |
| 14 | + ChartTooltip, |
| 15 | +} from "@progress/kendo-react-charts"; |
| 16 | +const importHammerJs = () => import("hammerjs"); |
| 17 | +import { groupBy, filterBy } from "@progress/kendo-data-query"; |
| 18 | +import { useInternationalization } from "@progress/kendo-react-intl"; |
| 19 | +import { locales } from "./../resources/locales"; |
19 | 20 |
|
20 | | -const MONTH_FORMAT = 'MMMM yyyy'; |
21 | | -const COMPACT_FORMAT = 'MM yyyy'; |
| 21 | +const MONTH_FORMAT = "MMMM yyyy"; |
22 | 22 |
|
23 | 23 | export const Chart = (props) => { |
24 | | - const { |
25 | | - data, groupByField, seriesCategoryField, seriesField, |
26 | | - filterStart, filterEnd, seriesType, groupResourceData, groupTextField, |
27 | | - groupColorField, onRefresh |
28 | | - } = props; |
| 24 | + if (typeof window !== "undefined") { |
| 25 | + importHammerJs(); |
| 26 | + } |
29 | 27 |
|
30 | | - const intlService = useInternationalization(); |
31 | | - const filteredData = filterBy(data, { |
32 | | - logic: "and", |
33 | | - filters: [ |
34 | | - { field: "orderDate", operator: "gt", value: filterStart }, |
35 | | - { field: "orderDate", operator: "lt", value: filterEnd } |
36 | | - ] |
37 | | - }); |
38 | | - const groupedData = groupBy(filteredData, [{ field: groupByField }]); |
| 28 | + const { |
| 29 | + data, |
| 30 | + groupByField, |
| 31 | + seriesCategoryField, |
| 32 | + seriesField, |
| 33 | + filterStart, |
| 34 | + filterEnd, |
| 35 | + seriesType, |
| 36 | + groupResourceData, |
| 37 | + groupTextField, |
| 38 | + groupColorField, |
| 39 | + onRefresh, |
| 40 | + } = props; |
39 | 41 |
|
40 | | - const ChartTooltipRender = ({ point }) => { |
41 | | - return ( |
42 | | - <div> |
43 | | - <div style={{textTransform: 'uppercase'}}>{intlService.formatDate(point.category, MONTH_FORMAT)}</div> |
44 | | - <div style={{textAlign: 'center'}}>{intlService.formatNumber(point.value, {locale: locales.locale})}</div> |
45 | | - </div> |
46 | | - ); |
47 | | - }; |
| 42 | + const intlService = useInternationalization(); |
| 43 | + const filteredData = filterBy(data, { |
| 44 | + logic: "and", |
| 45 | + filters: [ |
| 46 | + { field: "orderDate", operator: "gt", value: filterStart }, |
| 47 | + { field: "orderDate", operator: "lt", value: filterEnd }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + const groupedData = groupBy(filteredData, [{ field: groupByField }]); |
48 | 51 |
|
| 52 | + const ChartTooltipRender = ({ point }) => { |
49 | 53 | return ( |
50 | | - <KendoChart style={{ height: 300 }} onRefresh={onRefresh}> |
51 | | - <ChartLegend position="bottom" orientation="horizontal" background={'#f4f5f8'} padding={{ left: 80 }} labels={{padding: { right: 80 }}}/> |
52 | | - <ChartTooltip render={ChartTooltipRender} /> |
53 | | - <ChartSeries> |
54 | | - { |
55 | | - groupedData.map(group => { |
56 | | - const groupResource = groupResourceData.find(item => item[groupByField] === group.value); |
| 54 | + <div> |
| 55 | + <div style={{ textTransform: "uppercase" }}> |
| 56 | + {intlService.formatDate(point.category, MONTH_FORMAT)} |
| 57 | + </div> |
| 58 | + <div style={{ textAlign: "center" }}> |
| 59 | + {intlService.formatNumber(point.value, { locale: locales.locale })} |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + ); |
| 63 | + }; |
57 | 64 |
|
58 | | - return ( |
59 | | - <ChartSeriesItem |
60 | | - key={group.value} |
61 | | - name={groupResource[groupTextField]} |
62 | | - type={seriesType} |
63 | | - field={seriesField} |
64 | | - categoryField={seriesCategoryField} |
65 | | - data={group.items} |
66 | | - color={groupResource[groupColorField]} |
67 | | - > |
| 65 | + return ( |
| 66 | + <KendoChart style={{ height: 300 }} onRefresh={onRefresh}> |
| 67 | + <ChartLegend |
| 68 | + position="bottom" |
| 69 | + orientation="horizontal" |
| 70 | + background={"#f4f5f8"} |
| 71 | + padding={{ left: 80 }} |
| 72 | + labels={{ padding: { right: 80 } }} |
| 73 | + /> |
| 74 | + <ChartTooltip render={ChartTooltipRender} /> |
| 75 | + <ChartSeries> |
| 76 | + {groupedData.map((group) => { |
| 77 | + const groupResource = groupResourceData.find( |
| 78 | + (item) => item[groupByField] === group.value |
| 79 | + ); |
68 | 80 |
|
69 | | - </ChartSeriesItem> |
70 | | - ); |
71 | | - }) |
72 | | - } |
73 | | - </ChartSeries> |
74 | | - <ChartCategoryAxis> |
75 | | - <ChartCategoryAxisItem |
76 | | - baseUnit={'months'} |
77 | | - labels={{ |
78 | | - dateFormats: { |
79 | | - months: MONTH_FORMAT |
80 | | - } |
81 | | - }} |
82 | | - > |
83 | | - </ChartCategoryAxisItem> |
84 | | - </ChartCategoryAxis> |
85 | | - <ChartValueAxis> |
86 | | - <ChartValueAxisItem labels={{format: {locale: locales.locale}}}> |
87 | | - </ChartValueAxisItem> |
88 | | - </ChartValueAxis> |
89 | | - </KendoChart> |
90 | | - ); |
| 81 | + return ( |
| 82 | + <ChartSeriesItem |
| 83 | + key={group.value} |
| 84 | + name={groupResource[groupTextField]} |
| 85 | + type={seriesType} |
| 86 | + field={seriesField} |
| 87 | + categoryField={seriesCategoryField} |
| 88 | + data={group.items} |
| 89 | + color={groupResource[groupColorField]} |
| 90 | + ></ChartSeriesItem> |
| 91 | + ); |
| 92 | + })} |
| 93 | + </ChartSeries> |
| 94 | + <ChartCategoryAxis> |
| 95 | + <ChartCategoryAxisItem |
| 96 | + baseUnit={"months"} |
| 97 | + labels={{ |
| 98 | + dateFormats: { |
| 99 | + months: MONTH_FORMAT, |
| 100 | + }, |
| 101 | + }} |
| 102 | + ></ChartCategoryAxisItem> |
| 103 | + </ChartCategoryAxis> |
| 104 | + <ChartValueAxis> |
| 105 | + <ChartValueAxisItem |
| 106 | + labels={{ format: { locale: locales.locale } }} |
| 107 | + ></ChartValueAxisItem> |
| 108 | + </ChartValueAxis> |
| 109 | + </KendoChart> |
| 110 | + ); |
91 | 111 | }; |
92 | 112 |
|
93 | | -Chart.displayName = 'Chart'; |
| 113 | +Chart.displayName = "Chart"; |
94 | 114 | Chart.propTypes = { |
95 | | - data: PropTypes.array, |
96 | | - groupByField: PropTypes.string, |
97 | | - seriesCategoryField: PropTypes.string, |
98 | | - seriesField: PropTypes.string, |
99 | | - filterStart: PropTypes.object, |
100 | | - filterEnd: PropTypes.object, |
101 | | - seriesType: PropTypes.string, |
102 | | - groupResourceData: PropTypes.array, |
103 | | - groupTextField: PropTypes.string, |
104 | | - onRefresh: PropTypes.func |
| 115 | + data: PropTypes.array, |
| 116 | + groupByField: PropTypes.string, |
| 117 | + seriesCategoryField: PropTypes.string, |
| 118 | + seriesField: PropTypes.string, |
| 119 | + filterStart: PropTypes.object, |
| 120 | + filterEnd: PropTypes.object, |
| 121 | + seriesType: PropTypes.string, |
| 122 | + groupResourceData: PropTypes.array, |
| 123 | + groupTextField: PropTypes.string, |
| 124 | + onRefresh: PropTypes.func, |
105 | 125 | }; |
0 commit comments