2015-11-19 49 views
0

我遵循以下D3示例:http://bl.ocks.org/mbostock/1667367。在這個例子中,有一個焦點和一個上下文。當上下文更改時,焦點將顯示上下文中的數據。我的目標是增加另一個焦點(focus2)。爲了更容易,focus2(此刻)的功能與焦點(原始焦點)相同。預期的結果是:當畫筆移動時,兩個焦點(代碼中的focus和focus2)將會改變。下面是代碼:D3,當情景發生變化時,第二焦點不移動

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<!--head> \t 
 
\t <link rel="stylesheet" type= "text/css" href="angle_view.css"> 
 
</head--> 
 

 
<style> 
 

 
svg { 
 
    font: 10px sans-serif; 
 
} 
 

 
.area { 
 
    fill: steelblue; 
 
    clip-path: url(#clip); 
 
} 
 

 
.axis path, 
 
.axis line { 
 
    fill: none; 
 
    stroke: #000; 
 
    shape-rendering: crispEdges; 
 
} 
 

 
.brush .extent { 
 
    stroke: #fff; 
 
    fill-opacity: .125; 
 
    shape-rendering: crispEdges; 
 
} 
 
</style> 
 

 
<body> 
 
\t <!--script type = "text/javascript" src = "../d3/d3.min.js"> </script--> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.7/d3.min.js"></script> 
 
\t 
 
\t <p>Select a dataset from the list.</p> 
 
\t <select id = "datasetSelect" onchange = "datasetSelectFunction()"> 
 
\t <option value="load_small_data_set">small dataset</option> 
 
\t <option value="load_whole_data_set">whole dataset</option> \t 
 
\t </select> 
 
\t 
 
\t <!--script type = "text/javascript" src = "angle_view.js"> </script --> 
 
\t 
 
\t <script> \t 
 
\t 
 
\t // main script 
 
\t var g_fn = null; 
 
\t var g_pan = null; 
 
\t var g_feature = null; 
 
\t var g_fn_pan = null; 
 
\t 
 
// \t init_angle_feature_view(); 
 
\t var margin = {top: 10, right: 30, bottom: 370, left: 40}; 
 
\t var margin2 = {top: 250, right: 30, bottom: 130, left: 40}; 
 
\t var margin3 = {top: 500, right: 30, bottom: 20, left: 40}; 
 
\t var width = 960 - margin.left - margin.right; 
 
\t var height = 600 - margin.top - margin.bottom; 
 
\t var height2 = 600 - margin2.top - margin2.bottom; 
 
\t var height3 = 600 - margin3.top - margin3.bottom; 
 

 

 
\t // set scales 
 
\t var x = d3.scale.linear().range([0, width]); 
 
\t var x2 = d3.scale.linear().range([0, width]); 
 
\t var x3 = d3.scale.linear().range([0, width]); 
 
\t var y = d3.scale.linear().range([height, 0]); 
 
\t var y2 = d3.scale.linear().range([height2, 0]); 
 
\t var y3 = d3.scale.linear().range([height3, 0]); 
 

 
\t var xAxis = d3.svg.axis().scale(x).orient("bottom"); 
 
\t var xAxis2 = d3.svg.axis().scale(x2).orient("bottom"); 
 
\t var xAxis3 = d3.svg.axis().scale(x3).orient("bottom"); 
 
\t var yAxis = d3.svg.axis().scale(y).orient("left"); 
 

 
\t var brush = d3.svg.brush() 
 
    \t .x(x3)  // x scale 
 
    \t .on("brush", brushed); 
 

 
\t var area = d3.svg.area() // area on the top, detailed view of pan angle 
 
    \t .interpolate("monotone") 
 
    \t .x(function(d) { return x(d.frame_number); }) 
 
    \t .y0(height) 
 
    \t .y1(function(d) { return y(d.pan); }); 
 
\t 
 
\t var area2 = d3.svg.area() // area in the middle, detailed view of feature 
 
\t \t .interpolate("monotone") 
 
\t \t .x(function(d){return x2(d.frame_number);}) 
 
\t \t .y0(height2) 
 
\t \t .y1(function(d){return y2(d.pan); }); 
 

 
\t var area3 = d3.svg.area() // area on the bottom, global view of pan angle 
 
    \t .interpolate("monotone") 
 
    \t .x(function(d) { return x3(d.frame_number); }) 
 
    \t .y0(height3) 
 
    \t .y1(function(d) { return y3(d.pan); }); 
 

 
\t var svg = d3.select("body").append("svg") 
 
    \t .attr("width", width + margin.left + margin.right) 
 
    \t .attr("height", height + margin.top + margin.bottom); 
 

 
\t svg.append("defs").append("clipPath") // what is defs?, defs is not directly rendered. what is clipPath? draw only inside width x height 
 
    \t .attr("id", "clip") // what is clip and id do 
 
    \t .append("rect") 
 
    \t .attr("width", width) 
 
    \t .attr("height", height); 
 

 
\t var focus = svg.append("g") 
 
    \t .attr("class", "focus") // where does focus come from? 
 
    \t .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 
 
\t 
 
\t var focus2 = svg.append("g") 
 
\t \t .attr("class", "focus") 
 
\t \t .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")"); 
 

 
\t var context = svg.append("g") 
 
    \t .attr("class", "context") 
 
    \t .attr("transform", "translate(" + margin3.left + "," + margin3.top + ")"); \t 
 
