Public
Edited
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
daily_orders
Type Table, then Shift-Enter. Ctrl-space for more options.

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

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Suma = Array.from(
d3.group(daily_orders,
d => d3.timeMonth(new Date(d.order_date)),
d => d.store_id
),
([mes, store_id]) => ({
mes,
store_id: Array.from(store_id, ([store_id, revenue]) => ({
store_id,
total: d3.sum(revenue, r => r.revenue)
}))
})
);

Insert cell
Estadisticas = Suma.map(d => ({
mes: d.mes,
min: d3.min(d.store_id, t => t.total),
max: d3.max(d.store_id, t => t.total),
mean: d3.mean(d.store_id, t => t.total)
}));
Insert cell
Insert cell
Dataset = daily_orders.map(d => {
return {
...d,
Tasa: d.revenue / d.orders
};
});
Insert cell
Insert cell
function CategorizarTasa(Tasa) {
if (Tasa < 22) return "12 a 22";
if (Tasa < 24) return "22 a 24";
if (Tasa < 26) return "24 a 26";
if (Tasa < 28) return "26 a 28";
return "28 a 48";
}
Insert cell
NuevoDataSet = Dataset.map(d => ({
...d,
TasaCategorias: CategorizarTasa(d.Tasa)
}));
Insert cell
Insert cell
normalizados = d3.rollup(
NuevoDataSet,
v => v.length / d3.sum(NuevoDataSet, d => d.store_id === v[0].store_id ? 1 : 0),
d => d.store_id,
d => d.TasaCategorias
);

Insert cell
resultado = Array.from(normalizados, ([store_id, categorias]) =>
Array.from(categorias, ([TasaCategorias, proporción]) => ({
store_id,
TasaCategorias,
proporción
}))
).flat();

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

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
marginLeft: 120,
x: {label: "Ganancias diarias (miles)", transform: (x) => x / 1000},
y: {label: null},
marks: [
Plot.ruleX([0]),
Plot.tickX(
daily_orders,
{x: "revenue", y: "store_id", strokeOpacity: 0.3}
),
Plot.tickX(
daily_orders,
Plot.groupY(
{x: "median"},
{x: "revenue", y: "store_id", stroke: "red", strokeWidth: 4, sort: {y: "x"}}
)
)
]
})
Insert cell
Insert cell
Plot.plot({
y: {
grid: true,
label: "Ganancias mensuales ($)"
},
x: {
label: "Fecha"
},
marks: [
// Área entre las ganancias mínimas y máximas
Plot.areaY(Estadisticas, { x: "mes", y1: "min", y2: "max", fillOpacity: 0.3, fill: "steelblue" }),

// Línea para las ganancias mínimas
Plot.lineY(Estadisticas, { x: "mes", y: "min", stroke: "blue", strokeWidth: 2 }),

// Línea para las ganancias máximas
Plot.lineY(Estadisticas, { x: "mes", y: "max", stroke: "green", strokeWidth: 2 }),

// Línea para la media
Plot.lineY(Estadisticas, { x: "mes", y: "mean", stroke: "red", strokeWidth: 2, strokeDasharray: "4,4" })
]
});

Insert cell
Insert cell
Insert cell
xyz = Plot.normalizeX("sum", {z: "store_id", x: "proporción", y: "store_id"});
Insert cell
Plot.plot({
height: 660,
axis: null,
grid: true,
x: {
axis: "top",
label: "Proporción normalizada (%)",
percent: true
},
color: {
scheme: "Spectral",
domain: [...new Set(resultado.map(d => d.TasaCategorias))],
legend: true
},
marks: [
Plot.ruleX([0]),
Plot.ruleY(resultado,
Plot.groupY({x1: "min", x2: "max"}, {...xyz, sort: {y: "x1"}})
),
Plot.dot(resultado, {
...xyz,
fill: "TasaCategorias",
title: d => `${d.store_id}: ${(d.proporción * 100).toFixed(2)}%`
}),
Plot.text(resultado,
Plot.selectMinX({...xyz, textAnchor: "end", dx: -6, text: "store_id"})
)
]
});

Insert cell
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