中心D3图表+添加响应性

时间:2016-02-20 18:53:43

标签: javascript css d3.js

有一点背景,我对JS和开发一般都是新手,所以我可能会遗漏一些明显的东西。我使用d3让这张图表工作得很好,但无论我做什么,我都无法获得正确的定位。我试过用CSS操作它,它似乎没有以合乎逻辑的方式表现。我将显示设置为阻止和边距为自动,它根本不影响它。我可以改变定位的唯一方法是调整d3代码中的边距,但这对响应性没有太大作用。我也尝试过使用text-align,但也没用。我正在尝试做的是使它居中并随着屏幕尺寸的增加而扩大规模。这在理论上应该很容易在CSS中完成,但它似乎根本不起作用。谢谢你的帮助。

这是JS代码:

            // Set the dimensions of the canvas / graph
            var margin = {top: 20, right: 0, bottom: 70, left: 70},
                width = 300 - margin.left - margin.right,
                height = 300 - margin.top - margin.bottom;

            // Parse the date / time
            var parseDate = d3.time.format("%-m/%-d/%Y").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(10);

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

            // chart area fill

            var area = d3.svg.area()
                .x(function(d) { return x(d.Date); })
                .y0(height)
                .y1(function(d) { return y(d.Forecast); });

            // Define the line
            var valueline = d3.svg.line()
                .interpolate("cardinal")
                .x(function(d) { return x(d.Date); })
                .y(function(d) { return y(d.Orders); });

            var valueline2 = d3.svg.line()
                .interpolate("cardinal")
                .x(function(d) { return x(d.Date); })
                .y(function(d) { return y(d.Forecast); });

            // 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.csv("csv/Forecast.csv", function(error, data) {
                data.forEach(function(d) {
                    d.Date = parseDate(d.Date);
                    d.Orders = +d.Orders;
                });
             // Scale the range of the data
                x.domain(d3.extent(data, function(d) { return d.Date; }));
                y.domain([0, d3.max(data, function(d) { return d.Orders; })]);


                // Area

                svg.append("path")
                    .datum(data)
                    .attr("class", "area")
                    .attr("d", area);

                // Add the valueline path.

                svg.append("path")
                    .attr("class", "line")
                    .attr("d", valueline2(data))
                    .style("stroke", "#A7A9A6");

                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)
                    .selectAll("text")
                        .style("text-anchor", "end")
                        .attr("dx", "-.8em")
                        .attr("dy", ".15em")
                        .attr("transform", "rotate(-65)");

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

            });

1 个答案:

答案 0 :(得分:0)

JSFIDDLES

响应&居中: https://jsfiddle.net/sladav/6fyjhmne/3/

没有响应: https://jsfiddle.net/sladav/7tp5vdkr/

为了响应,请利用SVG viewBox:

一些链接:

设置viewBox:

var margin = {top: 100, right: 150, bottom: 100, left: 150}

var outerWidth  = 1600,
    outerHeight = 900;

var width  = outerWidth - margin.right - margin.left,
    height = outerHeight - margin.top - margin.bottom;


d3.select(".plot-div").append("svg")
    .attr("class", "plot-svg")
    .attr("width", "100%")
    .attr("viewBox", "0 0 " + outerWidth + " " + outerHeight)
    .append("g")
      .attr("class", "plot-space")
      .attr("transform", 
        "translate(" + margin.left + "," + margin.top + ")"
      );

以上注意事项:

  1. SVG被放入div中 - 我将调整div的大小和位置而不是svg
  2. SVG宽度设置为parent / div的%,而不是绝对值。
  3. 您在SVG中绘制的所有内容现在都与outerWidth x outerHeight相关,无单位参数由viewBox重新调整。
  4. 看看我的JSFIDDLE示例中的 rect ...

    d3.select(".plot-svg").append("rect")
        .attr("x", 0)
        .attr("y", 3*outerHeight/4)
        .attr("width", 800)
        .attr("height", outerHeight/4)
        .attr("fill", "grey")
    

    调整窗口大小,矩形将始终填充svg的1/2,因为800/1600(注意:1600是外部宽度)。

    用于调整位置/居中:

    操纵包含SVG /图表的div,按照您想要的方式定位。在我的例子中,它占据了页面的50%并且由于margin:auto。而居中。

    .plot-div{
      width: 50%;
      display: block;
      margin: auto;
     }
    

    无论您使用何种方法来缩放/定位div,您的图表都应该遵循。