2015-10-16 89 views
3

我在Apex中建立了一个基本类似于http://bl.ocks.org/mbostock/1093130http://bl.ocks.org/mbostock/4062045的D3力图。不同之处在于,我从数据库的地址表中使用应用程序进程来提取数据。它工作得很好。如何将动态图例添加到Apex的D3力指向图中?

enter image description here

节点的颜色由地址类型(如联系人,缴费处,被授权人,...)来确定。现在我想用图形所用的不同颜色和连接的地址类型在页面的一侧添加图例。

是否可以在CSS内联部分的页面属性中执行此操作,还是必须在D3图形JavaScript代码中添加某些内容?

这里是我的代码:

var graph; 

function get_chart_data() { 
var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=AddressData',$v('pFlowStepId')); 
var data_all = get.get(); 
var obj = eval ("(" + data_all + ")"); 
return obj; 
} 


function showChart2() { 

graph = get_chart_data(); 

var width = 1000, 
    height = 800; 

var color = d3.scale.category20(); 

var force = d3.layout.force() 
    .gravity(0) 
    .charge(-400) 
    .linkDistance(90) 
    .size([width, height]); 


var svg = d3.select("#chart2").append("svg") 
    .attr("width", width) 
    .attr("height", height); 

var nodeById = d3.map(); 

    graph.nodes.forEach(function(node) { 
    nodeById.set(node.id, node); 
    }); 

    graph.links.forEach(function(link) { 
    link.source = nodeById.get(link.source); 
    link.target = nodeById.get(link.target); 
    }); 

    force 
     .nodes(graph.nodes) 
     .links(graph.links) 
     .start(); 

    var link = svg.selectAll(".link") 
     .data(graph.links) 
    .enter().append("line") 
     .attr("class", "link") 
     .style("stroke-width", function(d) { return Math.sqrt(d.value); }); 

    var node = svg.selectAll(".node") 
     .data(graph.nodes) 
    .enter().append("g") 
     .attr("class", "node") 
     .call(force.drag); 

    node.append("circle") 
     .attr("r", 8) 
     .style("fill", function(d) { return color(d.type); }) 

    node.append("text") 
     .attr("x", 12) 
     .attr("dy", ".35em") 
     .text(function(d) { return d.first_name; }); 

    force.on("tick", function() { 
    link.attr("x1", function(d) { return d.source.x; }) 
     .attr("y1", function(d) { return d.source.y; }) 
     .attr("x2", function(d) { return d.target.x; }) 
     .attr("y2", function(d) { return d.target.y; }); 

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); 

    }); 

}; 

我希望我解释的不够好让你明白。

+0

颜色是为每个导尿管预定义的或每次随机化的? – Typo

+0

颜色随机:var color = d3.scale.category20(); – Sara

回答

3

你猜怎么着,我刚刚解决我的问题:)

我在页面的JavaScript部分中加入了代码在function showChart2()结束属性,但仍处于它。

var legend = svg.selectAll(".legend") 
    .data(color.domain()) 
    .enter().append("g") 
    .attr("class", "legend") 
    .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); 

legend.append("rect") 
    .attr("x", width - 18) 
    .attr("width", 18) 
    .attr("height", 18) 
    .style("fill", color); 

legend.append("text") 
    .attr("x", width - 24) 
    .attr("y", 9) 
    .attr("dy", ".35em") 
    .style("text-anchor", "end") 
    .text(function(d) { return d; }); 

这里是完整的工作代码:

var graph; 

function get_chart_data() { 
var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=AddressData',$v('pFlowStepId')); 
var data_all = get.get(); 
var obj = eval ("(" + data_all + ")"); 
return obj; 
} 


function showChart2() { 

graph = get_chart_data(); 

var width = 1000, 
    height = 800; 

var color = d3.scale.category20(); 

var force = d3.layout.force() 
    .gravity(0) 
    .charge(-400) 
    .linkDistance(90) 
    .size([width, height]); 


var svg = d3.select("#chart2").append("svg") 
    .attr("width", width) 
    .attr("height", height); 

var nodeById = d3.map(); 

    graph.nodes.forEach(function(node) { 
    nodeById.set(node.id, node); 
    }); 

    graph.links.forEach(function(link) { 
    link.source = nodeById.get(link.source); 
    link.target = nodeById.get(link.target); 
    }); 

    force 
     .nodes(graph.nodes) 
     .links(graph.links) 
     .start(); 

    var link = svg.selectAll(".link") 
     .data(graph.links) 
    .enter().append("line") 
     .attr("class", "link") 
     .style("stroke-width", function(d) { return Math.sqrt(d.value); }); 

    var node = svg.selectAll(".node") 
     .data(graph.nodes) 
    .enter().append("g") 
     .attr("class", "node") 
     .call(force.drag); 

    node.append("circle") 
     .attr("r", 8) 
     .style("fill", function(d) { return color(d.type); }) 

    node.append("text") 
     .attr("x", 12) 
     .attr("dy", ".35em") 
     .text(function(d) { return d.first_name; }); 

    force.on("tick", function() { 
    link.attr("x1", function(d) { return d.source.x; }) 
     .attr("y1", function(d) { return d.source.y; }) 
     .attr("x2", function(d) { return d.target.x; }) 
     .attr("y2", function(d) { return d.target.y; }); 

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); 

    }); 
var legend = svg.selectAll(".legend") 
    .data(color.domain()) 
    .enter().append("g") 
    .attr("class", "legend") 
    .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); 

legend.append("rect") 
    .attr("x", width - 18) 
    .attr("width", 18) 
    .attr("height", 18) 
    .style("fill", color); 

legend.append("text") 
    .attr("x", width - 24) 
    .attr("y", 9) 
    .attr("dy", ".35em") 
    .style("text-anchor", "end") 
    .text(function(d) { return d; }); 

}; 

我从来没有想过我能回答我的问题,但它的工作原理;)

我希望它可以帮助别人太..

+0

谢谢莎拉,这确实派上用场 –

+0

非常有用,谢谢! – Rodolphe