Визуализация структуры чат-бота с помощью рычага - PullRequest
0 голосов
/ 21 октября 2018

Я создал чат-бота с помощью Snatchbot, но хочу создать свой собственный пользовательский интерфейс, чтобы вести беседы на моем веб-сайте.Также я хочу визуализировать разговор в виде дерева решений, которое можно отобразить на веб-странице.Как визуализировать разговоры в древовидной структуре решений?

Это тип структуры, которую я хочу создать: https://codepen.io/NitByte/pen/KGQZZo

var margin = {
    top: 20,
    right: 120,
    bottom: 20,
    left: 120
},
width = 960 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;

var root ={"name":"Start","text":"Without using any instruments can you tell the difference between DNA,RNA and protein present in 3 tubes ?\n\nWhat to look for?\n[[Physcical Properties]]\n[[Chemical Properties]]","children":[{"name":"Physcical Properties","text":"Which physical property is suitable?\n1.[[Color]] \n2.[[Foam]]\n3.[[Viscosity]]  \n4.[[PH]]","children":[{"name":"Color","text":"Which color is the liquid in the tube?\n[[White]],[[Green]],[[Red]]","children":[{"name":"White","text":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour.","children":[{"name":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour."}]},{"name":"Green","text":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour.","children":[{"name":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour."}]},{"name":"Red","text":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour.","children":[{"name":"The Solution is colourless.Protein,RNA,DNA are not coloured and hence cannot estimate by colour."}]}]},{"name":"Foam","text":"Is foam present?\n[[Yes]]\n[[No]]","children":[{"name":"Yes","text":"The tube contains Protein.2 tubes left.","children":[{"name":"The tube contains Protein.2 tubes left."}]},{"name":"No","text":"Is there swial?\n[[Yes->Positive]]\n[[No->Nope]]","children":[{"name":"Positive","text":"The tube contains RNA.","children":[{"name":"The tube contains RNA."}]},{"name":"Nope","text":"The tube contains DNA.","children":[{"name":"The tube contains DNA."}]}]}]},{"name":"Viscosity","text":"DNA is viscous at PH 7.0.Need PH meter.Cannot determine using viscosity.","children":[{"name":"DNA is viscous at PH 7.0.Need PH meter.Cannot determine using viscosity."}]},{"name":"PH","text":"At PH 7.0 DNA and RNA partition into af phase.\n[[PH < 7.0]]\n[[PH > 7.0]]","children":[{"name":"PH < 7.0","text":"It contains DNA in Org phase.But PH meter is required for the estimation of PH.","children":[{"name":"It contains DNA in Org phase.But PH meter is required for the estimation of PH."}]},{"name":"PH > 7.0","text":"It contains RNA in af phase.But PH meter is required for the estimation of PH.","children":[{"name":"It contains RNA in af phase.But PH meter is required for the estimation of PH."}]}]}]},{"name":"Chemical Properties","text":"Which process can be used?\nHydrolysis by [[acids]],[[alkalie]] or [[enzymes]].","children":[{"name":"acids","text":"To perform this reaction one needs instruments.Physical properties is a better option.","children":[{"name":"To perform this reaction one needs instruments.Physical properties is a better option."}]},{"name":"alkalie","text":"To perform this reaction one needs instruments.Physical properties is a better option.","children":[{"name":"To perform this reaction one needs instruments.Physical properties is a better option."}]},{"name":"enzymes","text":"To perform this reaction one needs instruments.Physical properties is a better option.","children":[{"name":"To perform this reaction one needs instruments.Physical properties is a better option."}]}]}]}

var i = 0,
    duration = 750,
    rectW = 500,
    rectH = 60;

var tree = d3.layout.tree().nodeSize([500, 500]);
var diagonal = d3.svg.diagonal()
    .projection(function (d) {
    return [d.x + rectW / 2, d.y + rectH / 2];
});

var svg = d3.select("#body").append("svg").attr("width", 1000).attr("height", 1000)
    .call(zm = d3.behavior.zoom().scaleExtent([1,3]).on("zoom", redraw)).append("g")
    .attr("transform", "translate(" + 350 + "," + 20 + ")");

//necessary so that zoom knows where to zoom and unzoom from
zm.translate([350, 20]);

root.x0 = 0;
root.y0 = height / 2;

function collapse(d) {
    if (d.children) {
        d._children = d.children;
        d._children.forEach(collapse);
        d.children = null;
    }
}

root.children.forEach(collapse);
update(root);

d3.select("#body").style("height", "800px");

function update(source) {

    // Compute the new tree layout.
    var nodes = tree.nodes(root).reverse(),
        links = tree.links(nodes);

    // Normalize for fixed-depth.
    nodes.forEach(function (d) {
        d.y = d.depth * 180;
    });

    // Update the nodes…
    var node = svg.selectAll("g.node")
        .data(nodes, function (d) {
        return d.id || (d.id = ++i);
    });

    // Enter any new nodes at the parent's previous position.
    var nodeEnter = node.enter().append("g")
        .attr("class", "node")
        .attr("transform", function (d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
    })
        .on("click", click);

    nodeEnter.append("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)
        .style("fill", function (d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

    nodeEnter.append("text")
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("dy", ".35em")
        .attr("text-anchor", "middle")
        .text(function (d) {
        return d.name;
    });

    // Transition nodes to their new position.
    var nodeUpdate = node.transition()
        .duration(duration)
        .attr("transform", function (d) {
        return "translate(" + d.x + "," + d.y + ")";
    });

    nodeUpdate.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
        .attr("stroke", "black")
        .attr("stroke-width", 1)
        .style("fill", function (d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

    nodeUpdate.select("text")
        .style("fill-opacity", 1);

    // Transition exiting nodes to the parent's new position.
    var nodeExit = node.exit().transition()
        .duration(duration)
        .attr("transform", function (d) {
        return "translate(" + source.x + "," + source.y + ")";
    })
        .remove();

    nodeExit.select("rect")
        .attr("width", rectW)
        .attr("height", rectH)
    //.attr("width", bbox.getBBox().width)""
    //.attr("height", bbox.getBBox().height)
    .attr("stroke", "black")
        .attr("stroke-width", 1);

    nodeExit.select("text");

    // Update the links…
    var link = svg.selectAll("path.link")
        .data(links, function (d) {
        return d.target.id;
    });

    // Enter any new links at the parent's previous position.
    link.enter().insert("path", "g")
        .attr("class", "link")
        .attr("x", rectW / 2)
        .attr("y", rectH / 2)
        .attr("d", function (d) {
        var o = {
            x: source.x0,
            y: source.y0
        };
        return diagonal({
            source: o,
            target: o
        });
    });

    // Transition links to their new position.
    link.transition()
        .duration(duration)
        .attr("d", diagonal);

    // Transition exiting nodes to the parent's new position.
    link.exit().transition()
        .duration(duration)
        .attr("d", function (d) {
        var o = {
            x: source.x,
            y: source.y
        };
        return diagonal({
            source: o,
            target: o
        });
    })
        .remove();

    // Stash the old positions for transition.
    nodes.forEach(function (d) {
        d.x0 = d.x;
        d.y0 = d.y;
    });
}

// Toggle children on click.
function click(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
}

//Redraw for zoom
function redraw() {
  //console.log("here", d3.event.translate, d3.event.scale);
  svg.attr("transform",
      "translate(" + d3.event.translate + ")"
      + " scale(" + d3.event.scale + ")");
}

Я хочу создать аналогичную структуру, используя диалоги чата.Это возможно?

1 Ответ

0 голосов
/ 29 октября 2018

Вы можете использовать balkangraph для визуализации разговоров на вашем веб-сайте.

enter image description here

Вот пример:

window.onload = function () {
    OrgChart.templates.toni = Object.assign({}, OrgChart.templates.belinda);
    OrgChart.templates.toni.size = [30, 30];
    OrgChart.templates.toni.node = '<circle cx="15" cy="15" r="15" fill="#039BE5" stroke-width="1" stroke="#aeaeae"></circle>';
    OrgChart.templates.toni.minus = "";
    OrgChart.templates.toni.plus = "";
    OrgChart.templates.toni.rippleRadius = 15;
    OrgChart.templates.toni.link_field_0 = '<g transform="rotate(90)"><text text-anchor="middle" fill="#aeaeae" width="290" x="0" y="0" style="font-size:18px;">{val}</text></g>';

    OrgChart.templates.toni.field_0 = '<text class="field_0" style="font-size: 18px;" text-anchor="middle" fill="#039BE5"  x="15" y="48">{val}</text>';
    var chart = new OrgChart(document.getElementById("tree"), {
        scaleInitial: BALKANGraph.match.boundary,
        enableDragDrop: false,
        enableSearch: false,
        mouseScroolBehaviour: BALKANGraph.action.zoom,
        template: "toni",
        orientation: BALKANGraph.orientation.left,
        nodeMouseClickBehaviour: BALKANGraph.action.expandCollapse,
        padding: 60,
        levelSeparation: 200,
        siblingSeparation: 100,
        nodeBinding: {
            field_0: "text"
        },
        linkBinding: {
            link_field_0: "text"
        },
        links: [
            { from: 2, to: 1, text: "no", tags: ["link-no"] },
            { from: 3, to: 1, text: "yes", tags: ["link-yes"] },
            { from: 4, to: 2, text: "no", tags: ["link-no"] },
            { from: 5, to: 2, text: "yes", tags: ["link-yes"] },
            { from: 6, to: 4, text: "no", tags: ["link-no"] },
            { from: 7, to: 4, text: "yes", tags: ["link-yes"] }
        ],
        nodes: [
            { id: 1, text: "Is it raining?" },
            { id: 2, text: "Is it windy?" },
            { id: 3, text: "Don't bring anything", tags: ["node-end"] },
            { id: 4, text: "Is it extremely windy?" },
            { id: 5, text: "Use an ubrella", tags: ["node-end"] },
            { id: 6, text: "Stay home", tags: ["node-end"] },
            { id: 7, text: "Wear a rain jacket", tags: ["node-end"] }
        ]
    });
};
html, body {
    margin: 0px;
    padding: 0px;
    width: 100%;
    height: 100%;
    font-family: Helvetica;
    overflow: hidden;
}

#tree {
    width: 100%;
    height: 100%;
}

/*partial*/
.link-yes path {
    stroke: #FFCA28;
}

.link-yes text {
    fill: #FFCA28;
}

.link-no path {
    stroke: #F57C00;
}

.link-no text {
    fill: #F57C00;
}

[level='0'] path {
    stroke-width: 9;
}

[level='1'] path {
    stroke-width: 3;
}

[level='2'] path {
    stroke-width: 1;
}

.node-end circle {
    fill: #f1f1f1;
}

.node-end text {
    fill: #aeaeae;
}
#tree {
    background-color: #f1f1f1;
    border-radius: 40px;
}
<script src="https://balkangraph.com/js/latest/OrgChart.js"></script>

<div id="tree"></div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...