gpt4 book ai didi

jquery - D3.js 仅在一行上绘制条形图矩形

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:46 24 4
gpt4 key购买 nike

正在尝试关注 this使用 Backbone 和 D3.js 创建动态更新图表/图形的演示。只是做了一个快速复制/粘贴来尝试使用代码,但我无法让条形水平排列。条形图中的每个条都绘制在图形的第一行,创建一个看起来像这样的“图形”

enter image description here

有趣的是,文本书写正确,但它只是 <rect>似乎在同一行上绘制的标签。第一次使用此插件和非常新的 Backbone(以及与此相关的 CSS),因此非常感谢任何想法/建议。

SliderApp.js

var w = 440,
h = 200;


var DataPoint = Backbone.Model.extend({

initialize: function (x) {
this.set({
x: x
});
},

type: "point",

randomize: function () {
this.set({
x: Math.round(Math.random() * 10)
});
}

});

var DataSeries = Backbone.Collection.extend({

model: DataPoint,

fetch: function () {
this.reset();
this.add([
new DataPoint(10),
new DataPoint(12),
new DataPoint(15),
new DataPoint(18)
]);
},

randomize: function () {
this.each(function (m) {
m.randomize();
});
}

});

var BarGraph = Backbone.View.extend({

"el": "#graph",

initialize: function () {

_.bindAll(this, "render", "frame");
this.collection.bind("reset", this.frame);
this.collection.bind("change", this.render);


this.chart = d3.selectAll($(this.el)).append("svg").attr("class", "chart").attr("width", w).attr("height", h).append("g").attr("transform", "translate(10,15)");

this.collection.fetch();
},

render: function () {

var data = this.collection.models;

var x = d3.scale.linear().domain([0, d3.max(data, function (d) {
return d.get("x");
})]).range([0, w - 10]);

var y = d3.scale.ordinal().domain([0, 1, 2, 3]).rangeBands([0, h - 20]);

var self = this;
var rect = this.chart.selectAll("rect").data(data, function (d, i) {
return i;
});

rect.enter().insert("rect", "text").attr("y", function (d) {
return y(d.get("x"));
}).attr("width", function (d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());

rect.transition().duration(1000).attr("width", function (d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());

rect.exit().remove();

var text = this.chart.selectAll("text").data(data, function (d, i) {
return i;
});

text.enter().append("text")
.attr("x", function (d) {
return x(d.get("x"));
})
.attr("y", function (d, i) { return y(i) + y.rangeBand() / 2; })
.attr("dx", -3) // padding-right
.attr("dy", ".35em") // vertical-align: middle
.attr("text-anchor", "end") // text-align: right
.text(function (d) { return d.get("x"); });

text
.transition()
.duration(1100)
.attr("x", function (d) {
return x(d.get("x"));
})
.text(function (d) { return d.get("x"); });
},

frame: function () {

this.chart.append("line").attr("y1", 0).attr("y2", h - 10).style("stroke", "#000");

this.chart.append("line").attr("x1", 0).attr("x2", w).attr("y1", h - 10).attr("y2", h - 10).style("stroke", "#000");
}


});


$(function () {

var dataSeries = new DataSeries();
new BarGraph({
collection: dataSeries
}).render();

setInterval(function () {
dataSeries.randomize();
}, 2000);
});

Razor/Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/ChartCss")
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/uiSlider")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jsMVC")
@Scripts.Render("~/bundles/BackboneApps")
@Scripts.Render("~/bundles/SignalR")
@Scripts.Render("~/bundles/Charts")
<script src="/signalr/hubs"></script>
</head>
<body>
@RenderBody()
</body>
</html>

Razor/Index.cshtml

<div id="graph">
</div>

呈现的 HTML

<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width"/>
<title>
</title>
<link href="/Content/Charts.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery-ui.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery-ui.structure.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.slider.css" rel="stylesheet"/>
<script src="/Scripts/jquery.js">
</script>
<script src="/Scripts/jquery-ui.js">
</script>
<script src="/Scripts/underscore.js">
</script>
<script src="/Scripts/backbone.js">
</script>
<script src="/Scripts/SliderApp.js">
</script>
<script src="/Scripts/jquery.signalR-2.1.2.js">
</script>
<script src="/Scripts/SignalRApp.js">
</script>
<script src="/Scripts/d3.js">
</script>
<script src="/Scripts/ChartView.js">
</script>
<script src="/signalr/hubs">
</script>
</head>
<body>
<div id="graph">
<svg class="chart" width="440" height="200">
<g transform="translate(10,15)">
<line y1="0" y2="190" style="stroke: rgb(0, 0, 0);">
</line>
<line x1="0" x2="440" y1="190" y2="190" style="stroke: rgb(0, 0, 0);">
</line>
<rect width="286.66666666666663" height="45">
</rect>
<rect width="430" height="45">
</rect>
<rect width="382.22222222222223" height="45">
</rect>
<rect width="191.11111111111111" height="45">
</rect>
<text x="286.66666666666663" y="22.5" dx="-3" dy=".35em" text-anchor="end">
6
</text>
<text x="430" y="67.5" dx="-3" dy=".35em" text-anchor="end">
9
</text>
<text x="382.22222222222223" y="112.5" dx="-3" dy=".35em" text-anchor="end">
8
</text>
<text x="191.11111111111111" y="157.5" dx="-3" dy=".35em" text-anchor="end">
4
</text>
</g>
</svg>
</div>
</body>
</html>

site.css

html {
background-color: #e2e2e2;
margin: 0;
padding: 0;
}

body {
background-color: #fff;
border-top: solid 10px #000;
color: #333;
font-size: .85em;
font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
margin: 0;
padding: 0;
}

Charts.css

.chart rect {
stroke: white;
fill: steelblue;
}

最佳答案

我还没有测试过,我根本不知道 Backbone,但在我看来你使用的 y 尺度不正确。

在:

    rect.enter().insert("rect", "text").attr("y", function (d) {
return y(d.get("x"));
}).attr("width", function (d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());

代码 y(d.get("x")) 可能没有返回任何有用的信息。 y 域定义为 0..3,您将传入 d.get("x"),它将是 10, 12, 15... 当您将域外的值传递给序数标度时,您将返回 NaN。

你可能想要的是:

    rect.enter().insert("rect", "text").attr("y", function (d, i) {
return y(i);
}).attr("width", function (d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());

这将传入数据元素的索引,而不是它的值到 y 标度,这应该会导致相应的值被吐出。区别在于前两行。我已将索引变量 i 作为参数添加到匿名函数,但我返回了 y(i)

如果您查看为每个栏创建文本元素的代码,您实际上已经找到了它,您在函数中使用了 y(i)计算您的 y 属性的位置。

这是我根据您的代码开发的片段(数据是静态定义的,相应的引用方式已更改,否则就是复制和粘贴):

var data = [
{ x: 10, },
{ x: 12, },
{ x: 15, },
{ x: 18, }
];

var w = 440, h = 200;

var x = d3.scale.linear().domain([0, d3.max(data, function (d) {
return d.x;
})]).range([0, w - 10]);

var y = d3.scale.ordinal().domain([0, 1, 2, 3]).rangeBands([0, h - 20]);

var svg = d3.selectAll("div#graph").append("svg")
.attr("class", "chart")
.attr("width", w).attr("height", h)
.append("g")
.attr("transform", "translate(10,15)");

var rect = svg.selectAll("rect").data(data, function (d, i) {
return i;
});

rect.enter().insert("rect", "text").attr("y", function (d) {
return y(d.x);
}).attr("width", function (d) {
return x(d.x);
}).attr("height", y.rangeBand());

rect.transition().duration(1000).attr("width", function (d) {
return x(d.x);
}).attr("height", y.rangeBand());

rect.exit().remove();

var text = svg.selectAll("text").data(data, function (d, i) {
return i;
});

text.enter().append("text")
.attr("x", function (d) {
return x(d.x);
})
.attr("y", function (d, i) { return y(i) + y.rangeBand() / 2; })
.attr("dx", -3) // padding-right
.attr("dy", ".35em") // vertical-align: middle
.attr("text-anchor", "end") // text-align: right
.text(function (d) { return d.x; });

text
.transition()
.duration(1100)
.attr("x", function (d) {
return x(d.x);
})
.text(function (d) { return d.x; });
.chart rect {
stroke: white;
fill: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="graph"></div>

这是一个更正的片段:

var data = [
{ x: 10, },
{ x: 12, },
{ x: 15, },
{ x: 18, }
];

var w = 440, h = 200;

var x = d3.scale.linear().domain([0, d3.max(data, function (d) {
return d.x;
})]).range([0, w - 10]);

var y = d3.scale.ordinal().domain([0, 1, 2, 3]).rangeBands([0, h - 20]);

var svg = d3.selectAll("div#graph").append("svg")
.attr("class", "chart")
.attr("width", w).attr("height", h)
.append("g")
.attr("transform", "translate(10,15)");

var rect = svg.selectAll("rect").data(data, function (d, i) {
return i;
});

rect.enter().insert("rect", "text").attr("y", function (d, i) {
return y(i);
}).attr("width", function (d) {
return x(d.x);
}).attr("height", y.rangeBand());

rect.transition().duration(1000).attr("width", function (d) {
return x(d.x);
}).attr("height", y.rangeBand());

rect.exit().remove();

var text = svg.selectAll("text").data(data, function (d, i) {
return i;
});

text.enter().append("text")
.attr("x", function (d) {
return x(d.x);
})
.attr("y", function (d, i) { return y(i) + y.rangeBand() / 2; })
.attr("dx", -3) // padding-right
.attr("dy", ".35em") // vertical-align: middle
.attr("text-anchor", "end") // text-align: right
.text(function (d) { return d.x; });

text
.transition()
.duration(1100)
.attr("x", function (d) {
return x(d.x);
})
.text(function (d) { return d.x; });
.chart rect {
stroke: white;
fill: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="graph">
</div>

我已经修改了您的代码以删除 Backbone 内容,并且数据是静态定义的,以及在您的代码中引用数据时带来的更改。否则,它们之间的唯一区别就是我在上面所描述的。

请注意,您可能希望像这样设置您的 y 尺度:

var y = d3.scale.ordinal().domain(d3.range(0, data.length-1)).rangeBands([0, h - 20]);

然后它会将 y 尺度分成与数据数组中的记录一样多的波段,而不是对域进行硬编码。

关于jquery - D3.js 仅在一行上绘制条形图矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com