Data, including stocks, weather, and sports scores, is most useful when it's constantly updated with new information. SpreadJS is a very versatile JavaScript spreadsheet component that can also easily consume, display, and provide real-time updates via data binding.
We will use WebSocket to get the real-time data from Finnhub.IO and then use basic SpreadJS functionality to showcase the data. To utilize the Finnhub Stock API, you will need to create a free account and generate your API key, which we will add later in this application.
In this tutorial, we'll use Node.JS Express with WebSocket, so be sure to install the latest version from here. We'll also be using Visual Studio Code, so run it as administrator so that the NPM commands will work in the terminal.
Download the sample zip for this tutorial to follow along.
In this blog, we will cover how to use JavaScript real-time data in a spreadsheet following the steps below:
- Set Up the Application
- Connect to the Datasource
- Use the Data in Spread
- Add Data for the Line Chart
- Add the Line Chart
- Run the Program
Ready to Test it Out? Download SpreadJS Today!
Set Up the Application
We can start by creating a folder for the application. In this case, we named it "Real Time Data." Next, we'll want to create a package.json file in that folder that will be used as the manifest file for our project. This can contain something similar to the following:
For this application, we will use Express as the web framework and WebSockets for real-time data, and we can install that simply with npm, which we will also use to install the SpreadJS files. In the Visual Studio Code terminal, you can type:
Once those are installed, we can create a file called "index.js" to use to set up our application. This should contain the following:
Now we can add the HTML file the application will serve. In this case, we can name the file "index.html" We can go ahead and add some code to our HTML file, including the script and CSS references to SpreadJS as well as some basic initialization code:
In the previous code snippet, we used spread.fromJSON() to load a template file. For this project, we created that template file to use as the basis for the stock ticker.
Using only the SpreadJS Designer, we created data labels and bindings for the data source, formatted cells, removed grid lines and headers, and added an area for the chart to go.
The file in this tutorial is provided, which is called "stockTemplate.js." To export to JS in the Designer, click File>Export and select "Export JavaScript File." We placed that template file in the same folder for this tutorial as our index.js and index.html files.
Back in the index.js file, we'll need to tell the program to serve the HTML file and the template by using the following code:
Going back to the index.html file, we can add the script to load that template file:
To finish the setup, we can initialize the variables we are going to need later on and create a drop-down cell to select the stock:
We can also set specific conditional formatting for the change in the open price.
Green = Positive
Red = Negative
Connect to the Data Source
Before actually writing code to connect to the data source, we'll want to add some code to handle when the user selects a stock from the drop-down list in Spread. Only then will we connect and get the data. We can do this by binding to the EditEnded event, looking up the stock symbol from our lookup array, and then connecting to that stock:
This calls a new function that we can create called "connectToDataSource":
This code connects to the data source using a WebSocket and passes in the stock symbol to subscribe to.
Note: When initializing the WebSocket, you would add the token you generated from Finnhub.IO.
In addition, there is a section about removing a chart called "line", which is used later to reset the chart once we add it since this function will be called every time the stock selection is changed.
When the program is connected to the data source and subscribed to a specific stock value, the program will receive updates from that data source in the form of JSON data, which we'll need to parse through to use in Spread. To do this, we can use an event listener to listen for a message from the WebSocket:
In the above code, we go through the data source and fill in some sample data in the sheet. We also called some functions that will be defined: bindData, addLineData, addChart, and setConditionalFormatting.
Use the Data in Spread
Before going through each function, the main structure of the program should be explained. Essentially, the data is bound directly to the first sheet in the Spread instance, "Stock_Ticker", via cell-level data-binding, an explanation of which can be found here.
The second sheet is a backlog of the open price value of the stock since the program started. Normally it would be best to keep track of the values recorded since a particular date, but to simplify this program, it is just based on the program start time, and in this case, only the ten most recent values.
This backlog of values is what the line chart will point to, essentially showing the changes in the value since the program started.
When the template is defined in the designer and the format matches that of the data source, it can be set in the sheet using the setDataSource function called in the "bindData" function.
In addition, we can set the data source for the second sheet, "Data_Sheet", and let the columns auto-generate from the data since we don't care about the formatting on that sheet:
Add Data for the Line Chart
The next function to define is the "addLineData" function, which uses the array defined earlier in this tutorial and adds a value to it every time a new value is received from the data source if that value is different from the one before:
Add the Line Chart
We can now create a JavaScript real-time chart by specifying the cell range in the "Data_Sheet" sheet bound to the lineData data source. We can also change the formatting of the title, axes, data labels, legend, and chart area–all inside of the "addChart" function:
Run the Program
With all of the code added, running the program is easy. Open the terminal in Visual Studio Code and type:
Then navigate to localhost:3000 in a web browser:
Select a stock from the drop-down menu to load the data:
This is a simple example of JavaScript real time data manipulation in SpreadJS, but it can be done in many ways. With the added power of charts and data binding in SpreadJS, you can do more than display the data.
Thanks for following along. Feel free to leave any comments below. Happy Coding!
Ready to Test it Out? Download SpreadJS Today!