impaktapps-design 0.2.993-graph.1 → 0.2.993-testTimer.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { BarStackHorizontalProps } from "../../interface/interface";
2
+ declare const _default: ({ theme, width, height, events, margin, barValue, }: BarStackHorizontalProps) => import("react/jsx-runtime").JSX.Element;
3
+ export default _default;
@@ -1,3 +1,2 @@
1
- import { BarStackHorizontalProps } from "../../interface/interface";
2
- declare const _default: ({ theme, width, height, events, margin, barValue, }: BarStackHorizontalProps) => import("react/jsx-runtime").JSX.Element;
3
- export default _default;
1
+ declare const HorizontalBarGraph: ({ value, theme }: any) => import("react/jsx-runtime").JSX.Element;
2
+ export default HorizontalBarGraph;
package/dist/style.css CHANGED
@@ -1 +1 @@
1
- @import"https://fonts.googleapis.com/css2?family=Roboto;600&display=swap";.visx-tooltip-glyph{svg {width: 10px; height: 10px;}}.container{margin:0;box-sizing:border-box;font-family:Roboto,sans-serif}
1
+ @import"https://fonts.googleapis.com/css2?family=Roboto;600&display=swap";.container{margin:0;box-sizing:border-box;font-family:Roboto,sans-serif}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impaktapps-design",
3
- "version": "0.2.993-graph.1",
3
+ "version": "0.2.993-testTimer.2",
4
4
  "dependencies": {
5
5
  "@babel/core": "^7.16.12",
6
6
  "@emotion/react": "^11.11.4",
@@ -71,39 +71,12 @@ export default function DrawBarGraph({
71
71
  range: value.style.barStyle.colorRange,
72
72
  });
73
73
 
74
- const baseCharacterWidth = 7;
75
-
76
- let ticksArray: any[] = [];
77
- ticksArray = tempScale.ticks();
78
-
79
- const maxLength = Math.max(
80
- ...ticksArray.map((label) => String(label || "").length)
81
- );
82
-
83
- const calculatedOffset = maxLength * baseCharacterWidth + 10;
84
-
85
-
86
- const baseCharacterWidthBottom = 5;
87
-
88
- let ticksArrayBottom: any[] = [];
89
- ticksArrayBottom = dateScale.domain();
90
-
91
- const maxLengthBottom = Math.max(
92
- ...ticksArrayBottom.map((label) => String(label || "").length)
93
- );
94
-
95
- const calculatedOffsetBottom = maxLengthBottom * baseCharacterWidthBottom + 10;
96
-
97
- margin.left = calculatedOffset+20;
98
- margin.bottom = value.main?.bottomAxisAngle ? calculatedOffsetBottom+40 : margin.bottom;
99
-
100
74
  const xMax = width - margin.left - margin.right;
101
75
  const yMax = height - margin.top - margin.bottom;
102
76
 
103
77
  dateScale.rangeRound([0, xMax]);
104
78
  cityScale.rangeRound([0, dateScale.bandwidth()]);
105
79
  tempScale.range([yMax, 0]);
106
-
107
80
  const {
108
81
  tooltipData,
109
82
  tooltipLeft,
@@ -141,14 +114,6 @@ export default function DrawBarGraph({
141
114
 
142
115
  return width < 10 ? null : (
143
116
  <div>
144
- {value?.main?.legendAvailable && keys.length > 1 && (
145
- <Legend
146
- dataKeyArray={keys}
147
- colorRange={value.style.barStyle.colorRange}
148
- value={value}
149
- theme={theme.myTheme}
150
- />
151
- )}
152
117
  <svg width={width} height={height}>
153
118
  <rect
154
119
  x={0}
@@ -280,6 +245,13 @@ export default function DrawBarGraph({
280
245
  parentWidth={width}
281
246
  />
282
247
  </svg>
248
+ {value?.main?.legendAvailable && keys.length > 1 && (
249
+ <Legend
250
+ dataKeyArray={keys}
251
+ colorRange={value.style.barStyle.colorRange}
252
+ value={value}
253
+ />
254
+ )}
283
255
  {tooltipOpen && (
284
256
  <ToolTip
285
257
  value={value}
@@ -1,32 +1,25 @@
1
- import React, { useEffect, useState } from "react";
1
+ import React, { useState } from "react";
2
+ import { BarGroupHorizontal, BarStackHorizontal } from "@visx/shape";
3
+ import { SeriesPoint } from "@visx/shape/lib/types";
2
4
  import { Group } from "@visx/group";
3
- import { BarGroup } from "@visx/shape";
4
- import { BarStack } from "@visx/shape";
5
+ import { CityTemperature } from "@visx/mock-data/lib/mocks/cityTemperature";
5
6
  import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
6
- import LeftAxis from "../LeftAxis";
7
7
  import BottomAxis from "../BottomAxis";
8
- import { BarGroupProps } from "../../interface/interface";
9
8
  import { useTooltip } from "@visx/tooltip";
9
+ import LeftAxis from "../LeftAxis";
10
+ import { Grid } from "@visx/grid";
11
+ import { BarStackHorizontalProps, CityName } from "../../interface/interface";
10
12
  import ToolTip from "../ToolTip";
11
13
  import Legend from "../Legend";
12
- import { Grid } from "@visx/grid";
13
- import { ScaleOrdinal } from "d3-scale";
14
- import { colors } from "@mui/material";
15
14
 
16
- const defaultMargin = { top: 40, right: 0, bottom: 0, left: 0 };
17
- export default function DrawBarGraph({
15
+ export default ({
18
16
  theme,
19
- width,
20
- height,
17
+ width = 600,
18
+ height = 400,
21
19
  events = false,
22
- margin = defaultMargin,
23
- value,
24
- }: BarGroupProps) {
25
- const data: any[] = value.main?.data;
26
- const keys = Object.keys(data[0]).filter(
27
- (d) => d !== value.main.xAxisValue
28
- ) as any[];
29
-
20
+ margin,
21
+ barValue,
22
+ }: BarStackHorizontalProps) => {
30
23
  const [hoveredBar, setHoveredBar] = useState<{
31
24
  date: any;
32
25
  city: number;
@@ -35,7 +28,14 @@ export default function DrawBarGraph({
35
28
  city: null,
36
29
  });
37
30
 
38
- const getDate = (d: any) => d[value.main.xAxisValue];
31
+ const xMax = width - margin.left - margin.right;
32
+ const yMax = height - margin.top - margin.bottom;
33
+ const data: any[] = barValue?.main?.data;
34
+
35
+ const keys = Object.keys(data[0]).filter(
36
+ (d) => d !== barValue.main.xAxisValue
37
+ ) as any[];
38
+
39
39
  const temperatureTotals = data.reduce((allTotals, currentDate) => {
40
40
  const totalTemperature = keys.reduce((dailyTotal, k) => {
41
41
  dailyTotal += Number(currentDate[k]);
@@ -45,16 +45,11 @@ export default function DrawBarGraph({
45
45
  return allTotals;
46
46
  }, [] as number[]);
47
47
 
48
- const dateScale = scaleBand<string>({
49
- domain: data.map(getDate),
50
- padding: 0.4,
51
- });
52
- const cityScale = scaleBand<string>({
53
- domain: keys,
54
- });
55
- const tempScale = scaleLinear<number>({
48
+ const getDate = (d: any) => d[barValue.main.xAxisValue];
49
+
50
+ const temperatureScale = scaleLinear<number>({
56
51
  domain:
57
- value?.main?.type === "StackBarGraph"
52
+ barValue?.main?.type === "HorizontalStackBarGraph"
58
53
  ? [0, Math.max(...temperatureTotals)]
59
54
  : [
60
55
  0,
@@ -66,43 +61,21 @@ export default function DrawBarGraph({
66
61
  ],
67
62
  nice: true,
68
63
  });
64
+ const dateScale = scaleBand<string>({
65
+ domain: data.map(getDate),
66
+ padding: 0.4,
67
+ });
68
+ const cityScale = scaleBand<string>({
69
+ domain: keys,
70
+ });
69
71
  const colorScale = scaleOrdinal<string, string>({
70
72
  domain: keys,
71
- range: value.style.barStyle.colorRange,
73
+ range: barValue.style.barStyle.colorRange,
72
74
  });
73
75
 
74
- const baseCharacterWidth = 7;
75
-
76
- let ticksArray: any[] = [];
77
- ticksArray = tempScale.ticks();
78
-
79
- const maxLength = Math.max(
80
- ...ticksArray.map((label) => String(label || "").length)
81
- );
82
-
83
- const calculatedOffset = maxLength * baseCharacterWidth + 10;
84
-
85
-
86
- const baseCharacterWidthBottom = 5;
87
-
88
- let ticksArrayBottom: any[] = [];
89
- ticksArrayBottom = dateScale.domain();
90
-
91
- const maxLengthBottom = Math.max(
92
- ...ticksArrayBottom.map((label) => String(label || "").length)
93
- );
94
-
95
- const calculatedOffsetBottom = maxLengthBottom * baseCharacterWidthBottom + 10;
96
-
97
- margin.left = calculatedOffset+20;
98
- margin.bottom = value.main?.bottomAxisAngle ? calculatedOffsetBottom+40 : margin.bottom;
99
-
100
- const xMax = width - margin.left - margin.right;
101
- const yMax = height - margin.top - margin.bottom;
102
-
103
- dateScale.rangeRound([0, xMax]);
76
+ temperatureScale.rangeRound([0, xMax]);
77
+ dateScale.rangeRound([yMax, 0]);
104
78
  cityScale.rangeRound([0, dateScale.bandwidth()]);
105
- tempScale.range([yMax, 0]);
106
79
 
107
80
  const {
108
81
  tooltipData,
@@ -112,12 +85,13 @@ export default function DrawBarGraph({
112
85
  showTooltip,
113
86
  hideTooltip,
114
87
  }: any = useTooltip();
88
+
115
89
  const handleMouse = (event: any, bar: any, barGroup: any) => {
116
90
  setHoveredBar({ date: barGroup, city: bar.index });
117
91
  showTooltip({
118
92
  tooltipLeft: event.clientX,
119
93
  tooltipTop: event.clientY,
120
- tooltipData: {groupData : data[barGroup.index], highlightedBar: bar},
94
+ tooltipData: { groupData: data[barGroup.index], highlightedBar: bar },
121
95
  });
122
96
  };
123
97
  const handleMouseOut = (event: any) => {
@@ -129,40 +103,32 @@ export default function DrawBarGraph({
129
103
  showTooltip({
130
104
  tooltipLeft: event.clientX,
131
105
  tooltipTop: event.clientY,
132
- tooltipData: {groupData: data[bar.index], highlightedBar: bar},
106
+ tooltipData: { groupData: data[bar.index], highlightedBar: bar },
133
107
  });
134
108
  };
135
109
  function calcWidthofBar(date: any): number {
136
110
  return date?.bars.reduce(
137
- (totalWidth, bar) => totalWidth + Math.min(bar.width, 15),
111
+ (totalWidth, bar) => totalWidth + Math.min(bar.height, 15),
138
112
  0
139
113
  );
140
114
  }
141
115
 
142
116
  return width < 10 ? null : (
143
117
  <div>
144
- {value?.main?.legendAvailable && keys.length > 1 && (
145
- <Legend
146
- dataKeyArray={keys}
147
- colorRange={value.style.barStyle.colorRange}
148
- value={value}
149
- theme={theme.myTheme}
150
- />
151
- )}
152
118
  <svg width={width} height={height}>
153
119
  <rect
154
120
  x={0}
155
121
  y={0}
156
122
  width={width}
157
123
  height={height}
158
- fill={value.style.containerStyle.background || "#612efb"}
124
+ fill={barValue.style.containerStyle.background || "#612efb"}
159
125
  />
160
126
  <Grid
161
127
  top={margin.top}
162
128
  left={margin.left}
163
- xScale={dateScale}
164
- yScale={tempScale}
165
- width={width - margin.left - margin.right}
129
+ xScale={temperatureScale}
130
+ yScale={dateScale}
131
+ width={xMax}
166
132
  height={yMax}
167
133
  stroke="black"
168
134
  xOffset={dateScale.bandwidth() / 50}
@@ -174,62 +140,66 @@ export default function DrawBarGraph({
174
140
  {hoveredBar && (
175
141
  <rect
176
142
  style={{ display: hoveredBar.date ? "block" : "none" }}
177
- x={
178
- value.main.type === "StackBarGraph"
179
- ? hoveredBar?.date?.bars[hoveredBar.city].x -
180
- (Math.min(hoveredBar?.date?.bars[0].width, 15) -
181
- hoveredBar?.date?.bars[0].width)/2
182
- : hoveredBar.date?.x0 -
183
- (Math.min(hoveredBar?.date?.bars[0].width, 15) -
184
- hoveredBar?.date?.bars[0].width) || 0
143
+ y={
144
+ barValue.main.type === "HorizontalStackBarGraph"
145
+ ? hoveredBar?.date?.bars[hoveredBar.city].y -
146
+ (Math.min(hoveredBar?.date?.bars[0].height, 15) -
147
+ hoveredBar?.date?.bars[0].height) /
148
+ 2
149
+ : hoveredBar.date?.y0 -
150
+ (Math.min(hoveredBar?.date?.bars[0].height, 15) -
151
+ hoveredBar?.date?.bars[0].height) || 0
185
152
  }
186
- width={
187
- value.main.type !== "StackBarGraph"
153
+ width={xMax}
154
+ height={
155
+ barValue.main.type !== "HorizontalStackBarGraph"
188
156
  ? calcWidthofBar(hoveredBar?.date)
189
- : Math.min(hoveredBar?.date?.bars[0].width, 15)
157
+ : Math.min(hoveredBar?.date?.bars[0].height, 15)
190
158
  }
191
- height={yMax}
192
- fill={value.style?.barStyle?.hoverBackgroundColor}
159
+ fill={barValue.style?.barStyle?.hoverBackgroundColor}
193
160
  opacity={1}
194
161
  />
195
162
  )}
196
- <LeftAxis value={value} yScale={tempScale} parentWidth={width} />
197
- {value?.main?.type === "StackBarGraph" ? (
198
- <BarStack<any, any>
163
+ {barValue.main.type === "HorizontalStackBarGraph" ? (
164
+ <BarStackHorizontal
199
165
  data={data}
200
166
  keys={keys}
201
- x={getDate}
202
- xScale={dateScale}
203
- yScale={tempScale}
167
+ height={yMax}
168
+ y={getDate}
169
+ xScale={temperatureScale}
170
+ yScale={dateScale}
204
171
  color={colorScale}
205
172
  >
206
173
  {(barStacks) =>
207
174
  barStacks.map((barStack) =>
208
175
  barStack.bars.map((bar) => (
209
176
  <rect
210
- key={`bar-stack-${barStack.index}-${bar.index}`}
211
- x={bar.x - (Math.min(bar.width, 15) - bar.width) / 2}
212
- y={bar.y}
213
- height={bar.height}
214
- width={Math.min(bar.width, 15)}
177
+ key={`barstack-horizontal-${barStack.index}-${bar.index}`}
178
+ x={bar.x}
179
+ y={bar.y - (Math.min(bar.height, 15) - bar.height) / 2}
180
+ width={bar.width}
181
+ height={Math.min(bar.height, 15)}
215
182
  fill={bar.color}
216
183
  onMouseOver={(e) => stackhHandleMouse(e, bar, barStack)}
217
184
  onMouseOut={(e) => handleMouseOut(e)}
218
- stroke={hoveredBar.city === bar.index ? 'black' : "transparent"}
185
+ stroke={
186
+ hoveredBar.city === bar.index ? "black" : "transparent"
187
+ }
219
188
  />
220
189
  ))
221
190
  )
222
191
  }
223
- </BarStack>
192
+ </BarStackHorizontal>
224
193
  ) : (
225
- <BarGroup
194
+ <BarGroupHorizontal
226
195
  data={data}
227
196
  keys={keys}
197
+ width={xMax}
228
198
  height={yMax}
229
- x0={getDate}
230
- x0Scale={dateScale}
231
- x1Scale={cityScale}
232
- yScale={tempScale}
199
+ y0={getDate}
200
+ y0Scale={dateScale}
201
+ y1Scale={cityScale}
202
+ xScale={temperatureScale}
233
203
  color={(key, index) => {
234
204
  const color = colorScale(key);
235
205
  const opColor = `${colorScale(key)}50`;
@@ -243,46 +213,57 @@ export default function DrawBarGraph({
243
213
  {(barGroups) =>
244
214
  barGroups.map((barGroup) => (
245
215
  <Group
246
- key={`bar-group-${barGroup.index}-${barGroup.x0}`}
247
- left={barGroup.x0}
216
+ key={`bar-group-${barGroup.index}-${barGroup.y0}`}
217
+ top={barGroup.y0}
248
218
  >
249
219
  {barGroup.bars.map((bar) => {
250
- const barWidth = Math.min(bar.width, 15);
251
- const barX =
220
+ const barHeight = Math.min(bar.height, 15);
221
+ const barY =
252
222
  bar.index === 0
253
- ? bar.x - (barWidth - bar.width)
254
- : bar.x;
223
+ ? bar.y - (barHeight - bar.height)
224
+ : bar.y;
255
225
  return (
256
226
  <rect
257
227
  key={`bar-group-bar-${barGroup.index}-${bar.index}-${bar.value}-${bar.key}`}
258
- x={barX}
259
- y={bar.y}
260
- width={barWidth}
261
- height={bar.height}
228
+ y={barY}
229
+ x={bar.x}
230
+ width={bar.width}
231
+ height={barHeight}
262
232
  fill={bar.color}
263
233
  onMouseOver={(e) => handleMouse(e, bar, barGroup)}
264
234
  onMouseOut={(e) => handleMouseOut(e)}
265
- stroke={hoveredBar.city === bar.index ? 'black' : "transparent"}
235
+ stroke={
236
+ hoveredBar.city === bar.index
237
+ ? "black"
238
+ : "transparent"
239
+ }
266
240
  />
267
241
  );
268
242
  })}
269
243
  </Group>
270
244
  ))
271
245
  }
272
- </BarGroup>
246
+ </BarGroupHorizontal>
273
247
  )}
248
+ <LeftAxis value={barValue} yScale={dateScale} parentWidth={width} />
249
+ <BottomAxis
250
+ value={barValue}
251
+ yMax={yMax}
252
+ xScale={temperatureScale}
253
+ parentWidth={width}
254
+ />
274
255
  </Group>
275
- <BottomAxis
276
- yMax={yMax + margin.top}
277
- value={value}
278
- left={margin.left}
279
- xScale={dateScale}
280
- parentWidth={width}
281
- />
282
256
  </svg>
283
- {tooltipOpen && (
257
+ {barValue?.main?.legendAvailable && keys.length > 1 && (
258
+ <Legend
259
+ dataKeyArray={keys}
260
+ colorRange={barValue.style.barStyle.colorRange}
261
+ value={barValue}
262
+ />
263
+ )}
264
+ {tooltipOpen && tooltipData && (
284
265
  <ToolTip
285
- value={value}
266
+ value={barValue}
286
267
  theme={theme}
287
268
  top={tooltipTop}
288
269
  left={tooltipLeft}
@@ -292,4 +273,4 @@ export default function DrawBarGraph({
292
273
  )}
293
274
  </div>
294
275
  );
295
- }
276
+ };