如何在D3

时间:2016-05-13 09:15:35

标签: javascript json date d3.js

我试图在X轴上创建一个带日期的图表。我在字符串中转换日期,输出是从后端以json格式。因此,我在d3图表中将日期排序为字符串而不是日期。请在下面找到输入文件的样本

[{"GetCreatedAt1": "May 12,11 AM", "id__count": 1},
{"GetCreatedAt1": "May 12,04 AM", "id__count": 101}, 
{"GetCreatedAt1": "May 12,03 PM", "id__count": 1}, 
{"GetCreatedAt1": "May 13,08 AM", "id__count": 97}]

我正在创建id__count(Y轴)与GetCreatedAt1(X轴)。在我的d3脚本中,我无法将GetCreatedAt1转换为日期,因此它不像日期那样排序,而且我的ticks(10)函数也无效。由于在大量日期的情况下,x轴变得杂乱。请在下面找到我的代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">



<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js">     </script>


</head>

<style>
.timeline {
float : left ; 
height : 400px; 
width : 600px; 
font: 20px sans-serif;
    text-align: left;
margin-left  : 20px; 
border : 1px solid black;
}


.header1{
float : left ; 
height : 60px; 
width : 550px; 
font: 20px sans-serif;
    text-align: left;
margin-left  : 50px; 


}

.header1 p{
float : left ; 


}

</style>


<body>


<div class="header1">
<p>Timeline</p>
</div>

<div style="clear:both"></div>

<div id="area1" class="timeline"> </div>

<div style="clear:both"></div>


<script > 


// Timeline

var margin = {top: 20, right: 20, bottom: 100, left: 50},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;


var parseDate= d3.time.format('%b %d,%I %p').parse ;

d3.json( "/grp_by_time_post.json", function (error , data) { 
    data.forEach(function (d) { 
        d.GetCreatedAt1=(parseDate(d.GetCreatedAt1)) ; 
        d.id__count=d.id__count ; } ); 

var x= d3.scale.ordinal().domain(d3.range(data.length)).rangePoints([0, width], 0.05);  

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




// Define the axis 


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

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

// Define the area
var area = d3.svg.area()
        .x(function(d) { return x((d.GetCreatedAt1)); })
        .y0(height)
            .y1(function(d) { return y(d.id__count); })
// Define the line
var valueline = d3.svg.line()
        .x(function(d) { return x((d.GetCreatedAt1)); })
            .y(function(d) { return y(d.id__count); })

// Adds the svg canvas

// Adds the svg canvas
var chart1 = d3.select("#area1")
.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 + ")");

// Scale the range of the data
x.domain(data.map(function(d) { return (d.GetCreatedAt1); }));
y.domain([0, d3.max(data, function(d) { return d.id__count; })]);

// Add the valueline path.
chart1.append("path")
    .attr("class", "area")
    .attr("d", area(data));


// Add the X Axis
chart1.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .selectAll("text")  
            .style("text-anchor", "end")
            .attr("dx", "-.8em")
            .attr("dy", ".15em")
            .attr("transform", "rotate(-65)" )
    ;
;

// Add the Y Axis
chart1.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("# of tweets");
}); 


</script>   



</body>

另请注意我对D3很新。

先谢谢

0 个答案:

没有答案