2015-10-16 82 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