Insert cell
Insert cell
Insert cell
tmpq6k6kydy.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
rawData = FileAttachment("tmpq6k6kydy.csv").csv({typed: true});
Insert cell
rawData
Insert cell
Insert cell
// Import the vega-lite api
import {vl} from '@vega/vega-lite-api-v5'
Insert cell
import {printTable} from '@uwdata/data-utilities'
Insert cell
// have a look at the first 10 rows of data
printTable(rawData.slice(0,10));
Insert cell
dateSet = new Set();
Insert cell
// reformat data to {year/Month: {morning: number, afternoon: number, night: number}}
data = rawData.map(d => {
const timeStamp = d["DateTime_Measured"];
const electricityUsage = d["Total_Demand_KW"];

const year = timeStamp.slice(0, 4);
const month = timeStamp.slice(5, 7);
const hour = parseInt(timeStamp.slice(11, 13));
let timePeriod = null;

// check if it is morning[4-12pm), afternoon[12-8pm) or night[8-4am)
if (hour >= 4 && hour <12 ){
timePeriod = 'morning';
}else if (hour >= 12 && hour < 20){
timePeriod = 'afternoon';
}else{
timePeriod = 'night';
}
const date = new Date(year, month);
dateSet.add(date);

return {date, timePeriod, electricityUsage}
})
Insert cell
// group the objects together with the same date and timePeriod in data
groupData = new Object;
Insert cell
dateSet.forEach(date => groupData[date] = {morning: 0, afternoon: 0, night: 0});
Insert cell
groupData
Insert cell
data.forEach(d =>{
groupData[d['date']][d['timePeriod']] += d['electricityUsage'];
});
Insert cell
// have a look at the groupData
groupData
Insert cell
df = []
Insert cell
Object.entries(groupData).forEach(([key, value])=>{
// df.push({date: key, ...value, total: value['morning']+value['afternoon']+value['night']})
df.push({date: key, type: 'morning', electricityUsage: value['morning']});
df.push({date: key, type: 'afternoon', electricityUsage: value['afternoon']});
df.push({date: key, type: 'night', electricityUsage: value['night']});
})
Insert cell
df
Insert cell
// visualiza the data using vega-lite stacked bar chart
vl.markBar().width(900).height(600).data(df).encode(
vl.y().aggregate('sum').title('KW').field('electricityUsage'),
vl.x().timeUnit('yearmonth').field('date').title('Date'),
vl.color().field('type')
).render()
Insert cell
Insert cell
// visualize the data using vega-lite grouped bar chart
vl.markBar().data(df).encode(
vl.column().field("date").timeUnit('yearmonth').header({'orient': 'bottom'}),
vl.y().field('electricityUsage').type('quantitative'),
vl.x().field('type').axis(null),
vl.color().field('type')
).render()
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more