-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (46 loc) · 1.66 KB
/
utils.py
File metadata and controls
54 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def convert_to_chartjs_format(input_data):
"""
Converts input data into a Chart.js compatible format.
Parameters:
- input_data (dict): The input data containing chart information.
Returns:
- dict: A dictionary formatted for Chart.js.
"""
# Determine the chart type, defaulting to 'line' if not specified
chart_type = input_data.get('type', 'line')
# Extract data points
data_points = input_data.get('data', [])
# Check if data_points is a list and has at least one item
if isinstance(data_points, list) and data_points:
# Extract the first dictionary to determine label and data keys
first_item = data_points[0]
if isinstance(first_item, dict) and len(first_item) >= 2:
# Get the first two keys
keys = list(first_item.keys())
label_key = keys[0]
data_key = keys[1]
# Extract labels and data
labels = [item.get(label_key) for item in data_points]
data = [item.get(data_key) for item in data_points]
# Create the Chart.js compatible structure
chartjs_data = {
"type": chart_type,
"data": {
"labels": labels,
"datasets": [
{
"label": data_key,
"data": data
}
]
}
}
return chartjs_data
# Return an empty structure if input data is not as expected
return {
"type": chart_type,
"data": {
"labels": [],
"datasets": []
}
}