Sarath_Mj
Sarath_Mj

Reputation: 333

Embedding matplotlib moving chart in django template

I am new to python Django framework.

I have matplotlib moving or live chart, It should be embedded in django template. I checked many question relevant to this here but none of them is worked for me. Most of us suggested here is displaying as image, but problem is moving chart with default interface like zoom, drag everything to be integrated. i am struggling to solve . I used this code and got to know it will be displaying as image.

def simple(request):
    import random
    import django
    import datetime

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter

    fig = Figure()
    ax = fig.add_subplot(111)
    x = []
    y = []
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now += delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas = FigureCanvas(fig)
    response = django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

I used this code for reference.

if this is not understandable please comment below, ill try to convey you clearly

can any one help me to get fixed this. Thanks in advance.

Upvotes: 0

Views: 628

Answers (2)

predsar
predsar

Reputation: 66

Instead of doing with matplotlib you can try with BOKEH chart. check this out Bokeh charts doc

Upvotes: 2

Adam Barnes
Adam Barnes

Reputation: 3232

While it may seem like a simple question, the machinery that matplotlib employs to present an interactive chart on desktop is reasonably complex, and not something one can just drop in to some HTML somewhere.

I would suggest you look at a frontend library for graphically displaying data, then provide your frontend the data with your backend.

As for libraries, check out C3. It's got good docs and examples.

Upvotes: 1

Related Questions