2017-03-01 57 views
0

我想將來自服務器的數據作爲json插入到d3中以生成圖表。使用請求正文填充從spring mvc到d3圖表的JSON數據

在服務器端,我使用spring生成JSON對象如下,只需在hashmap中填充數據並通過spring responce主體發送響應。

@RequestMapping("/greeting1") 
    @ResponseBody 
    public LinkedHashMap<String, String> greeting1(@RequestParam(value = "name", required = false) String name, 
      Model model) { 
     LinkedHashMap<String, String> values = data.populateData(); 
     model.addAttribute("name", values); 
     return values; 
    } 

在客戶端,我將數據填充到一個div並試圖注入d3,如下所示(但不工作)。

殼體1:

var test_data=d3.select("body").selectAll("test_data"); 
    data=test_data.html; 
    x.domain(data.map(function(d) { return d.x; })); 
    y.domain(data.map(function(d) { return d.y; })); 

上面的代碼被要求JSON如下面的格式。

案例2:

var data = [{x:0,y:0.5},{x:0.1,y:0.8},{x:0.2,y:1.1},{x:1.3,y:1.5},{x:0.4,y:2.5},{x:0.5,y:3.4},{x:0.6,y:4.3}]; 
     x.domain(data.map(function(d) { return d.x; })); 
     y.domain(data.map(function(d) { return d.y; })); 

我怎麼產生的JSON在從服務器端指定的格式(如情況2)?

我應該如何在d3中捕獲div test_data?

爲d3完整的js代碼如下。

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
<title>Getting Started: Serving Web Content</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

<meta charset="utf-8"></meta> 
<style> 
.axis text { 
    font: 10px sans-serif; 
} 

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

.line { 
    fill: none; 
    stroke-width: 1.5px; 
} 

.dot { 
    fill: #fff; 
    stroke: #000; 
} 
</style> 

<script src="./d3.js"></script> 
<script src="./jquery.js"></script> 
<script src="./data.tsv"></script> 

<script> 
$(document).ready(function(){ 
     sendAjax(); 
    }); 

    function sendAjax() { 

    $.ajax({ 
     url: "/greeting1", 
     type: 'GET', 
     /* dataType: 'json', 
     data: "{\"name\":\"hmkcode\",\"id\":2}", 
     contentType: 'application/json', 
     mimeType: 'application/json',*/ 
     success: function(data) { 
      //alert(data); 
      $('#test_data').html(data); 
      //alert($('#test_data').html); 
      callChart(); 
     }, 
     error:function(data,status,er) { 
      alert("error: "+data+" status: "+status+" er:"+er); 
     } 
    }); 
    } 
</script> 

</head> 
<body> 
    <p th:text="'Hello, ' + ${name} + '!'" /> 
    <div id="test"></div> 
    <div id="test_data"></div> 
    <script> 

function callChart() 
{ 

    var margin = {top: 40, right: 40, bottom: 40, left: 40}, 
    width = 960 - margin.left - margin.right, 
    height = 500 - margin.top - margin.bottom; 


var x = d3.scale.linear() 
    .domain([0, 1]) 
    .range([0, width]); 

var y = d3.scale.linear() 
    .domain([0, 1]) 
    .range([height, 0]); 

var z = d3.scale.linear() 
    .domain([2/3, 1]) // D3 3.x tension is buggy! 
    .range(["brown", "steelblue"]); 

var xAxis = d3.svg.axis() 
    .scale(x) 
    .orient("bottom"); 

var yAxis = d3.svg.axis() 
    .scale(y) 
    .orient("left"); 

var line = d3.svg.line() 
    .interpolate("cardinal") 
    .x(function(d) { return x(d.x); }) 
    .y(function(d) { return y(d.y); }); 

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

//d3.tsv("data.tsv", type, function(error, data) { 
// if (error) throw error; 

    //var test_data=d3.select("body").selectAll("test_data"); 
    //data=test_data.html; 
    var data = [{x:0,y:0.5},{x:0.1,y:0.8},{x:0.2,y:1.1},{x:1.3,y:1.5},{x:0.4,y:2.5},{x:0.5,y:3.4},{x:0.6,y:4.3}]; 
    x.domain(data.map(function(d) { return d.x; })); 
    y.domain(data.map(function(d) { return d.y; })); 
    svg.append("g") 
     .attr("class", "axis axis--x") 
     .attr("transform", "translate(0," + height + ")") 
     .call(xAxis); 

    svg.append("g") 
     .attr("class", "axis axis--y") 
     .call(yAxis); 

    svg.selectAll(".line") 
     .data(z.ticks(6)) 
    .enter().append("path") 
     .attr("class", "line") 
     .attr("d", function(d) { return line.tension(d)(data); }) 
     .style("stroke", z); 

    svg.selectAll(".dot") 
     .data(data) 
    .enter().append("circle") 
     .attr("class", "dot") 
     .attr("cx", function(d) { return x(d.x); }) 
     .attr("cy", function(d) { return y(d.y); }) 
     .attr("r", 3.5); 
//}); 

function type(d) { 
    d.x = +d.x; 
    d.y = +d.y; 
    return d; 
} 


} 

