jdesiree commited on
Commit
2374e70
·
verified ·
1 Parent(s): 3f0e206

Update graph_tool.py

Browse files
Files changed (1) hide show
  1. graph_tool.py +36 -12
graph_tool.py CHANGED
@@ -4,27 +4,40 @@ import matplotlib.pyplot as plt
4
  import numpy as np
5
  import json
6
 
7
- def generate_plot(data_json, labels_json, plot_type, title, x_label="", y_label=""):
8
  """
9
  Generates a plot (bar, line, or pie) and returns it as an HTML-formatted Base64-encoded image string.
10
 
11
  Args:
12
- data_json (str): JSON string containing data dictionary where keys are labels and values are numerical data.
13
- labels_json (str): JSON string containing list of labels for the data points.
14
- plot_type (str): The type of plot to generate ('bar', 'line', or 'pie').
15
- title (str): The title of the plot.
16
- x_label (str): The label for the x-axis.
17
- y_label (str): The label for the y-axis.
 
 
 
18
 
19
  Returns:
20
  str: An HTML img tag with Base64-encoded plot image.
21
  """
22
  try:
23
- # Parse JSON strings
24
- data = json.loads(data_json)
25
- labels = json.loads(labels_json)
26
  except json.JSONDecodeError as e:
27
- return f'<p style="color:red;">Error parsing JSON data: {e}</p>'
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Validate inputs
30
  if not isinstance(data, dict):
@@ -99,4 +112,15 @@ def generate_plot(data_json, labels_json, plot_type, title, x_label="", y_label=
99
 
100
  except Exception as e:
101
  plt.close('all') # Clean up any open figures
102
- return f'<p style="color:red;">Error generating plot: {str(e)}</p>'
 
 
 
 
 
 
 
 
 
 
 
 
4
  import numpy as np
5
  import json
6
 
7
+ def generate_plot_single_input(plot_config_json):
8
  """
9
  Generates a plot (bar, line, or pie) and returns it as an HTML-formatted Base64-encoded image string.
10
 
11
  Args:
12
+ plot_config_json (str): JSON string containing all plot configuration:
13
+ {
14
+ "data": {"key1": value1, "key2": value2, ...},
15
+ "labels": ["label1", "label2", ...],
16
+ "plot_type": "bar|line|pie",
17
+ "title": "Plot Title",
18
+ "x_label": "X Axis Label" (optional),
19
+ "y_label": "Y Axis Label" (optional)
20
+ }
21
 
22
  Returns:
23
  str: An HTML img tag with Base64-encoded plot image.
24
  """
25
  try:
26
+ # Parse the main JSON configuration
27
+ config = json.loads(plot_config_json)
 
28
  except json.JSONDecodeError as e:
29
+ return f'<p style="color:red;">Error parsing JSON configuration: {e}</p>'
30
+
31
+ # Extract parameters with defaults
32
+ try:
33
+ data = config.get("data", {})
34
+ labels = config.get("labels", [])
35
+ plot_type = config.get("plot_type", "bar")
36
+ title = config.get("title", "Untitled Plot")
37
+ x_label = config.get("x_label", "")
38
+ y_label = config.get("y_label", "")
39
+ except Exception as e:
40
+ return f'<p style="color:red;">Error extracting configuration parameters: {e}</p>'
41
 
42
  # Validate inputs
43
  if not isinstance(data, dict):
 
112
 
113
  except Exception as e:
114
  plt.close('all') # Clean up any open figures
115
+ return f'<p style="color:red;">Error generating plot: {str(e)}</p>'
116
+
117
+ # Example usage:
118
+ # plot_config = {
119
+ # "data": {"A": 10, "B": 20, "C": 15},
120
+ # "labels": ["Category A", "Category B", "Category C"],
121
+ # "plot_type": "bar",
122
+ # "title": "Sample Bar Chart",
123
+ # "x_label": "Categories",
124
+ # "y_label": "Values"
125
+ # }
126
+ # result = generate_plot_single_input(json.dumps(plot_config))