Public
Edited
Aug 12
1 fork
Insert cell
Insert cell
workbook = FileAttachment("Sample - Superstore (1).xlsx").xlsx()
Insert cell
workbook.sheetNames
Insert cell
data = workbook.sheet(0, {
headers: true,
// range: "A1:J10"
})
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
// make grouped bar chart of Sales by Region x Segment
Plot.plot({
title: "Sales by Region & Segment", //set title
subtitle: html`<h3 style = "margin-bottom: 15px">Total Sales, All Dates</h3>`, //set subtitle with additional html styling
style: {fontSize: "14px"}, //set font size for entire chart
marginTop: 40, //set margin around plot
marginBottom: 60,
marginLeft: 75,
x: {label: null},
y: {label: "Sales ($)"},
color: {legend: true}, // turn on legend
marks: [
Plot.barY(stackedBarData,
Plot.groupX(
{y: "sum"}, // aggregation method (mean, median, etc also avaiable)
{x: "Region", // x-axis (group-by)
y: "Sales", // y-axis
fill: "Segment", opacity: 0.8, // color-by
tip: true})), // turn on tooltip
Plot.text(dataLabel, {x: "Region", y: "Sales", text: d => d3.format("$,d")(d.Sales), dy: -6, lineAnchor: "bottom"}),
Plot.ruleY([0])
],
grid: true
})
Insert cell
// checkbox to control chart
viewof checkboxes = Inputs.checkbox(
Array.from(new Set(data.map(d => d["Segment"]))),
{label: "Select some",
value: ["Consumer", "Corporate", "Home Office"]
}
)
Insert cell
// filtered data to go into chart
stackedBarData = data.filter(d =>
checkboxes.includes(d["Segment"])
)
Insert cell
// group and sum data to make data labels
groupedSums = d3.rollups(
stackedBarData,
v => d3.sum(v, d => d.Sales), // reducer: sum the 'value'
d => d.Region // group by 'gender'
)
Insert cell
// convert to array to put into data label
dataLabel = Array.from(groupedSums, ([Region, Sales]) => ({ Region, Sales }))
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