I would handle this on the server side. The simplest approach would be
data: {url: "service.php"}
and in your service.php
switch ($_REQUEST["unit"]) {
case "y":
case "M":
case "d":
print file_get_contents("cache-".$_REQUEST["unit"]);
break;
default:
/* to the nitty gritty data loading based on from/to and unit from the db */
print $generated_response;
}
Of course, you can do the same thing in the frontend by firing a XHR request for data to your server-side component that grabs data from db. Note, If chart requests:
from = value, to = from + x and if you return less than that (because there are too many dat points), so for instance you return from = from and to = to / 2, chart will send a new request where from = to / 2 and to = from + x - this way you can return smaller chunks if necessary and the chart will "appear" gradually which might be a better option than waiting for long time.
Have a look at this sql php service example:
Janis