2013-07-29 43 views
10

我不使用css,因爲我想保存和處理創建的SVG可視化文件。這意味着我需要使用內聯樣式。到目前爲止,我經歷了d3完美無缺,所以我很可能做錯了一些事情。軸上的內聯樣式stroke-width使大寫刻度標籤

我期待{'stroke-width':'3px'}來製作粗軸線。但它使粗體軸標籤。我希望文本可以用像'{font-style':'normal'}這樣的字體相關的風格進行控制。

我如何使用'stroke-width'有什麼問題?我在Chrome和Firefox中測試了這一點。

這裏是我的代碼:

<script> 
    var margin = {top: 20, right: 10, bottom: 20, left: 40}; 
    var width = 960 - margin.left - margin.right; 
    var height = 100 - margin.top - margin.bottom; 

    var x = d3.scale.linear().range([0, width]); 
    var y = d3.scale.linear().range([0, height]); 
    var xAxis = d3.svg.axis().scale(x).orient("bottom"); 
      // .tickFormat(d3.time.format("%H:%M")); 
    var yAxis = d3.svg.axis().scale(y).orient("left").ticks(height/10); 

    var svg = d3.select("svg"); 
    var vis = svg.append("g") 
     .attr("transform", "translate(" + margin.left + "," + margin.top + ")") 
     .style({'font-size': '10px', 'font-family': 'sans-serif', 
      'font-style': 'normal', 'font-variant': 'normal', 
      'font-weight': 'normal'}); 

    var redraw = function(selection, data, style) { 
     selection.selectAll(".bar") 
      .data(data) 
      .enter().append("rect") 
      .attr('class', "bar") 
      .attr("x", function(d) { return x(d[0]) - .5; }) 
      .attr("y", function(d) { return y(d[1]); }) 
      .attr("width", 5) 
      .attr("height", function(d) { return height - y(d[1]); }) 
      .style(style); 

     vis.select(".x.axis").call(xAxis); 
     vis.select(".y.axis").call(yAxis); 
    }; 

    svg.attr("width", width + margin.left + margin.right) 
     .attr("height", height + margin.top + margin.bottom); 

    vis.append("g") 
     .attr("class", "x axis") 
     .attr("transform", "translate(0," + height + ")") 
     .style({ 'stroke': 'Black', 'fill': 'none', 'stroke-width': '3px'}) 
     .call(xAxis); 

    vis.append("g") 
     .attr("class", "y axis") 
     .style({ 'stroke': 'Black', 'fill': 'none', 'stroke-width': '3px'}) 
     .call(yAxis); 

    // now we draw the first barchart (we do not know about the 2nd one yet) 
    var data1 = [[2,0.5], [4,0.8], [6,0.6], [8,0.7], [12,0.8]]; 
    x.domain([0, 13]); 
    y.domain([0.9, 0]); 

    vis.append("g") 
     .attr("class", "bar1"); 

    vis.select(".bar1") 
     .call(redraw, data1, {'fill': 'Red', 'stroke': 'Black'}); 
</script> 

回答

20

我建立在explunit的答案上,並更有選擇地應用筆畫寬度。這裏是我最後的結果:

vis.selectAll('.axis line, .axis path') 
    .style({'stroke': 'Black', 'fill': 'none', 'stroke-width': '3px'}); 
10

.call到軸功能給你一個很好的方式來創建一個全力以赴的線和文本元素,但沒有什麼可以從此回來阻止你選擇創建的部分,並給它進一步的樣式。就像這樣:

var yAxisNodes = vis.append("g") 
    .attr("class", "y axis") 
    .style({ 'stroke': 'Black', 'fill': 'none', 'stroke-width': '3px'}) 
    .call(yAxis); 
yAxisNodes.selectAll('text').style({ 'stroke-width': '1px'});