在Chrome和Firefox中d3坐标轴标签被截断

6

我使用d3创建了一个散点图。问题在于,在Firefox和Chrome浏览器中,y轴标签无法显示出来(在IE中可以正常工作)。我尝试过许多方法,例如将svg宽度设置为100%,但由于某种原因,标签总是被截断。

<div id="TimeSeriesChartDiv" style="display: inline; float: right; width: 650px;
                            height: 415px">
                        </div>

 //Width and height
        var w = 600;
        var h = 300;
        var padding = 30;
        var margin = { top: 20, right: 20, bottom: 30, left: 20 };
        var df = d3.time.format("%b-%y");


        //Create scale functions
        var xScale = d3.time.scale()
                            .domain([d3.min(dataset, function (d) { return d[0]; }), d3.max(dataset, function (d) { return d[0]; })])
                            .range([padding, w - padding * 2])
                            .nice(5);

        var yScale = d3.scale.linear()
                             .domain([0, d3.max(dataset, function (d) { return d[1]; })])
                             .range([h - padding, padding]);
 //Define X axis
        var xAxis = d3.svg.axis()
                          .scale(xScale)
                          .orient("bottom")
                          .ticks(5)
                          .tickFormat(df);

        //Define Y axis
        var yAxis = d3.svg.axis()
                          .scale(yScale)
                          .orient("left")
                          .ticks(5);

        //Create SVG element
        var svg = d3.select("#TimeSeriesChartDiv")
                    .append("svg")
                    .attr("width", w + margin.left + margin.right)
                    .attr("height", h + margin.top + margin.bottom);

//Create X axis
        svg.append("g")
            .attr("class", "axis")
            .attr("transform", "translate(20," + (h - padding) + ")")
            .call(xAxis);

        //Create Y axis
        svg.append("g")
            .attr("class", "axis")
            .attr("transform", "translate(" + 50 + ",0)")
            .call(yAxis);

        svg.append("text")
        .attr("class", "axislabel")
         .attr("text-anchor", "end")
         .attr("x", w / 2)
         .attr("y", h + 8)
         .text("Date");

        svg.append("text")//-->>this is the text that gets cut off
        .attr("class", "axislabel")
        .attr("text-anchor", "end")
        .attr("x", -100)
        .attr("y", -15)
        //.attr("dy", ".75em")
        .attr("transform", "rotate(-90)")
        .text(unit);

任何想法都将不胜感激。谢谢。
2个回答

4

0

感谢Jan — 还有其他人的额外帮助:

http://www.d3noob.org/2012/12/adding-axis-labels-to-d3js-graph.html

這個運作了:

         svg.append("text")
        .attr("transform", "rotate(-90)")
        .attr("class", "axislabel")
        .attr("text-anchor", "middle")
        .attr("x", 0 - (h / 2))
        .attr("y",0)//any negative value here wouldnt display in ff or chrome
        .attr("dy", "1em")
        .text(unit);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接