Carboxylic Acids & Derivatives

<!DOCTYPE html><html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carboxylic Acids & Derivatives</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.4/d3.min.js"></script>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; }
        svg { width: 100%; height: 600px; }
        .node { fill: lightblue; stroke: black; stroke-width: 1.5px; cursor: pointer; }
        .link { stroke: black; stroke-width: 1.5px; }
        .label { font-size: 14px; }
    </style>
</head>
<body>
    <h2>Carboxylic Acids & Carboxylic Acid Derivatives</h2>
    <svg id="diagram"></svg>
    <script>
        const data = {
            nodes: [
                { id: "Acid Chloride", color: "red" },
                { id: "Acid Anhydride", color: "orange" },
                { id: "Aldehyde", color: "yellow" },
                { id: "Ketone", color: "green" },
                { id: "Ester", color: "blue" },
                { id: "Carboxylic Acid", color: "purple" },
                { id: "Amide", color: "brown" }
            ],
            links: [
                { source: "Acid Chloride", target: "Acid Anhydride" },
                { source: "Acid Anhydride", target: "Aldehyde" },
                { source: "Aldehyde", target: "Ketone" },
                { source: "Ketone", target: "Ester" },
                { source: "Ester", target: "Carboxylic Acid" },
                { source: "Carboxylic Acid", target: "Amide" }
            ]
        };const width = 800, height = 500;
    const svg = d3.select("#diagram").attr("viewBox", `0 0 ${width} ${height}`);
    const simulation = d3.forceSimulation(data.nodes)
        .force("link", d3.forceLink(data.links).id(d => d.id).distance(100))
        .force("charge", d3.forceManyBody().strength(-300))
        .force("center", d3.forceCenter(width / 2, height / 2));

    const link = svg.append("g")
        .selectAll("line")
        .data(data.links)
        .enter().append("line")
        .attr("class", "link");

    const node = svg.append("g")
        .selectAll("circle")
        .data(data.nodes)
        .enter().append("circle")
        .attr("class", "node")
        .attr("r", 20)
        .attr("fill", d => d.color)
        .call(d3.drag()
            .on("start", dragStarted)
            .on("drag", dragged)
            .on("end", dragEnded));

    const label = svg.append("g")
        .selectAll("text")
        .data(data.nodes)
        .enter().append("text")
        .attr("class", "label")
        .attr("text-anchor", "middle")
        .attr("dy", 5)
        .text(d => d.id);

    simulation.on("tick", () => {
        link.attr("x1", d => d.source.x)
            .attr("y1", d => d.source.y)
            .attr("x2", d => d.target.x)
            .attr("y2", d => d.target.y);
        node.attr("cx", d => d.x).attr("cy", d => d.y);
        label.attr("x", d => d.x).attr("y", d => d.y - 25);
    });

    function dragStarted(event, d) {
        if (!event.active) simulation.alphaTarget(0.3).restart();
        d.fx = d.x;
        d.fy = d.y;
    }

    function dragged(event, d) {
        d.fx = event.x;
        d.fy = event.y;
    }

    function dragEnded(event, d) {
        if (!event.active) simulation.alphaTarget(0);
        d.fx = null;
        d.fy = null;
    }
</script>

</body>
</html>

Post a Comment

Previous Post Next Post

View All