Use AJAX for chart plotting to make pages more responsive
When charts are active there can be a significant delay between the user clicking something and the data and charts appearing.
It would be better if the tabulated data appeared straight away, followed by the charts when the server has finished its calculations.
It should be possible to do this using AJAX.
https://code.djangoproject.com/wiki/AJAX ?
http://www.w3schools.com/ajax/
http://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications
Comments (87)
-
reporter -
reporter http://www.dajaxproject.com/
-
I think when I was looking at this, the dajax project had been dumped - check out the github.
I don't think there actually is anything you need to pull in for this - we already have jquery and I think that is all you will need. I'm hoping that a simple implementation won't be much more than a
$(document).ready(function(){ ... }
wrap around your code. Or maybe I'm being a little naive? -
reporter Hi @edmcdonagh. The cause of the delay in a chart-containing page appearing is the wait for the database to return data to views.py (the rendering of the chart once the data has arrived is pretty quick). Waiting for data from the database isn't helped by wrapping the chart JavaScript code in a ready function as you suggest. I think.
-
Yeah, it will be something like rendering the page with just the tabular information and a chart holding area, then triggering a different view to go off and get the data for the charts. Down the line, as it is just for prettiness, but I'd say that if you have charts plotted, you aren't really as interested in the listing, so it might be smart to minimise that block? Or putting the charts at the top above the listing, with some sort of waiting indication in that region whilst it is computing?
-
reporter I've been looking into Django / AJAX things and all I've achieved so far is an increased level of confusion.
Most of the Django / AJAX stuff that I've found seems to use AJAX to deal with a form. I want to be able to use AJAX from the first initial view of the pages, without the user submitting a form.
I think that there will need to be a view that returns the information to populate the main body of the page, and the associated tabular data etc. This doesn't need to be AJAX. I think there then needs to be a second view that is called: this will return the chart data via AJAX, and the charts can be plotted once the data is available.
The problem with this is that I'm not sure that it's possible to have two views contributing to one Django html template.
It would be good to speak to someone that's done this sort of thing before, as I feel that I don't really know where to start. Any suggestions?
-
I became extremely confused for a long while, and was very frustrated that there were no examples that matched what I needed to do!
In the 207 branch I'm working on, the AJAX works like this:
- Browse to
admin/queryremote
URL - View
dicom_qr_page
(innetdicom/dicomviews.py
) thows updicomqr.py
with a form and the admin user details dicomqr.py
has a form for search details, and loadsdicom.js
dicom.js
has the ready function, and when you click on the submit button the javascript takes over in the form function, and loads theq_process
view - URL is in the html, javascript uses$(this).attr('action')
.q_process
view starts the task with Celery- if the
q_process
view returns success then thequery_progress
js is called from the form submit js query_progress
calls theadmin/dicomstore/ajax_test3
URL every so many milliseconds, which loads theajax_test3
view which queries the database to see how the task is progressing.
Each of these views pass data back via json to the javascript to populate the
qr-status
div in the template.So, this does use a form, but the form isn't really relevant to the process. A button to launch the chart would make it directly comparable, but I assume you could launch automatically on load.
Unfortunately I've a lot of unused code and inappropriate function names, so it's not as easy to follow as I'd like!
Not sure if this is at all helpful to you, I hope it is a little!
- Browse to
-
reporter Thanks for that - I'll take a look. I'm glad that you were initially confused too!
-
reporter Chart data is only calculated if
response.is_ajax()
is true. The chart data is returned via aJsonResponse
ifresponse.is_ajax()
, otherwise just the tabulated data is returned via arender_to_response
pointing to the appropriate html page. I think that this will let me use the same view for the tabulated data and the later Ajax call to obtain chart data, with different types of return depending on the request type. I haven't written any JavaScript to make the Ajax call yet. References issue#253.→ <<cset fc019b2c2f6f>>
-
reporter I think I'm going to need a separate view for the chart data, as the
DXSummaryListFilter
andDXChartOptionsForm
items that I am putting in the current viewreturnStructure
are not JSON serializable.I'll create a new view that just sends back the plot data.
-
reporter Removed chart data calculation from the
dx_summary_list_filter
view. Created new view calleddx_summary_chart_data
that will eventually calculate chart data and return it via a JsonResponse. Added a bit of JavaScript todxfiltered.html
that will need to call the new view. Mostly skeletons at the moment. References issue#253.→ <<cset 51055be28582>>
-
reporter Trying to get AJAX working - without success. I have created a new view that tries to return the required chart data via JSON. However, no data is ever recieved by the JavaScript in the DXfiltered.html page. Don't know why. References issue
#253.→ <<cset 8a52cf935233>>
-
reporter AJAX response back to the html template now working. Need to work out how to trigger the AJAX to get the chart data without having to click on a button so that when the page initially comes up the chart data is polled. Also need to work out how to use the returned JSON data to populate the JavaScript arrays for the charts. References issue
#253.→ <<cset 4d30fe2eeef9>>
-
reporter Sorted out how to get the JSON data into JavaScript arrays. References issue
#253.→ <<cset 87fb2fdd79bc>>
-
reporter Populated JavaScript arrays for one of the plots. Seems that the AJAX is a bit intermittent... References issue
#253.→ <<cset 93f6336ac138>>
-
reporter Moved AJAX JavaScript into its own file. Chart data is now returned via AJAX when the page is initially loaded. Chart data is also updated when the user submits the form. Something isn't quite right though, but it is an improvement. Need to work out how to get the charts to use the data that is send back via AJAX. References issue
#253→ <<cset c8cb8d6aafa8>>
-
reporter Almost working... One of the DX charts now updates after a form submission. Doesn't populate to begin with though... References issue
#253→ <<cset e2d33b36ccfd>>
-
reporter Tabulated data and one radiographic chart now updated using AJAX on form submission. Haven't got chart data to appear when page is first loaded yet. Also need to work out how to populate the chart's drilldown data. The tabulated data is updated by replacing a DIV's html with new html provided by the view. References issue
#253→ <<cset 4f03fa3b262b>>
-
reporter Chart and tabulated data now updated on initial page load. To do: update top row showing number of studies; update chart options and chart display. Sanity check: is this a sensible direction to be going in? References issue
#253→ <<cset 8a74e0395e84>>
-
It looks like it'll be ace. Do you have it on the demo server?
-
reporter Chart and tabulated data now being updated correctly on initial page view and subsequent form submissions. Only works for plot of DAP per mean acquisition protocol at the moment. Drilldown data not currently updated. References issue
#253→ <<cset 68a09ffb7b39>>
-
reporter Drilldown works now. References issue
#253→ <<cset a661ed10b97c>>
-
reporter Plot of DAP vs acquisition protocol now working for mean, median and both. References issue
#253→ <<cset 9ac864c96857>>
-
reporter Plot of acquisition protocol frequency now working. Removed redundant code from the html template for the two working plots. References issue
#253→ <<cset cc1dceb30039>>
-
reporter Plot of kVp and mAs vs acquisition protocol and study workload now working. References issue
#253→ <<cset 99b42fbc932e>>
-
reporter Fixed typo in kVp data that was preventing the sorting from working. References issue
#253→ <<cset 61f072765742>>
-
reporter Mean DAP over time now working. Add radiographic charts now working with AJAX. References issue
#253→ <<cset ea394f885424>>
-
reporter Need to tweak the plot of DAP over time so that when there is no data in a given time period it doesn't plunge to zero.
-
reporter Plot of DAP over time not working for median or both selection. Will fix after holidays.
-
reporter Added check for zero values on plot of DAP over time. If found, replace with so that a gap is left in the line. Fixed plot of median DAP over time: the median values are returned as strings: they are now converted to a float. References issue
#253.→ <<cset 59c3f678005a>>
-
reporter Tidied up variable declarations. Kept some variables as global so that chart sorting works. Combined the code for mean and median DAP over time into one routine. References issue
#253.→ <<cset b9bf5b2483a4>>
-
reporter Fixed error in study workload piechart drilldown that my earlier changes had caused. References issue
#253.→ <<cset d10620caebbf>>
-
reporter http://djp-openremdemo.rhcloud.com/openrem/ reflects the current status of this.
-
reporter Removed reliance on global JavaScript variables to hold chart data for all radiographic plots. These changes will have broken the sorting of the CT charts until I update them too. References issue
#253.Apparently global variables are a bad idea: http://www.w3.org/wiki/JavaScript_best_practices#Avoid_globals
→ <<cset c5df4b6b4246>>
-
reporter The JavaScript for chart plotting used to be constructed in the dxfiltered.html page using Django template code. It produced a large amount of badly-formatted JavaScript. The change to using AJAX has the side-effect that all this ugly JavaScript is gone. I suspect this may have speeded things up a little, but I'm not certain. Sort-of references issue
#217 -
reporter Made AJAX changes for CT chart of DLP vs acquisition protocol. References issue
#253.→ <<cset 92e36b7e862b>>
-
reporter Made AJAX changes for CT chart of CTDI and CTDI+DLP vs acquisition protocol. References issue
#253.→ <<cset e8f72d08833d>>
-
reporter Made AJAX changes for CT chart of acquisition frequency and DLP per study type. References issue
#253.→ <<cset db47fa48ba43>>
-
reporter Made AJAX changes for CT chart of study frequency. References issue
#253.→ <<cset 215de6dafe7e>>
-
reporter Made AJAX changes for CT chart of request DLP and frequency. References issue
#253.→ <<cset 99703a0a39c5>>
-
reporter Made AJAX changes for CT chart of study workload. References issue
#253.→ <<cset 53fb3e8b871b>>
-
reporter Made AJAX changes for CT chart of DLP over time. That's the last of the charts - all are now AJAX. References issue
#253.→ <<cset 085eb553af25>>
-
reporter Made AJAX changes for CT chart of CTDI per acquisition protocol. That's really the last of the charts - all are now AJAX. Need to fix the sorting for the combined DLP and CTDI plot (4 series), then all will be done. References issue
#253.→ <<cset 44a93bffb689>>
-
reporter http://djp-openremdemo.rhcloud.com/openrem/ reflects the current status of this.
-
reporter 4-series sorting now works for the combined DLP CTDI mean and median plot. References issue
#253.→ <<cset 8033c03a79e3>>
-
reporter - changed status to resolved
All charts now feeding data using AJAX.
-
Chart data is only calculated if is true. The chart data is returned via a if , otherwise just the tabulated data is returned via a pointing to the appropriate html page. I think that this will let me use the same view for the tabulated data and the later Ajax call to obtain chart data, with different types of return depending on the request type. I haven't written any JavaScript to make the Ajax call yet. References issue
#253.→ <<cset fc019b2c2f6f>>
-
Removed chart data calculation from the dx_summary_list_filter view. Created new view called dx_summary_chart_data that will eventually calculate chart data and return it via a JsonResponse. Added a bit of JavaScript to dxfiltered.html that will need to call the new view. Mostly skeletons at the moment. References issue
#253.→ <<cset 51055be28582>>
-
Trying to get AJAX working - without success. I have created a new view that tries to return the required chart data via JSON. However, no data is ever recieved by the JavaScript in the DXfiltered.html page. Don't know why. References issue
#253.→ <<cset 8a52cf935233>>
-
AJAX response back to the html template now working. Need to work out how to trigger the AJAX to get the chart data without having to click on a button so that when the page initially comes up the chart data is polled. Also need to work out how to use the returned JSON data to populate the JavaScript arrays for the charts. References issue
#253.→ <<cset 4d30fe2eeef9>>
-
Sorted out how to get the JSON data into JavaScript arrays. References issue
#253.→ <<cset 87fb2fdd79bc>>
-
Populated JavaScript arrays for one of the plots. Seems that the AJAX is a bit intermittent... References issue
#253.→ <<cset 93f6336ac138>>
-
Moved AJAX JavaScript into its own file. Chart data is now returned via AJAX when the page is initially loaded. Chart data is also updated when the user submits the form. Something isn't quite right though, but it is an improvement. Need to work out how to get the charts to use the data that is send back via AJAX. References issue
#253→ <<cset c8cb8d6aafa8>>
-
Almost working... One of the DX charts now updates after a form submission. Doesn't populate to begin with though... References issue
#253→ <<cset e2d33b36ccfd>>
-
Tabulated data and one radiographic chart now updated using AJAX on form submission. Haven't got chart data to appear when page is first loaded yet. Also need to work out how to populate the chart's drilldown data. The tabulated data is updated by replacing a DIV's html with new html provided by the view. References issue
#253→ <<cset 4f03fa3b262b>>
-
Chart and tabulated data now updated on initial page load. To do: update top row showing number of studies; update chart options and chart display. Sanity check: is this a sensible direction to be going in? References issue
#253→ <<cset 8a74e0395e84>>
-
Chart and tabulated data now being updated correctly on initial page view and subsequent form submissions. Only works for plot of DAP per mean acquisition protocol at the moment. Drilldown data not currently updated. References issue
#253→ <<cset 68a09ffb7b39>>
-
Drilldown works now. References issue
#253→ <<cset a661ed10b97c>>
-
Plot of DAP vs acquisition protocol now working for mean, median and both. References issue
#253→ <<cset 9ac864c96857>>
-
Plot of acquisition protocol frequency now working. Removed redundant code from the html template for the two working plots. References issue
#253→ <<cset cc1dceb30039>>
-
Plot of kVp and mAs vs acquisition protocol and study workload now working. References issue
#253→ <<cset 99b42fbc932e>>
-
Fixed typo in kVp data that was preventing the sorting from working. References issue
#253→ <<cset 61f072765742>>
-
Mean DAP over time now working. Add radiographic charts now working with AJAX. References issue
#253→ <<cset ea394f885424>>
-
Added check for zero values on plot of DAP over time. If found, replace with so that a gap is left in the line. Fixed plot of median DAP over time: the median values are returned as strings: they are now converted to a float. References issue
#253.→ <<cset 59c3f678005a>>
-
Tidied up variable declarations. Kept some variables as global so that chart sorting works. Combined the code for mean and median DAP over time into one routine. References issue
#253.→ <<cset b9bf5b2483a4>>
-
Fixed error in study workload piechart drilldown that my earlier changes had caused. References issue
#253.→ <<cset d10620caebbf>>
-
Removed reliance on global JavaScript variables to hold chart data for all radiographic plots. These changes will have broken the sorting of the CT charts until I update them too. References issue
#253.→ <<cset c5df4b6b4246>>
-
Made AJAX changes for CT chart of DLP vs acquisition protocol. References issue
#253.→ <<cset 92e36b7e862b>>
-
Made AJAX changes for CT chart of CTDI and CTDI+DLP vs acquisition protocol. References issue
#253.→ <<cset e8f72d08833d>>
-
Made AJAX changes for CT chart of acquisition frequency and DLP per study type. References issue
#253.→ <<cset db47fa48ba43>>
-
Made AJAX changes for CT chart of study frequency. References issue
#253.→ <<cset 215de6dafe7e>>
-
Made AJAX changes for CT chart of request DLP and frequency. References issue
#253.→ <<cset 99703a0a39c5>>
-
Made AJAX changes for CT chart of study workload. References issue
#253.→ <<cset 53fb3e8b871b>>
-
Made AJAX changes for CT chart of DLP over time. That's the last of the charts - all are now AJAX. References issue
#253.→ <<cset 085eb553af25>>
-
Made AJAX changes for CT chart of CTDI per acquisition protocol. That's really the last of the charts - all are now AJAX. Need to fix the sorting for the combined DLP and CTDI plot (4 series), then all will be done. References issue
#253.→ <<cset 44a93bffb689>>
-
4-series sorting now works for the combined DLP CTDI mean and median plot. References issue
#253.→ <<cset 8033c03a79e3>>
-
reporter Added animated gif that is displayed in the centre of the screen whilst waiting for chart data. References issue
#253.→ <<cset 6f732718b902>>
-
reporter Forgot to add the updated css file (Added animated gif that is displayed in the centre of the screen whilst waiting for chart data. References issue
#253.)→ <<cset 1d662d1e4a88>>
-
reporter Replaced loading gif with one that has a transparent background. Simplified the css. References issue
#253.→ <<cset 10216beed7bd>>
-
reporter @edmcdonagh, http://djp-openremdemo.rhcloud.com/openrem/ct/ is up to date with this latest commit. Any thoughts on the animation whilst the page waits for the chart data?
-
I like, but I'd like it better if the charts, and the annimation, pushed the tabular data down the page.
-
reporter Something like it is now? [or not - they're at the top and bottom... Odd - I moved the
plotdata
block into thetoprow
block. This did move the charts up to the top, but rather strangely there were also the chart titles below the tabulated data too. Don't understand why that was. Moved them back to where they were for the time being.] -
Ok. Just checked it after seeing the email, and the wait logo was at the top over the tabular data, which is fine, but the charts were still at the bottom. Now I've read your update and it makes sense.
-
reporter I need to change the location of the
plotdata
block infilteredbase.html
, rather than indxfiltered.html
andctfiltered.html
. The presence of aplotdata
block infilteredbase.html
explains why I saw duplicate chart titles at the top and bottom of the tabulated data yesterday. I'd forgotted about thebase
templates. -
reporter Added a line to remove the animated gif from the screen if something goes wrong with retrieving the chart data via AJAX. References issue
#253.→ <<cset c733213fc234>>
-
reporter Added a polite user message if something goes wrong with fetching the chart data. References issue
#253→ <<cset 682c7c485f02>>
-
- changed milestone to 0.7.0
- Log in to comment
https://github.com/yceruto/django-ajax