Table of Contents
7,07/24/05,00:00:00,0 9,07/24/05,00:00:00,0 7,07/24/05,00:30:00,1 9,07/24/05,00:30:00,0 7,07/24/05,01:00:00,0The data is a comma-separated table. The first column is 7 for outflow and 9 for inflow. We’ll plot only the outflow. The second and third columns are date and time. The last column is the count of people.
npm i -D webpack-dev-server html-webpack-plugin path webpack-cli fusionchartsCreate a dist and src sub-directory in the project directory. Also, create an empty file index.js, which would contain the source code for the project. Next, tt the console, type the following:
mkdir src touch src/index.js mkdir dist
// webpack.config.js
const HtmlWebPackPlugin = require( 'html-webpack-plugin' );
const path = require( 'path' );
module.exports = {
context: __dirname,
entry: './src/index.js',
output: {
path: path.resolve( __dirname, 'dist' ),
filename: 'main.js',
},
plugins: [
new HtmlWebPackPlugin()
],
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
},
proxy: {
'/UCIAPI': {
target: 'https://archive.ics.uci.edu',
pathRewrite: { '^/UCIAPI': 'https://archive.ics.uci.edu/ml/machine-learning-databases/event-detection/CalIt2.data'},
changeOrigin: true,
},
},
}
}; The configuration above includes a proxy to prevent CORS errors when reading data from the UCI machine learning repository. // src/index.js
//Import section
import FusionCharts from 'fusioncharts/core';
import TimeSeries from 'fusioncharts/viz/timeseries';
import DataStore from 'fusioncharts/datastore';
//Add the div tag for the chart container
const myDiv = document.createElement('div');
myDiv.id = 'container';
document.body.appendChild(myDiv);
//Set up the schema for two table columns
let schema = [{
"name": "Date",
"type": "date",
"format": "%-m/%-d/%Y %-I:%-M:%-S"
}, {
"name": "Outflow of People",
"type": "number"
}]
//main function read data and call renderPage
async function main() {
//Get the data
let response = await fetch('/UCIAPI');
let text = await response.text();
if (response.ok){
renderPage(text);
}
else {
alert('Error reading data from ML repository');
}
}
//Convert the UCI ML data to two column table and draw chart
//renders the html page when passed data as text
function renderPage(text){
//Convert data to table
var data = textToMatrix(text);
//Draw teh chart with this data
drawChart(data);
}
//convert text to matrix. The data read from UCI ML repository is comma separated
function textToMatrix(text){
var matrix = [];
var rows = text.split("\n");
for(var i=0;i<rows.length;i++){
var cols = rows[i].split(',');
//7 is out flow in CalIt2.data
if (cols.length > 1 && cols[0] == 7)
var dataRow = [cols[1].concat(' ', cols[2]), parseInt(cols[3])]
matrix.push(dataRow);
}
return matrix;
}
//Render the final chart
function drawChart(data){
FusionCharts.addDep(TimeSeries);
let fusionDataStore = new DataStore();
let fusionTable = fusionDataStore.createDataTable(data, schema);
window.charInstance = new FusionCharts({
type: 'timeseries',
renderAt: 'container',
width: "90%",
height: 650,
dataSource: {
data: fusionTable,
caption: {
text: 'Outflow Of People From CalIt2 Building At UCI, Source: UCI ML repository'
}
}
});
//Render the chart
window.charInstance.render();
}
main(); npx webpack serve --mode=developmentYou can view the app in the browser by navigating to localhost:8080
To create pie charts and generate statistical data, you’ll find this list of the best…
To implement FusionCharts in a dynamically resizing flexbox layout, ensure that the chart's dimensions are…
At FusionCharts, we believe in empowering developers and businesses with cutting-edge data visualization tools that…
Ever had a data set that seemed more complicated than a Rubik's cube? You’re not…
We’ve all seen them in textbooks or presentations—those overlapping circles that simplify complex information into…
We’re excited to announce the upcoming release of FusionCharts v4.1—a groundbreaking step forward in the…