d3轴标签在chrome和firefox中切断

时间:2014-06-10 17:04:39

标签: svg d3.js

我在d3中创建了一个散点图。问题是y轴标签没有出现在firefox和chrome中(在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 个答案:

答案 0 :(得分:4)

您正在为文本使用负坐标,这意味着它们会在SVG之外绘制。其他浏览器似乎IE9 doesn't seem to clip thing to the SVG area。最好的解决方案是在图表中添加足够的填充,以便可以在SVG中绘制文本。禁用剪辑does not seem to be supported in all browsers

答案 1 :(得分: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);