@dhirajpatil2025/report1chart 1.0.0
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.
- package/package.json +15 -0
- package/src/index.js +134 -0
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dhirajpatil2025/report1chart",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "src/index.js",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"description": ""
|
|
15
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
View,
|
|
4
|
+
Text,
|
|
5
|
+
TouchableOpacity,
|
|
6
|
+
ActivityIndicator,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
|
|
10
|
+
const ReportChart = ({ baseUrl, token, buttons = [], height = 200 }) => {
|
|
11
|
+
const [loading, setLoading] = useState(false);
|
|
12
|
+
const [chartData, setChartData] = useState([]);
|
|
13
|
+
const [active, setActive] = useState(null);
|
|
14
|
+
const [error, setError] = useState(null);
|
|
15
|
+
|
|
16
|
+
const fetchData = async (btn) => {
|
|
17
|
+
try {
|
|
18
|
+
setActive(btn.label);
|
|
19
|
+
setLoading(true);
|
|
20
|
+
setError(null);
|
|
21
|
+
|
|
22
|
+
const res = await fetch(baseUrl + btn.endpoint, {
|
|
23
|
+
headers: {
|
|
24
|
+
Authorization: `Bearer ${token}`,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const json = await res.json();
|
|
29
|
+
|
|
30
|
+
// 🔹 Transform API response INSIDE npm
|
|
31
|
+
const formatted = json.labels.map((label, i) => ({
|
|
32
|
+
name: label,
|
|
33
|
+
value: json.values[i],
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
setChartData(formatted);
|
|
37
|
+
} catch (e) {
|
|
38
|
+
setError('Failed to load data');
|
|
39
|
+
} finally {
|
|
40
|
+
setLoading(false);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<View>
|
|
46
|
+
{/* 🔘 BUTTONS */}
|
|
47
|
+
<View style={styles.btnRow}>
|
|
48
|
+
{buttons.map((btn) => (
|
|
49
|
+
<TouchableOpacity
|
|
50
|
+
key={btn.label}
|
|
51
|
+
style={[
|
|
52
|
+
styles.btn,
|
|
53
|
+
active === btn.label && styles.activeBtn,
|
|
54
|
+
]}
|
|
55
|
+
onPress={() => fetchData(btn)}
|
|
56
|
+
>
|
|
57
|
+
<Text style={styles.btnText}>{btn.label}</Text>
|
|
58
|
+
</TouchableOpacity>
|
|
59
|
+
))}
|
|
60
|
+
</View>
|
|
61
|
+
|
|
62
|
+
{/* 🔄 LOADING */}
|
|
63
|
+
{loading && <ActivityIndicator size="large" />}
|
|
64
|
+
|
|
65
|
+
{/* ❌ ERROR */}
|
|
66
|
+
{error && <Text style={styles.error}>{error}</Text>}
|
|
67
|
+
|
|
68
|
+
{/* 📊 CHART */}
|
|
69
|
+
{!loading && chartData.length > 0 && (
|
|
70
|
+
<View style={[styles.chart, { height }]}>
|
|
71
|
+
{chartData.map((item, index) => (
|
|
72
|
+
<View key={index} style={styles.barWrap}>
|
|
73
|
+
<View
|
|
74
|
+
style={[
|
|
75
|
+
styles.bar,
|
|
76
|
+
{ height: Math.max(item.value / 1000, 10) },
|
|
77
|
+
]}
|
|
78
|
+
/>
|
|
79
|
+
<Text style={styles.label}>{item.name}</Text>
|
|
80
|
+
</View>
|
|
81
|
+
))}
|
|
82
|
+
</View>
|
|
83
|
+
)}
|
|
84
|
+
</View>
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export default ReportChart;
|
|
89
|
+
|
|
90
|
+
const styles = StyleSheet.create({
|
|
91
|
+
btnRow: {
|
|
92
|
+
flexDirection: 'row',
|
|
93
|
+
justifyContent: 'space-between',
|
|
94
|
+
marginBottom: 12,
|
|
95
|
+
},
|
|
96
|
+
btn: {
|
|
97
|
+
flex: 1,
|
|
98
|
+
marginHorizontal: 4,
|
|
99
|
+
paddingVertical: 8,
|
|
100
|
+
borderRadius: 6,
|
|
101
|
+
backgroundColor: '#ddd',
|
|
102
|
+
alignItems: 'center',
|
|
103
|
+
},
|
|
104
|
+
activeBtn: {
|
|
105
|
+
backgroundColor: '#4CAF50',
|
|
106
|
+
},
|
|
107
|
+
btnText: {
|
|
108
|
+
fontWeight: '600',
|
|
109
|
+
color: '#000',
|
|
110
|
+
},
|
|
111
|
+
chart: {
|
|
112
|
+
flexDirection: 'row',
|
|
113
|
+
alignItems: 'flex-end',
|
|
114
|
+
marginTop: 12,
|
|
115
|
+
},
|
|
116
|
+
barWrap: {
|
|
117
|
+
flex: 1,
|
|
118
|
+
alignItems: 'center',
|
|
119
|
+
},
|
|
120
|
+
bar: {
|
|
121
|
+
width: 22,
|
|
122
|
+
backgroundColor: '#4CAF50',
|
|
123
|
+
borderRadius: 6,
|
|
124
|
+
},
|
|
125
|
+
label: {
|
|
126
|
+
fontSize: 12,
|
|
127
|
+
marginTop: 6,
|
|
128
|
+
},
|
|
129
|
+
error: {
|
|
130
|
+
color: 'red',
|
|
131
|
+
textAlign: 'center',
|
|
132
|
+
marginTop: 10,
|
|
133
|
+
},
|
|
134
|
+
});
|