\t 
 
\t function datasetSelectFunction(){ 
 
\t \t var x = document.getElementById("datasetSelect").value; \t 
 
\t \t //console.log(x); 
 
\t \t 
 
\t \t if (x == "load_small_data_set") 
 
\t \t { 
 
\t \t \t // load frame number, pan data \t \t \t 
 
\t \t \t d3.csv("https://dl.dropboxusercontent.com/u/54750216/javascript/sampled_fn_pan_small.csv", function(error, data) { 
 
\t \t \t \t if (error) throw error; 
 
\t \t \t \t // sparse data \t \t \t 
 
\t \t \t \t data.forEach(function(d){ 
 
\t \t \t \t \t d.frame_number = parseInt(d.frame_number); 
 
\t \t \t \t \t d.pan = parseFloat(d.pan); \t \t \t \t \t 
 
\t \t \t \t }); 
 
\t \t \t \t g_fn_pan = data; 
 
\t \t \t 
 
    \t \t \t d3.csv("https://dl.dropboxusercontent.com/u/54750216/javascript/sampled_fn_dim_feature_small.csv", function(error, feature) { 
 
    \t \t \t if (error) throw error; 
 
    \t \t \t // Coerce the CSV data to the appropriate types.  
 
    \t \t \t feature.forEach(function(d) { 
 
    \t \t \t  d.fn  = parseInt(d.frame_number); 
 
    \t \t \t  d.dim  = parseInt(d.dim); 
 
    \t \t \t  d.feature = parseFloat(d.feature); 
 
    \t \t \t }); 
 
    \t \t \t g_feature = feature; 
 
\t \t \t 
 
\t \t \t angle_feature_view(g_fn_pan, g_feature); \t \t \t 
 
    \t \t \t }); \t \t 
 
\t \t \t 
 
\t \t \t }); 
 
\t \t \t console.log("load_small_data_set"); \t \t \t \t 
 
\t \t \t // load frame number, feature data \t \t  \t \t \t 
 
\t \t  \t \t 
 
\t \t } 
 
\t \t else if (x == "load_whole_data_set") 
 
\t \t { 
 
\t \t \t // load frame number, pan data \t \t \t 
 
\t \t \t d3.csv("sampled_data/sampled_fn_pan.csv", function(error, data) { 
 
\t \t \t \t if (error) throw error; 
 
\t \t \t \t // sparse data \t \t \t 
 
\t \t \t \t data.forEach(function(d){ 
 
\t \t \t \t \t d.frame_number = parseInt(d.frame_number); 
 
\t \t \t \t \t d.pan = parseFloat(d.pan); \t \t \t \t \t 
 
\t \t \t \t }); 
 
\t \t \t \t g_fn_pan = data; 
 
\t \t \t 
 
    \t \t \t d3.csv("sampled_data/sampled_fn_dim_feature.csv", function(error, feature) { 
 
    \t \t \t if (error) throw error; 
 
    \t \t \t // Coerce the CSV data to the appropriate types.  
 
    \t \t \t feature.forEach(function(d) { 
 
    \t \t \t  d.fn  = parseInt(d.frame_number); 
 
    \t \t \t  d.dim  = parseInt(d.dim); 
 
    \t \t \t  d.feature = parseFloat(d.feature); 
 
    \t \t \t }); 
 
    \t \t \t g_feature = feature; 
 
\t \t \t 
 
\t \t \t // angle_feature_view(g_fn_pan, g_feature); \t \t \t 
 
    \t \t \t }); \t \t 
 
\t \t \t 
 
\t \t \t }); 
 
\t \t \t console.log("load_whole_data_set"); 
 
\t \t } \t \t 
 
\t } 
 
