Public
Edited
Dec 15, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
daily_orders_product
Type Table, then Shift-Enter. Ctrl-space for more options.

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
Insert cell
wordCountMapReducer = function (wordCountMap, word) {
if (wordCountMap.has(word)) {
wordCountMap.set(word, wordCountMap.get(word) + 1)
} else {
wordCountMap.set(word, 1)
}
return wordCountMap
}
Insert cell
Insert cell
function getUniqueValues(columnArray) {
const uniqueValues = new Set(columnArray);

const uniqueArray = [...uniqueValues];

return uniqueArray;
}
Insert cell
Insert cell
uniqueNames = getUniqueValues(daily_orders_product.map(item => item.name));
Insert cell
Insert cell
function getUniqueValuesWithAccumulatedSum(data) {
const uniqueValuesMap = new Map();

data.forEach(item => {
const name = item.name;
const orders = item.orders;

if (uniqueValuesMap.has(name)) {
uniqueValuesMap.set(name, {
count: uniqueValuesMap.get(name).count + 1,
accumulatedSum: uniqueValuesMap.get(name).accumulatedSum + orders
});
} else {
uniqueValuesMap.set(name, {
count: 1,
accumulatedSum: orders
});
}
});

const uniqueValuesArray = Array.from(uniqueValuesMap.entries());

return uniqueValuesArray;
}
Insert cell
Insert cell
daily_orders_product2 = daily_orders_product.map(item => {
item.name = item.name.replace('Extra Large', 'Extra_Large');
return item;
});
Insert cell
Insert cell
uniqueNames2 = getUniqueValuesWithAccumulatedSum(daily_orders_product2);
Insert cell
uniqueNames4 =uniqueNames2.map(([name, { count, accumulatedSum }]) => ({
name,
count,
accumulatedSum,
}));
Insert cell
uniqueNames7 = uniqueNames4.reduce((result, item) => {
const [mainName] = item.name.split(' ');
const mainNameLower = mainName.toLowerCase();

const pizzaSize = item.name.split(' ').pop();

const data11 = {
count: item.count,
accumulatedSum: item.accumulatedSum
};

if (!result[mainNameLower]) {
result[mainNameLower] = {
children: []
};
}

const existingPizza = result[mainNameLower];
const existingSize = existingPizza.children.find(entry => entry.name === pizzaSize);

if (existingSize) {
existingSize.data.push(data11);
} else {
existingPizza.children.push({
name: pizzaSize,
data: [data11]
});
}

return result;
}, {});
Insert cell
function uni8(inputObject) {
return Object.entries(inputObject).map(([name, { children }]) => ({
name,
children: children.map(({ name, data }) => ({
name,
children: data.map(({ count, accumulatedSum }) => ({
name: `Pizzas sold: ${accumulatedSum}`,
value: accumulatedSum
}))
}))
}));
}
Insert cell
data={
const uniqueNames8= {
name: "pizza",
children: uni8(uniqueNames7)
}
return uniqueNames8
}
Insert cell
Insert cell
Insert cell
function getUniqueValuesWithAccumulatedSumWeek(data) {
const uniqueValuesMap = new Map();

data.forEach(item => {
const day_of_week = item.day_of_week;
const revenue = item.revenue;

if (uniqueValuesMap.has(day_of_week)) {
uniqueValuesMap.set(day_of_week, {
number_of_days_open: uniqueValuesMap.get(day_of_week).number_of_days_open + 1,
accumulatedSum: uniqueValuesMap.get(day_of_week).accumulatedSum + revenue
});
} else {
uniqueValuesMap.set(day_of_week, {
number_of_days_open: 1,
accumulatedSum: revenue
});
}
});

const uniqueValuesArray = Array.from(uniqueValuesMap.entries());

return uniqueValuesArray;
}
Insert cell
Insert cell
weekdays2=getUniqueValuesWithAccumulatedSumWeek(daily_orders_product)
Insert cell
weekdays3 =weekdays2.map(([day_of_week, { number_of_days_open, accumulatedSum }]) => ({
day_of_week,
accumulatedSum,
number_of_days_open
}));

