Public
Edited
Oct 3, 2024
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sports = ['rowing','volleyball','gymnastics']
Insert cell
olympians_subset = olympians.filter(d=>sports.includes(d.sport) && ['USA','BRA','RUS','CHI'].includes(d.nationality))
Insert cell
Insert cell
table = aq.from(olympians_subset)
Insert cell
Insert cell
results = table
//set up dynamic parameter, map to Observable input selectedGrouping
.params(
{ group_var: selectedGrouping}
)
//rename x axis variable to "name" and grouping variable to "group"
.rename({sport: 'name'})
//derive similar to R dplyr mutate, use it to create a new group variable, map to parameter
.derive({group: (d, $)=> d[$.group_var]})
//group by x axis variable ("name") and your grouping (e.g. sex)
.groupby(['name','group'])
//create aggregated variable (y) with rollup calc e.g. mean
.rollup({
y: d=> op.round(op.mean(d.weight) * 10) / 10
})
//group again by your grouping variable, create nested array with array_agg
.groupby('group')
.rollup({
data: d => op.array_agg(op.row_object())
})
//rename grouping variable
.rename({group: 'name'})
.objects();
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Highcharts.chart('plot', {
chart: {
type: 'column'
},
title: {
text: 'Average Olympain Weight by Sport',
align: 'left'
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Weight (kg)'
}
},
plotOptions: {
column: {
dataLabels: {enabled: true}
}
},
series: results
})
Insert cell
test = [{month: 'Jan', group:'A', y: 2}, {month: 'Mar', group: 'A', y: 4},{month: 'Jun', group: 'B', y: 5}]
Insert cell
months = [{'month': 'Jan'},{'month':'Feb'}, {'month': 'Mar'}, {'month': 'Apr'}, {'month': 'May'}, {'month': 'Jun'}]
Insert cell
group = [...new Set(test.map(d => d.group))];

Insert cell
aq.from(months)
.join_left((
aq.from(test)
.groupby(['month'])
.rollup({y: d=> aq.op.sum(d.y)})
))
.derive({
y: d=> d.y >0 ? d.y : 0
})
.objects()
Insert cell
generateDateSequence('2023-08-01','2023-10-23','week')
Insert cell
function generateDateSequence(startDate, endDate, interval) {
const result = [];
let currentDate = new Date(startDate);
const finalDate = new Date(endDate);

// Function to set the date to the next Monday if the interval is 'week'
function moveToMonday(date) {
const dayOfWeek = date.getDay();
const diff = (dayOfWeek === 0 ? 7 : dayOfWeek) - 1; // Move back to previous Monday
date.setDate(date.getDate() - diff);
}

if (interval === 'week') {
moveToMonday(currentDate);
}

while (currentDate <= finalDate) {
result.push(new Date(currentDate)); // Push a copy of the current date

// Increment based on the interval
if (interval === 'day') {
currentDate.setDate(currentDate.getDate() + 1);
} else if (interval === 'week') {
currentDate.setDate(currentDate.getDate() + 7);
} else if (interval === 'month') {
currentDate.setMonth(currentDate.getMonth() + 1);
}
}

return result.map(date => ({
date: new Date(date)
}))
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more