在D3.js中设置轴数据的样式

在D3.js中设置轴数据的样式,d3.js,D3.js,我有一段代码,它从MySQL中获取数据并绘制图形,并且它做得很正确。但是,文本中出现的标记段(如时间跨度)似乎是黑色的。我试过做很多改变,但都是徒劳的。我想把它变成白色 另外,我希望x轴从1月开始,而不是像图中所示的2015年 <style> body { font: 12px Arial;} path { stroke: steelblue; stroke-width: 2; fill: none; } .axis path, .axis line { fill: none; s

我有一段代码,它从MySQL中获取数据并绘制图形,并且它做得很正确。但是,文本中出现的标记段(如时间跨度)似乎是黑色的。我试过做很多改变,但都是徒劳的。我想把它变成白色

另外,我希望x轴从1月开始,而不是像图中所示的2015年

<style> 
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
} 
</style>

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 900 - margin.left - margin.right,
    height = 540 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%Y-%m-%d").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

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

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

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.lsdate); })
    .y(function(d) { return y(d.units); });

// Adds the svg canvas
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 + ")");

// Get the data
d3.json("common/scripts/charts.php", function(error, data) {
    data.forEach(function(d) {
        d.lsdate = parseDate(d.lsdate);
        d.units = +d.units;
    });

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

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

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

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);

});

正文{font:12px Arial;}
路径{
笔画:钢蓝;
笔画宽度:2;
填充:无;
}
.轴线路径,
.轴线{
填充:无;
笔画:灰色;
笔画宽度:1;
形状渲染:边缘清晰;
} 
//设置画布/图形的尺寸
var margin={顶部:30,右侧:20,底部:30,左侧:50},
宽度=900-边距。左侧-边距。右侧,
高度=540-margin.top-margin.bottom;
//解析日期/时间
var parseDate=d3.time.format(“%Y-%m-%d”).parse;
//设定范围
var x=d3.time.scale().range([0,width]);
变量y=d3.scale.linear().range([height,0]);
//定义轴
var xAxis=d3.svg.axis().scale(x)
.方向(“底部”)。刻度(5);
var yAxis=d3.svg.axis().scale(y)
.方向(“左”)。勾号(5);
//界定界线
var valueline=d3.svg.line()
.x(函数(d){返回x(d.lsdate);})
.y(函数(d){返回y(d.units);});
//添加svg画布
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”);
//获取数据
json(“common/scripts/charts.php”,函数(错误,数据){
data.forEach(函数(d){
d、 lsdate=parseDate(d.lsdate);
d、 单位=+d.单位;
});
//缩放数据的范围
x、 域(d3.extent(数据,函数(d){返回d.lsdate;}));
y、 域([0,d3.max(数据,函数(d){返回d.units;})];
//添加valueline路径。
追加(“路径”)
.attr(“类”、“行”)
.attr(“d”,valueline(数据));
//添加X轴
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
//添加Y轴
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis);
});
截图:

根据@Lars的评论,您需要使用
fill
属性将样式应用于
文本
元素:

.axis text {
    fill: white;
}
在这里演奏小提琴:

旁白:为了防止x轴以年份开始,但为了始终使用月份名称,请在定义
xAxis
时添加此行:

.tickFormat(d3.time.format("%B"));

这里有一个小提琴的更新也显示了这一变化:

您需要设置
文本
元素的样式。您能告诉我如何设置吗?我引用了很多示例,但还没有弄清楚。将样式应用于
文本
元素。另见