将JSON对象/字符串传递给D3而不是JSON文件

时间:2015-09-28 10:25:27

标签: javascript json d3.js

我正在尝试使用D3创建图表,以便我们可以分析基于城市的数据。由于数据来自后端,我不想使用JSON文件,而是创建一个JSON对象并将其传递给D3以呈现图形。我正在附上我正在处理的代码。任何有关这方面的帮助都会非常有用。

<!DOCTYPE html>
<html>
<%@ page language="java" import="java.util.*, java.lang.*"%>
<meta charset="utf-8">
<style>

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

.axis {
  font: 10px sans-serif;
}

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

.x.axis path {
  display: none;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var stringTry = '${city}';
var city = [];
var trial;
while(!(stringTry.indexOf('\,')== -1)){
    trial = stringTry.substring(0,stringTry.indexOf('\,'));
    if(trial.indexOf('\[')==0){
        trial = trial.substring(1,trial.length);
    }else{
        trial = trial;
    }
    city.push(trial);
    stringTry = stringTry.substring((stringTry.indexOf('\,')+1),stringTry.length);
}
trial = stringTry.substring(0,stringTry.indexOf('\]'));
city.push(trial);

var stringTry2 = '${frequency}';
var frequency = [];
var trial2;
while(!(stringTry2.indexOf('\,')== -1)){
    trial2 = stringTry2.substring(0,stringTry2.indexOf('\,'));
    if(trial2.indexOf('\[')==0){
        trial2 = trial2.substring(1,trial.length);
    }else{
        trial2 = trial2;
    }
    frequency.push(trial2);
    stringTry2 = stringTry2.substring((stringTry2.indexOf('\,')+1),stringTry2.length);
}
trial2 = stringTry2.substring(0,stringTry2.indexOf('\]'));
frequency.push(trial2); 

var test = []
test.push("\{\"city\"\:"+city[0]);
for (var i=1;i<(city.length-1);i++){
    test.push(city[i]);
}
test.push(city[city.length-1]+"\}");
var test2 = [];
test2.push("\{\"frequency\"\:"+frequency[0]);
for (var j=1;j<(frequency.length-1);j++){
    test2.push(frequency[j]);
}

test2.push(frequency[frequency.length-1]+"\}");

var dataArray = [];
 data.push(test);
 data.push(test2);

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

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

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

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

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

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.json('${file}', type, function(error, data) {
 // if (error) throw error;

  x.domain(dataArray.map(function(d) { return d.city; }));
  y.domain([0, d3.max(dataArray, function(d) { return d.frequency; })]);

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

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Frequency");

  svg.selectAll(".bar")
      .data(dataArray)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.city); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency); })
      .attr("height", function(d) { return height - y(d.frequency); });
//});

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

</script>**strong text**

0 个答案:

没有答案