There are a number of ways you could do this. I will you tell you one.
Create a controller class that has a method that is responsible for printing an array of numbers. Hit that endpoint via a javascript get request and do something with the returned data.
So you would create a routing file:
your_module.routing.yml
your_module.create_numbers:
path: '/your_module/{numbers}'
defaults:
_controller: '\your_module\Controller\YourCustomControllerClass::createNumberArray'
requirements:
_permission: 'access content'
YourCustomControllerClass.php
<?php
namespace 'your_module\Controller';
class YourCustomControllerClass extends ControllerBase {
/**
* Return a JSON response based on the number param
* @param $number integer The number passed into the route.
* @returns JsonResponse
*/
public function createNumberArray($number) {
$numbers = [];
for ($i = 0 ; $i < $number; $i++) {
$numbers[] = $i;
}
return new JsonResponse($numbers);
}
}
Now you can hit your route from Javascript. The cool thing about this is that your route can create dynamic arrays. If you notice in the routing file the numbers is in {}. That means it is dynamic and is passed in as an argument to the createNumbersArray method. It can then use it to create an array of any length.
your_module_graph.js
var numbers = 10;
// Note numbers can be set to whatever you want.
var jqxhr = $.get( "/your_module/" + numbers, function(data) {
// data will be a JSON object containing an array of numbers.
var parsedJson = JSON.parse(data);
// Now do what you want with parsedJson. Build your graphs etc.
});
I haven't tested that but have done similar things a lot lately and I'm quite confident that if not that exact code, the approach in general will suit your needs. As mentioned earlier there is no one way to do this but with Symfony now a core part of Drupal 8 it really is built for doing stuff like this. That is, taking HTTP requests and returning data.
Hope that helps.