Have my graph and graph it too?
-
How can I generate a graph server-side, and update it client-side? My boss just handed me the job of creating graphs for one of our systems, but he'd like it to work with or without Javascript, so I can't just use one of the many javascript-based graphing libraries unless I also implement the graphs as static images. Flash is also out, since it won't work on the iPad. So, what I'm looking for is some way to generate a graph on the server (possibly as SVG?) and then some way to manipulate the graph with Javascript (the dataset gets updated about once every 5 seconds) so that users with Javascript enabled can see auto-updating graphs in the UI (and users with Javascript disabled will just see a static representation of the data as of when the page is loaded). Does this kind of thing exist? Or would I just be better implementing things twice?
-
Answer:
You can make this work with HighCharts. They have instructions on how to set up a http://www.highcharts.com/documentation/how-to-use#exporting to convert the output data into an SVG and push the same data to their client-side js library.
fvox13 at Ask.Metafilter.Com Visit the source
Other answers
The enterprise way that I've done this before is to have two APIs - getData() and getChart() - so that JS users can access the raw values via getData() and have an interactive experience, and non-JS users can get an image via getChart(). JFreeChart is a great library for server-side charts, and Flot.js is a great JS library for client-side charts. You can tune JFreeChart to match the styling of Flot.js and the two will be almost indistinguishable. Depending on how many requests you get to view the same charts/time ranges, you can also cache the chart images server-side to improve response times. One advantage of supporting server-side charts is for embedding in alternate applications/dashboards. Using JS means that you have to access via a browser, and sometimes that's not what you want. Or that you have to view it as the JS library renders it (with all the little decorations that may not be desired). For example, in a fancy dashboard-like website, you want a lot of thumbnail charts without decoration. Dynamically rendering 100 charts in a webpage like that every 5min is kinda sucky. Also, when viewing a lot of data, like 10 metrics over a year @ 5min resolution, in a 300x600 chart, is much cheaper to render as an image than trying to return all that data to the browser and render something client-side in JS. You should make it clear to your boss that server-side images are non-interactive, and client-side (JS) images are (can be) interactive, so there will be a varying customer experience.
jpeacock
Do everything on the server side, throw the graph in an <iframe> , and set the iframe to auto-refresh every 5 seconds. Works for everyone with or without JavaScript.
xbonesgt
I'm sorry if this doesn't answer the question, but if it were me I would really push back hard on this no-javascript requirement. Things like the https://developers.google.com/chart/ make this so incredibly easy that it's really a bit crazy to do anything else. Does he have real, recent analytic data that shows a significant portion of your users don't have javascript enabled? Even if such data exists, I would still argue that your initial release use the API. That way you get the kinks worked out in terms of pulling the right data and have a working prototype of the charts themselves that shows the data is sound (and which, oh by the way, will function beautifully for 99% of your users). And you will have done it in the 1/2 the time that it will take to implement a server-side solution. (Seriously, once you have your data the google API is butt easy and provides functionality that will be simply impossible with a generated image, this d3 thing is probably the same). If, at that point, he still insists you indulge him on his outdated notions regarding javascript*, then you can talk about spending your other 1/2 of your time developing an alternate process for the users than need it. * Again, maybe he has good technical reasons for the requirement. I can't know from where I'm sitting, but I am guessing this boss is not a developer and is working off assumptions that may have been true in the past but have since been overtaken by events.
Dano St
http://d3js.org/ might be useful? I've never used it but I've heard it's very robust.
Strass
SVG could work. A friend of mine built a time-series graph that rendered an initial graph as SVG and then updated it via JS. On Preview: He built http://synynyms.no.de/, but he was rendering all of the SVG in the browser. Harumph.
thebigdeadwaltz
Use client-side d3 to render server-generated, JSON-formatted data into a graph. Use Ajax requests to deliver fresh data every x seconds. If the request is successful, call your d3 graph function on the new data to redraw.
Blazecock Pileon
A note to the various d3 suggestions: Unless you know something I don't, d3 is a client-side JavaScript library. Which means that, unless it operates on SVG files (which I wouldn't if I were developing it, I'd just stick with JavaScript Canvas elements), it doesn't fulfill the primary requirement of the original poster: No JavaScript. I suspect that what fvox13 is looking for is a way to not do what I'm going to suggest: Bifurcate the pipelines. Using something like one of the graphing libraries that sits on top of libGD generate PNG or GIF graphs. Then build a JavaScript version using D3 or jqPlot or Flot or what-have-you that takes the JSON data and builds the graph client side, and hides the raster server-side version. Make sure that you can log the requests for the raster server-side version and for the JSON data so that in 6 months you can go back to your boss and say "really, I appreciate the non-JS compatibility, but the reality is that out of 2815 requests, we've had 3 that may be non-JS". But sometimes the more complex version (2 pipelines) is going to be the simpler one in the long-term.
straw
Oh, and since I didn't mash reload before I posted (hand slap), you could also use server-push instead of iframes.
straw
If you're comfortable with javascript you could try your hand at node.js which is basically javascript on the server. Probably what I would do is set up a basic html template, inject the JSON data into it, then create a canvas element chart based on that data and inject that between <noscript> tags. That way, you have an up-to-date (from the last refresh) chart if JS is not available, but you also have the data on the page that can be initially picked up and played with if JS is available. You can then use AJAX to grab more data from the server as and when you need it. If JS is available the static chart won't be visible.
urbanwhaleshark
Related Q & A:
- What is the best data model and database systems to store social graph?Best solution by Quora
- How To Use the Graph API to Upload Photos to a user’s profile?Best solution by Stack Overflow
- How to approach Dynamic graph related problems?Best solution by Computer Science
- How to find connected components of a random graph?Best solution by Mathematics
- How to create a graph using python?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.