繁体   English   中英

使用表单输入更新d3图表

[英]updating d3 chart with input from form

已经在这方面寻求帮助,但似乎无法找到足够接近的东西。

简而言之,我正在尝试使用d3.js更新我使用与图表在同一页面上的表单中的数据生成的图表。 即我显示一个包含3列的柱形图,并希望能够通过图表旁边的简单输入调整第3列的高度。 截至目前,我正在通过d3的csv方法加载数据,所以这可能不是最好的操作方式。

如果有人可以提供帮助,我会非常感激! 如果你愿意,我可以发布代码,但我认为一般的解释可能是可以实现的。

欢呼,艾伦

以下是我开始工作后的看法:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <style>
            body {
                font: 10px sans-serif;
            }
            .axis path,
            .axis line {
              fill: none;
              stroke: #000;
              shape-rendering: crispEdges;
            }

            .bar {
              fill: steelblue;
            }

            .x.axis path {
              display: none;
            }
        </style>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://d3js.org/d3.v3.min.js"></script>
    </head>
    <body>
        <div id="chart"></div>
        <div>
            <form>
                A: <input class="fields" id="a" type="float"/>
                B: <input class="fields" id="b" type="float"/>
                C: <input class="fields" id="c" type="float"/>
                <input type="submit" />
            </form>
        </div>
        <!--d3 code-->
        <script>
            var data = [10,50,200];
            var margin = {top: 20, right: 20, bottom: 30, left: 40},
                width = 960 - margin.left - margin.right,
                height = 500 - margin.top - margin.bottom;
            var formatPercent = d3.format(".0%");

            var x = d3.scale.ordinal()
                .rangeRoundBands([0, width], .1);

            var y = d3.scale.linear()
                .range([height, 0]);

            var xAxis = d3.svg.axis()
                .scale(x)
                .orient("bottom");

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

            var svg = d3.select(document.getElementById("chart")).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 + ")");

            svg.selectAll(".bar")
                .data(data)
              .enter().append("rect")
                .attr("x", function(d, i) { return i * 200; })
                .attr("width", function(d) {return 100;})
                .attr("y", function(d) { return height - d;})
                .attr("height", function(d) { return d; });
        </script>
        <!--on form submit-->
        <script type="text/javascript">
            $("form").submit(function() {
                var newA = document.getElementById("a").value;
                var newB = document.getElementById("b").value;
                var newC = document.getElementById("c").value;

                svg.selectAll("rect")
                    .data([newA, newB, newC])
                    .attr("y", function(d) { return height - d;})
                    .attr("height", function(d) { return d; });

                return false;
            });
        </script>
    </body>
</html>

基本模式是您从表单的onclick处理程序调用一个函数来更新数据。 所以在你的情况下有3列,前两个数据点将保持不变,第三个数据点将根据输入值更新(你可以使用d3或jquery或类似的东西从表单中获取)。

只要您使用d3的选择模式,通过d3.csv加载初始数据应该不是问题 - 有关一般概念的更多背景信息,请参阅圆圈教程 因此,在您的处理程序中,您将根据新数据调整高度,该数据应与您最初用于创建图表的代码相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM