2011-04-14 5 views
1

그래서 두 개의 뷰가 있습니다. 첫 번째 뷰는 요청시 HTML을 생성하고 두 번째 뷰는 첫 번째 뷰를 표시 할 차트를 생성합니다.Django에서 Matplotlib로 동적 차트 생성

HTML보기

def activation_signupcount(request): 

    if 'datestart' not in request.GET: 

     return render_to_response('activation/activation_signupcount.html', {'datestart':''}) 

    else: 

     datestart = request.GET['datestart'] 
     dateend = request.GET['dateend'] 

     return render_to_response('activation/activation_signupcount.html', {'datestart':datestart, 'dateend':dateend})# 

차트보기 페이지 activation/activation_signupcount.html에 따라서

def activation_signupcount_graph(request): 

    datestart = request.GET['datestart'] #this doesnt work 
    dateend = request.GET['dateend'] #this doesnt work 

    print datestart,dateend 

    # open sql connection 
    cursor = connection.cursor() 
    # execute query 
    cursor.execute("SELECT COUNT(1), JoinDate FROM users WHERE JoinDate BETWEEN '"+ datestart +"' AND '"+ dateend +"' GROUP BY JoinDate;") 
    # close connection 

    data = cursor.fetchall() 

    cursor.close() 
    connection.close() 

    fig = Figure() 
     ax = fig.add_subplot(111) 

    x = [] 
    y = [] 

    x = [k[1] for k in data] 
    y = [k[0] for k in data] 

    ax.plot_date(x, y, '-') 
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) 
    fig.autofmt_xdate() 
    canvas = FigureCanvas(fig) 
    response = HttpResponse(content_type='image/png') 
    canvas.print_png(response) 

    return response 

, 나는 2 개 날짜 필드, 시작하고 GET 요청을 제출 끝을 보유하고 있습니다. 그래서 내 질문은 어떻게 내 함수 activation_signupcount_graph 차트를 생성하는 시작/끝 날짜를 얻으려면이 두 날짜 변수를 구문 분석 할 수 있습니다?

나는 그것이 분명했으면 좋겠다!

답변

3

적절한 매개 변수가있는 url-templatetag을 사용하여 템플릿의 차트보기에 액세스 할 수 있습니다.

<img src="{% url yourapp.chart_view %}?datestart={{ datestart }}" /> 
+0

아 네 물론! 너무나 분명한 감사! – super9

0

가 pySVG 작동이 BTW뿐만 아니라 SVG 그래프를 생성 (년 동안이 일을 한 : 당신이 얻을 - 매개 변수를 사용하는 등,

<img src="{% url yourapp.chart_view start_date end_date %}" /> 

또는 :

그래서 같이한다), 최근에 나는 virtualenvs에 matplotlib을 설치하는데 많은 문제점을 겪었습니다.

나는 애국가 .... 설치 대신 보통 핍의 virtualenvs에 (우분투 저장소에서) 시스템 전체하기 matplotlib 라이브러리를 추가하는 의지

+0

흥미 롭습니다! 알아 둘만한. – super9

+0

나는 여전히 virtualenvs에서 matplotlib 그래픽을 사용하고 있습니다. 문제가 아직 발생하지 않았습니다. OSX와 pythonanywhere.com 호스팅 사용하기. –