Slicers are a newly added feature to Spread.Sheets that gives users the ability to filter their table data in a new way. With SpreadJS V11, we have also added chart support, which can be combined with slicers to give a better visual representation of filtered data.
Download the sample
Setup
Create an HTML file and add the Spread.Sheets script and style references. For this page, we will need to reference the main Spread.Sheet script as well as the chart script:
Then add a DIV element to host the Spread.Sheets instance:
Now we can initialize the Spread component. For this, we will separate the process into 4 functions: the window’s onload function, initSpread, initSheet, and initChart. The onload function will be the starting function:
That function calls the initSpread function, which we will use to get the active sheet and then initialize the sheet and the chart:
When initiating the sheet, we will want to add a data-bound table. In this sample, the data is in a separate JavaScript file, so reference that in the page:
Now we can create a table and we can use that function to load the data and bind it to a table in the initSheet function:
Add Slicer
The data in this sample is census data of the age of the population of the United States, organized by State. Since there is a lot of data to look at, having a slicer to filter out the specific data that you want can be really helpful. We can add a slicer with just a few lines of code in the initSheet function:
Add Chart
Now we can add a chart to Spread. If the chart is bound to the table, then it will automatically be updated whenever the slicer is changed. In the initChart function we will add the table-bound chart, which will add the data to the chart. In order to ensure the series names are added, be sure to use “table1[#all]” as the table name to bind to.
Finally, we can add some code to get rid of gridlines and headers to make it look more presentable (add this code to the initSheet function):
By combining SJS built-in charts and slicers, developers can provide their users with powerful data analysis tools with just a small amount of code.