【问题标题】:inline style stroke-width for axis makes bold tick labels轴的内联样式笔触宽度使粗体刻度标签
【发布时间】:2013-07-29 20:27:22
【问题描述】:

我不使用 css,因为我想保存和处理创建的 SVG 可视化文件。这意味着我需要使用内联样式。到目前为止,我认为 d3 完美无瑕,所以很可能我做错了什么。

我期待 {'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>

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    我以 explunit 的答案为基础,更有选择性地应用了笔画宽度。这是我最终得到的结果:

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

    【讨论】:

      【解决方案2】:

      轴函数的.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'});
      

      【讨论】:

        猜你喜欢
        • 2015-06-28
        • 2020-09-24
        • 2017-04-17
        • 1970-01-01
        • 2017-03-16
        • 2020-02-18
        • 2021-12-23
        • 1970-01-01
        • 2012-03-14
        相关资源
        最近更新 更多