1

I have managed to integrate a scatter plot into my Django application following the very helpful steps laid out here. I would now like to do the same for a bar chart but I have not been able to get the data to render on the page. Can someone explain where I am going wrong?

Here is my views.py:

import plotly.offline as opy
import plotly.graph_objs as go

class Graph(TemplateView):
    template_name = 'graph.html'
   
trace = go.Figure(
                data=[
                    go.Bar(
                        name="Original",
                        x=data[1,2,3,4],
                        y=data[1,2,3,4],
                        offsetgroup=0,
                    ),
                ],
                layout=go.Layout(
                    title="Bar Chart",
                    yaxis_title="y axis values"
                )
            )

bar_div = opy.plot(trace, auto_open=False, output_type='div')

context['bar_div'] = bar_div

return context

Thanks!

Eddyhut
  • 55
  • 11
  • Since version 4 plotly is offline only so you can get rid of `import plotly.offline as opy` Why don't you save your plot as html and show on a iframe? – rpanai Jul 24 '20 at 13:50
  • Thanks - this solution is less convenient as the actual data I am trying to visualise is from my SQL db backend and I believe that your solution would require me to build two separate apps with two different dbs? – Eddyhut Jul 24 '20 at 15:00
  • I used this and it was working without problems, but now the figures don't stretch to 100% of the surrounding div? – Soerendip Nov 21 '20 at 20:36

1 Answers1

0

The following code worked for me to send a bar chart to my template :)

Make sure that you send the output_type as a div then you can render it through your context dictionary. Fig essentially holds all the parameters of my bar chart (data, layout etc.,)

plot(fig, output_type='div')
nance
  • 11
  • 2