还是 D3.js 的新手……这是我的第二张图表。
我正在尝试构建一个相当简单的条形图/柱形图,x 轴上有日期,y 轴上有一个简单的计数。我目前正在尝试使轴正确,并遇到一些问题:
- 我如何每天/日期获得一个刻度(和一个条形图)(图表将涵盖 30 天的时间段)
- 如何让 y 轴刻度为整数(目前给我 ~ 0、0.5、1、1.5 等)
- 如何让刻度标签响应文本锚...开始或结束?
我创建了一个 jsbin 来说明:http: //jsbin.com/axafid/4/
到目前为止,这是我的数据和代码(部分基于本教程:http ://bl.ocks.org/phoebebright/3061203 )
var testData = [
{
date: "2013-06-25",
programs: 1
},
{
date: "2013-06-26",
programs: 2
},
{
date: "2013-06-27",
programs: 3
},
{
date: "2013-06-28",
programs: 0
},
{
date: "2013-06-29",
programs: 0
},
{
date: "2013-06-30",
programs: 0
},
{
date: "2013-07-01",
programs: 1
},
{
date: "2013-07-02",
programs: 3,
premiums: 9000
},
{
date: "2013-07-03",
programs: 1
},
{
date: "2013-07-04",
programs: 4
},
{
date: "2013-07-05",
programs: 0
},
{
date: "2013-07-06",
programs: 0
},
{
date: "2013-07-07",
programs: 0
},
{
date: "2013-07-08",
programs: 1
},
{
date: "2013-07-09",
programs: 2
},
{
date: "2013-07-10",
programs: 2
},
{
date: "2013-07-11",
programs: 4
},
{
date: "2013-07-12",
programs: 4
},
{
date: "2013-07-13",
programs: 0
},
{
date: "2013-07-14",
programs: 0
},
{
date: "2013-07-15",
programs: 2
},
{
date: "2013-07-16",
programs: 2
},
{
date: "2013-07-17",
programs: 1
},
{
date: "2013-07-18",
programs: 5
},
{
date: "2013-07-19",
programs: 1
},
{
date: "2013-07-20",
programs: 0
},
{
date: "2013-07-21",
programs: 0
},
{
date: "2013-07-22",
programs: 3
},
{
date: "2013-07-23",
programs: 3
},
{
date: "2013-07-24",
programs: 3
}
]
和我的 javascript:
function renderGraph(){
var margin = {top: 20, right: 16, bottom: 32, left: 64},// space around the chart, not including labels
width = (mainWidth() - margin.left - margin.right), // width of svg
height = 400, // height of svg
parseDate = d3.time.format("%Y-%m-%d").parse;
var data = testData;
var x_domain = d3.extent(data, function(d) { return parseDate(d.date); }),
y_domain = [0, d3.max(data, function(d) { return d.programs; })]
// display date format
var date_format = d3.time.format("%d %b");
// create an svg container
var vis = d3.select("#programs-renewing-graph")
.append("svg:svg")
.attr("width", width)
.attr("height", height);
// define the y scale (vertical)
var yScale = d3.scale.linear()
.domain(y_domain).nice() // make axis end in round number
.range([height - margin.top, margin.bottom]);// map these to the chart height, less padding.
var xScale = d3.time.scale()
.domain(x_domain)
.range([32, width - margin.right]);
// define the y axis
var yAxis = d3.svg.axis()
.orient("left")
.scale(yScale);
// define the x axis
var xAxis = d3.svg.axis()
.orient("bottom")
.scale(xScale)
.tickFormat(date_format);
// draw y axis with labels and move in from the sides by the amount of padding
vis.append("g")
.attr("class", "axis")
.attr("transform", "translate("+margin.left+",0)")
.call(yAxis);
// draw x axis with labels and move to the bottom of the chart area
vis.append("g")
.attr("class", "xaxis axis") // two classes, one for css formatting, one for selection below
.attr("transform", "translate(" +margin.left+ "," + (height - margin.top) + ")")
.call(xAxis);
}
谢谢!