Plotly Bar Graph is NOT showing expected values
I am trying to add Plotly Bar graph in a div to a CSPpage. I am working in IRIS 2022.1. I created persistent class.
I copied relevant code into github repo:
oliverwilms/iris-python-plotly
iris-python-plotly/csp/otwPlotly.csp at master · oliverwilms/iris-python-plotly
Set tPlotlyDiv = ##class(dc.python.test).PlotlyDiv(trnYear,trnMonth)
ClassMethod PlotlyDiv(pTrnYear = 2025, pTrnMonth = 8) As %String
{
Set importlib = ##class(%SYS.Python).Import("importlib")
Set plotdiv = ##class(%SYS.Python).Import("plotdiv")
Do importlib.reload(plotdiv)
Set div = plotdiv.gendiv(pTrnYear,pTrnMonth)
Return div
}
iris-python-plotly/python/plotly.py at master · oliverwilms/iris-python-plotly
Comments
The Graph looks like this:
.png)
Here is my data:
.png)
This line in plotly.py generates a dataframe:
df = iris.sql.exec(query).dataframe()
where query = "SELECT Top 10 Category, Credit, Debit, TrnCount FROM dc_iris.trncount where TrnYear=2025 and TrnMonth=8 Order By Debit DESC"
The bars in the graph reflect rows 0 to 9, not the amount in 'Debit'
Here is your Plotly code.
fig = go.Figure(
data=[
go.Bar(
y=df['category'],
x=df['debit']
)
],
layout=go.Layout(
title='Total Monthly Cost'
)
)
You should convert df to a list to pass the data to the graph. Since it takes an array, it cannot display a data frame.
Try it like this:
df['category'].tolist()
df['debit'].tolist()
It works. Thank you, Dmitrij
💡 This question is considered a Key Question. More details here.