</script> 
</body> 
</html> 
+1

如果按ID選擇,它必須是'selectAll(「#test_data」);'。考慮改變'select',因爲ID是唯一的。除此之外,作爲getter的'html'有括號:'html()'。 –

+0

好的,謝謝我會糾正它,你的意思是var test_data = d3.selectAll(「test_data」);對。 – Curious

+0

是的。它應該是'var test_data = d3.select(「#test_data」);'和'data = test_data.html();'。 –

回答

0

我的問題是通過使用GSON爲構建JSON和通過@Gerardo朵通過D3獲取股利提供的解決方案來解決。

@RequestMapping("/greeting") 
    public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name, 
      Model model) { 
     LinkedList<Data.Temp> values = data.populateData(); 
     Gson gson = new Gson(); 
     String output=gson.toJson(values); 
     model.addAttribute("name",output); 
     return "greeting"; 
    } 

function callChart() 
{ 

    var margin = {top: 40, right: 40, bottom: 40, left: 40}, 
    width = 960 - margin.left - margin.right, 
    height = 500 - margin.top - margin.bottom; 


var x = d3.scale.linear() 
    .domain([0, 1]) 
    .range([0, width]); 

var y = d3.scale.linear() 
    .domain([0, 1]) 
    .range([height, 0]); 

var z = d3.scale.linear() 
    .domain([2/3, 1]) // D3 3.x tension is buggy! 
    .range(["brown", "steelblue"]); 

var xAxis = d3.svg.axis() 
    .scale(x) 
    .orient("bottom"); 

var yAxis = d3.svg.axis() 
    .scale(y) 
    .orient("left"); 

var line = d3.svg.line() 
    .interpolate("cardinal") 
    .x(function(d) { return x(d.x); }) 
    .y(function(d) { return y(d.y); }); 

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

//d3.tsv("data.tsv", type, function(error, data) { 
// if (error) throw error; 

    //var test_data=d3.select("body").selectAll("test_data"); 
    //data=test_data.html; 
    var test_data = d3.select("#test_data"); 
    var data = JSON.parse(test_data.text()); 
    x.domain(data.map(function(d) { return d.x; })); 
    y.domain(data.map(function(d) { return d.y; })); 
    svg.append("g") 
     .attr("class", "axis axis--x") 
     .attr("transform", "translate(0," + height + ")") 
     .call(xAxis); 

    svg.append("g") 
     .attr("class", "axis axis--y") 
     .call(yAxis); 

    svg.selectAll(".line") 
     .data(z.ticks(6)) 
    .enter().append("path") 
     .attr("class", "line") 
     .attr("d", function(d) { return line.tension(d)(data); }) 
     .style("stroke", z); 

    svg.selectAll(".dot") 
     .data(data) 
    .enter().append("circle") 
     .attr("class", "dot") 
     .attr("cx", function(d) { return x(d.x); }) 
     .attr("cy", function(d) { return y(d.y); }) 
     .attr("r", 3.5); 
//}); 

function type(d) { 
    d.x = +d.x; 
    d.y = +d.y; 
    return d; 
} 


}