\t 
 
\t // visualize frame number, pan angle and feature 
 
\t function angle_feature_view(fn_pan, feature) 
 
\t { 
 
\t \t var fn_min = d3.min(fn_pan, function(d){return d.frame_number}); 
 
\t  var fn_max = d3.max(fn_pan, function(d){return d.frame_number}); 
 
\t \t var pan_min = d3.min(fn_pan, function(d){return d.pan}); 
 
\t \t var pan_max = d3.max(fn_pan, function(d){return d.pan}); 
 
    
 
\t \t x.domain([fn_min, fn_max]); 
 
\t \t y.domain([pan_min, pan_max]); \t  
 
    \t \t 
 
\t \t x2.domain(x.domain()); 
 
\t \t y2.domain(y.domain()); 
 
\t \t // bottom 
 
\t \t x3.domain(x.domain()); 
 
\t \t y3.domain(y.domain()); 
 

 
\t \t // draw fn pan detail view 
 
\t \t 
 
\t \t focus.append("path") 
 
\t  \t .datum(fn_pan) 
 
\t  \t .attr("class", "area") 
 
\t  \t .attr("d", area); 
 

 
\t \t focus.append("g") 
 
\t  .attr("class", "x axis") 
 
\t  .attr("transform", "translate(0," + height + ")") 
 
\t  .call(xAxis); 
 

 
\t \t focus.append("g") 
 
\t  .attr("class", "y axis") 
 
\t  .call(yAxis); \t \t 
 
\t \t 
 
\t \t // add focus2 in this place 
 
    \t \t focus2.append("path") 
 
     \t .datum(fn_pan) 
 
     \t .attr("class", "area") 
 
     \t .attr("d", area2); 
 

 
    \t  focus2.append("g") 
 
     .attr("class", "x axis") 
 
     .attr("transform", "translate(0," + height2 + ")") 
 
     .call(xAxis2); 
 

 
    \t \t focus2.append("g") 
 
     \t \t .attr("class", "y axis") 
 
     \t \t .call(yAxis); 
 

 
\t \t // brush? 
 
\t \t context.append("path") 
 
\t  \t .datum(fn_pan) 
 
\t  \t .attr("class", "area") 
 
\t  \t .attr("d", area3); 
 

 
\t \t context.append("g") 
 
\t  .attr("class", "x axis") 
 
\t  .attr("transform", "translate(0," + height3 + ")") 
 
\t  .call(xAxis3); 
 

 
\t \t context.append("g") 
 
\t  .attr("class", "x brush") 
 
\t  .call(brush) 
 
\t  .selectAll("rect") 
 
\t  .attr("y", -6) 
 
\t  .attr("height", height3 + 7); \t \t 
 
\t } 
 
\t 
 
\t function brushed() { 
 
\t x.domain(brush.empty() ? x3.domain() : brush.extent()); 
 
\t focus.select(".area").attr("d", area); 
 
\t focus.select(".x.axis").call(xAxis); 
 
\t focus2.select(".area").attr("d", area2); // I add focus2 in this place 
 
\t focus2.select(".x.axis").call(xAxis2); 
 
\t console.log("call brushed"); 
 
\t } \t 
 
\t 
 
\t 
 
\t </script> 
 
\t 
 
\t 
 
</body> \t

敬請關注焦點,FOCUS2和環境相關的領域。 問題是,當上下文更改時,focus2視圖不會更改。它總是顯示整個數據集。

回答

0

你幾乎已經擁有它了,你只會忘記更新第二個尺度的域。

x2.domain(brush.empty() ? x3.domain() : brush.extent());添加到brushed()函數解決它。

+0

請添加一些解釋。您的答案可能會被標記爲「低質量」,最終可能會被刪除。 –

+0

感謝您的建議。我添加了一條線來解釋解決方案,但它非常簡單,沒有太多要添加真的... –

+0

@JohannesJander,希望你不介意我問。我相信我正確回答了這個問題(雖然相當簡單)。接受答案需要什麼?我試圖在這裏獲得一些聲譽點。這完全取決於問這個問題的人嗎?如果他不再有興趣呢?對不起,因爲不合時宜 –

相關問題