Insert cell
Insert cell
weekday= weekdays3.map(d => ({
day_of_week: d.day_of_week,
daily_revenue: parseFloat((d.accumulatedSum / d.number_of_days_open).toFixed(2)),
}));
Insert cell
Insert cell
Insert cell
cal_counties=d3.json("https://gis-calema.opendata.arcgis.com/datasets/59d92c1bf84a438d83f78465dce02c61_0.geojson?outSR=%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D")
Insert cell
Insert cell
import {colorOpacity} from '@mootari/color-opacity'
Insert cell
colores=colorOpacity({ max: 0})
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const width = 600;
const height = width;
const radius = width / 6;


const color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1));

const hierarchy = d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value);
const root = d3.partition()
.size([2 * Math.PI, hierarchy.height + 1])
(hierarchy);
root.each(d => d.current = d);


const arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius * 1.5)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, width])
.style("font", "10px sans-serif");

const path = svg.append("g")
.selectAll("path")
.data(root.descendants().slice(1))
.join("path")
.attr("fill", d => { while (d.depth > 1) d = d.parent; return color(d.data.name); })
.attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0)
.attr("pointer-events", d => arcVisible(d.current) ? "auto" : "none")

.attr("d", d => arc(d.current));

path.filter(d => d.children)
.style("cursor", "pointer")
.on("click", clicked);

const format = d3.format(",d");
path.append("title")
.text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);

const label = svg.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.style("user-select", "none")
.selectAll("text")
.data(root.descendants().slice(1))
.join("text")
.attr("dy", "0.35em")
.attr("fill-opacity", d => +labelVisible(d.current))
.attr("transform", d => labelTransform(d.current))
.text(d => d.data.name);

const parent = svg.append("circle")
.datum(root)
.attr("r", radius)
.attr("fill", "none")
.attr("pointer-events", "all")
.on("click", clicked);

function clicked(event, p) {
parent.datum(p.parent || root);

root.each(d => d.target = {
x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
});

const t = svg.transition().duration(750);

path.transition(t)
.tween("data", d => {
const i = d3.interpolate(d.current, d.target);
return t => d.current = i(t);
})
.filter(function(d) {
return +this.getAttribute("fill-opacity") || arcVisible(d.target);
})
.attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0)
.attr("pointer-events", d => arcVisible(d.target) ? "auto" : "none")

.attrTween("d", d => () => arc(d.current));

label.filter(function(d) {
return +this.getAttribute("fill-opacity") || labelVisible(d.target);
}).transition(t)
.attr("fill-opacity", d => +labelVisible(d.target))
.attrTween("transform", d => () => labelTransform(d.current));
}
function arcVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
}

function labelVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
}

function labelTransform(d) {
const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
const y = (d.y0 + d.y1) / 2 * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}

return svg.node();
}
Insert cell
Insert cell
Plot.plot({
title: "Daily revenue by weekday",
marginLeft: 60,
x: {reverse: true, label: ""},
y: { grid: true, label: "Revenue ($)" },
marks: [
Plot.barY(weekday, {
y: 'daily_revenue',
x: 'day_of_week',
tip: {
format: {
fill: false,
name: true,
daily_revenue: false,
}
},
fill: (d) => (d.day_of_week === 'Wednesday' || d.day_of_week === 'Thursday'? "red" : "black"),
channels: {daily_revenue: {value: 'daily_revenue'}, order: "descending"},
sort: {x: 'daily_revenue'}
}),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
title: "Store location in CA",
city: {legend: true},
marginLeft: 116,
marginTop: 16,
marginBottom: 30,
width: 900,
height: 910,
x: {label: "East"},
y: {label: "North"},
style: {
padding: 10,
color: "black",
background: `url(${await FileAttachment("imageedit_3_9594925531.png").url()})`,
backgroundSize: "cover",
marginBottom: 300,
},
marks: [
Plot.geo(cal_counties, { strokeOpacity: 0 }),
Plot.dot(stores, {x: "longitude", y: "latitude", fill: "#d002ad", r: 10, channels: {'Store in': "city"}, tip: {format:{x: false, y:false, city: true }}}),
Plot.axisX({
stroke: "white",
textStroke: "white",
textStrokeOpacity: 0,
color: "colores"
}),
Plot.axisY({
stroke: "white",
textStroke: "white",
textStrokeOpacity: 0,
color: "colores"
})
]
})
Insert cell
Insert cell
Insert cell
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