Macro in Action: HTML & Iframe Embed
Below is example visualization with D3.js 👇
Html macro |
---|
<style>
#chart {
height: 500px;
width: 500px;
margin: 0 auto;
}
#line-chart {
height: 200px;
width: 800px;
margin: 0 auto;
}
</style>
<script src="https://d3js.org/d3.v6.min.js"></script>
<div id="chart"></div>
<div id="line-chart"></div>
<script>
const data =
{
"name": "flare",
"children": [
{
"name": "A",
"children": [
{
"name": "Hello",
"value": 60
},
{
"name": "Nar",
"value": 50
},
{
"name": "thanks",
"value": 23
},
{
"name": "for",
"value": 15
},
{
"name": "helping",
"value": 10
},
]
},
{
"name": "B",
"children": [
{
"name": "Hope",
"value": 32
},
{
"name": "this",
"value": 14
},
{
"name": "works",
"value": 9
},
]
},
{
"name": "C",
"children": [
{
"name": "More",
"value": 19
},
{
"name": "fake",
"value": 15
},
{
"name": "data",
"value": 10
},
]
},
]
}
const lineChartData = {
"A": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"B": {
"data": [
3, 5,2,4 , 6, 2, 5, 3
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"C": {
"data": [
5, 4, 3, 6, 2, 5, 4
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"Hello": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"Nar": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"thanks": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"for": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"helping": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"Hope": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"this": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"works": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"More": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"fake": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
"data": {
"data": [
1, 2, 3, 4, 5, 6, 7
],
"dates": [
"2023-04-30",
"2023-05-31",
"2023-06-30",
"2023-07-31",
"2023-08-31",
"2023-09-30",
"2023-10-31"
]
},
}
const chart = () => {
// Specify the chart’s dimensions.
const width = 928;
const height = width;
const radius = width / 6;
// Create the color scale.
const color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1));
// Compute the layout.
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);
// Create the arc generator.
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))
// Create the SVG container.
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, width])
.style("font", "10px sans-serif");
// Append the arcs.
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))
.on("mouseover", function(event, d) {
const fillColor = this.getAttribute("fill");
const fillOpacity = this.getAttribute("fill-opacity");
const sectionName = d.data.name;
// Display section details on hover
drawAnimatedBarChart(sectionName, fillColor, fillOpacity);
})
// Make them clickable if they have children.
path.filter(d => d.children)
.style("cursor", "pointer")
.on("click", clicked);
const formatValue = d3.format("$,.2f"); // Formats the value as a dollar amount
path.append("title")
.text(d => `${d.data.name}\n${formatValue(d.value)} YTD`);
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)
.style("font-size", "18px");
const parent = svg.append("circle")
.datum(root)
.attr("r", radius)
.attr("fill", "none")
.attr("pointer-events", "all")
.on("click", clicked);
// Handle zoom on click.
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);
// Transition the data on all arcs, even the ones that aren’t visible,
// so that if this transition is interrupted, entering arcs will start
// the next transition from the desired position.
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();
};
const chartContainer = document.getElementById('chart');
chartContainer.appendChild(chart());
const lineChartContainer = document.getElementById('line-chart');
function drawAnimatedBarChart(name, color, opacity) {
const data = lineChartData[name];
const lineChart = d3.create("svg")
.attr("id", "line-chart-svg")
.attr("width", 800)
.attr("height", 250);
const margin = { top: 20, right: 20, bottom: 30, left: 50 };
const chartWidth = 800 - margin.left - margin.right;
const chartHeight = 250 - margin.top - margin.bottom;
const x = d3.scaleBand().range([0, chartWidth]).padding(0.1);
const y = d3.scaleLinear().range([chartHeight, 0]);
const g = lineChart.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
// Append a foreignObject to hold a div for the filled text box
const foreignObject = lineChart.append("foreignObject")
.attr("x", margin.left + 14) // Position title in the middle
.attr("y", chartHeight - margin.bottom / 2 - 25) // Positioned at the top, adjusted for text alignment
.attr("width", 200) // Adjust the width according to your requirement
.attr("height", 50); // Adjust the height according to your requirement
// Append a div within the foreignObject to simulate the text box
const textBox = foreignObject.append("xhtml:div")
.style("width", "100%")
.style("height", "100%")
.style("background-color", "white") // Set the background color to white
.style("border", "1px solid black") // Add border if desired
.style("padding", "5px"); // Add padding for the text
// Append the text to the div within the text box
textBox.append("p")
.text("Value: \n" + name)
.style("font-size", "18px")
.style("color", "black")
.style("margin", 0); // Remove any default margin
// Other chart-related code remains the same...
x.domain(data.dates.map((date) => new Date(date).getTime()));
y.domain([0, d3.max(data.data)]);
// Continue with your remaining code for the chart
g.selectAll(".bar")
.data(data.data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", (d, i) => x(new Date(data.dates[i]).getTime()))
.attr("y", chartHeight)
.attr("width", x.bandwidth())
.attr("height", 0)
.attr("fill", color)
.style("opacity", 0);
g.selectAll(".bar")
.transition()
.duration(500)
.delay((d, i) => i * 25)
.attr("y", d => y(d))
.attr("height", d => chartHeight - y(d))
.style("opacity", opacity);
const xAxis = g.append("g")
.attr("transform", `translate(0,${chartHeight})`)
.call(
d3.axisBottom(x)
.tickValues(data.dates
.filter((date, i) => i % 3 === 0) // Filter to show every 12th tick line
.map(date => new Date(date).getTime()))
.tickFormat((d, i) => {
const date = new Date(d);
return (date.getMonth() === 0) ? d3.timeFormat("%Y")(date) : d3.timeFormat("%b")(date);
})
);
xAxis.selectAll('.tick line').remove();
xAxis.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-0.8em")
.attr("dy", "0.15em")
.attr("transform", "rotate(-65)");
lineChartContainer.innerHTML = '';
lineChartContainer.appendChild(lineChart.node());
// After the animation is complete, display text over the max value
setTimeout(() => {
const maxIndex = data.data.indexOf(d3.max(data.data));
const maxValue = data.data[maxIndex];
const formattedValue = formatValue(maxValue);
const text = g.append("text")
.attr("x", x(new Date(data.dates[maxIndex]).getTime()) + x.bandwidth() / 2)
.attr("y", y(maxValue) - 10)
.attr("text-anchor", "middle")
.attr("opacity", 0) // Initially invisible
.text("$" + formattedValue)
.attr("font-size", "14px")
.attr("fill", "black");
text.transition()
.delay(0) // Wait for the bar animation to complete
.duration(500)
.attr("opacity", 1); // Fade in the text
}, 950); // Adjust timing to ensure it appears after the bars have animated
}
function formatValue(value) {
const million = 1000000;
const thousand = 1000;
if (value >= million) {
return (value / million).toFixed(2) + "M";
} else if (value >= thousand) {
return (value / thousand).toFixed(2) + "k";
} else {
return value.toFixed(2);
}
}
</script>
|