# pie_chart_python.py
# September - 2026
# RJM Programming
# Thanks to https://www.w3schools.com/python/default.asp

import os
import matplotlib
import sys

# Set backend to Agg before importing pyplot
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import numpy as np
#import plotly.express as px
from PIL import Image

nums = []
lbls = []
mytitle = ""
sofar = 0
place = ''
midsuff = ''
if len(sys.argv[0].split('/')) > 1:
  fname = sys.argv[0].split(".py")[0].split('/')[-1 + len(sys.argv[0].split(".py")[0].split('/'))];
else:
  fname = sys.argv[0].split(".py")[0];
fname = fname.split("\\")[-1 + len(fname.split("\\"))].replace('_','').replace('_','').replace('_','').replace('_','').replace('_','').replace('_','');
fname = fname.split('_0')[0].split('_1')[0].split('_2')[0].split('_3')[0].split('_4')[0].split('_5')[0].split('_6')[0].split('_7')[0].split('_8')[0].split('_9')[0]
fname = fname.split('0')[0].split('1')[0].split('2')[0].split('3')[0].split('4')[0].split('5')[0].split('6')[0].split('7')[0].split('8')[0].split('9')[0]
if len(sys.argv) > 1:
  place = sys.argv[-1 + len(sys.argv)]

if len(sys.argv) > 2:
  midsuff = sys.argv[-2 + len(sys.argv)].replace(fname,'').replace('.py','')

if len(nums) <= 0:
  while sofar < 100 and sofar >= 0:
     if fname == "piechartpython":
       print(f"Enter your percentage value regarding a pie chart where so far {sofar} has already been allocated.  Can append after a comma a pie chart sector label.")
     elif fname == "barchartpython":
       print(f"Enter your value regarding a bar chart and append after a comma a bar chart sector label.")
     anum = input()
     if anum == "":
       sofar = -1
     elif "," in anum:
       x = anum.split(",")
       nums.append(int(x[0]))
       lbls.append(anum.replace(x[0] + ',', ''))
       if fname == "piechartpython":
         sofar += int(x[0])
     else:
       nums.append(int(anum))
       lbls.append('')
       if fname == "piechartpython":
         sofar += int(anum)
  

y = np.array(nums)

fig, ax = plt.subplots()

if len(mytitle) > 0:
  plt.title(mytitle)

if fname == "barchartpython":
  ax.bar(lbls, nums)
elif len(lbls) < len(nums) and fname == "piechartpython":
  ax.pie(y, autopct="%1.1f%%")
elif fname == "piechartpython":
  ax.pie(y, labels=lbls)

fig.savefig(place + "plot" + midsuff + ".png", bbox_inches="tight")


# 1. Load the image
img = mpimg.imread(place + 'plot' + midsuff + '.png')

# 2. Process or manipulate the image (Optional)
# For example, let's look at the shape or apply changes
#print(f"Image shape: {img.shape}")

# 3. Create the plot without displaying it
plt.imshow(img)
plt.axis('off')  # Hide axis lines and labels

# 4. Save the result to a file instead of showing it
plt.savefig(place + 'output_image' + midsuff + '.png', bbox_inches='tight', pad_inches=0)
plt.close()


# Open the PNG image file
with Image.open(place + "plot" + midsuff + ".png") as img:
    # Display the image using your system's default viewer
    img.show()
    
    # Optional: Load the data into memory so you can safely close the file
    img.load() 


exit()


  
