- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
正在尝试关注 this使用 Backbone 和 D3.js 创建动态更新图表/图形的演示。只是做了一个快速复制/粘贴来尝试使用代码,但我无法让条形水平排列。条形图中的每个条都绘制在图形的第一行,创建一个看起来像这样的“图形”
有趣的是,文本书写正确,但它只是 <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/
我正在学习构建单页应用程序 (SPA) 所需的所有技术。总而言之,我想将我的应用程序实现为单独的层,其中前端仅使用 API Web 服务(json 通过 socket.io)与后端通信。前端基本上是
当我看到存储在我的数据库中的日期时。 这是 正常 。日期和时间就是这样。 但是当我运行 get 请求来获取数据时。 此格式与存储在数据库 中的格式不同。为什么会发生这种情况? 最佳答案 我认为您可以将
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在尝试使用backbone.js 实现一些代码 和 hogan.js (http://twitter.github.com/hogan.js/) Hogan.js was developed ag
我正在使用 Backbone.js、Node.js 和 Express.js 制作一个 Web 应用程序,并且想要添加用户功能(登录、注销、配置文件、显示内容与该用户相关)。我打算使用 Passpor
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我尝试在 NodeJS 中加载数据,然后将其传递给 ExpressJS 以在浏览器中呈现 d3 图表。 我知道我可以通过这种方式加载数据 - https://github.com/mbostock/q
在 node.js 中,我似乎遇到了相同的 3 个文件名来描述应用程序的主要入口点: 使用 express-generator 包时,会创建一个 app.js 文件作为生成应用的主要入口点。 通过 n
最近,我有机会观看了 john papa 关于构建单页应用程序的精彩类(class)。我会喜欢的。它涉及服务器端和客户端应用程序的方方面面。 我更喜欢客户端。在他的实现过程中,papa先生在客户端有类
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一个图形新手,需要帮助了解各种 javascript 2D 库的功能。 . . 我从 Pixi.js 中得到了什么,而我没有从 Konva 等基于 Canvas 的库中得到什么? 我从 Konva
我正在尝试将一些 LESS 代码(通过 ember-cli-less)构建到 CSS 文件中。 1) https://almsaeedstudio.com/ AdminLTE LESS 文件2) Bo
尝试查看 Express Passport 中所有登录用户的所有 session ,并希望能够查看当前登录的用户。最好和最快的方法是什么? 我在想也许我可以在登录时执行此操作并将用户模型数据库“在线”
我有一个 React 应用程序,但我需要在组件加载完成后运行一些客户端 js。一旦渲染函数完成并加载,运行与 DOM 交互的 js 的最佳方式是什么,例如 $('div').mixItUp() 。对
请告诉我如何使用bodyparser.raw()将文件上传到express.js服务器 客户端 // ... onFilePicked(file) { const url = 'upload/a
我正在尝试从 Grunt 迁移到 Gulp。这个项目在 Grunt 下运行得很好,所以我一定是在 Gulp 中做错了什么。 除脚本外,所有其他任务均有效。我现在厌倦了添加和注释部分。 我不断收到与意外
我正在尝试更改我的网站名称。找不到可以设置标题或应用程序名称的位置。 最佳答案 您可以在 config/ 目录中创建任何文件,例如 config/app.js 包含如下内容: module.expor
经过多年的服务器端 PHP/MySQL 开发,我正在尝试探索用于构建现代 Web 应用程序的新技术。 我正在尝试对所有 JavaScript 内容进行排序,如果我理解得很好,一个有效的解决方案可以是服
我是 Nodejs 的新手。我在 route 目录中有一个 app.js 和一个 index.js。我有一个 app.use(multer....)。我还定义了 app.post('filter-re
我正在使用 angular-seed用于构建我的应用程序的模板。最初,我将所有 JavaScript 代码放入一个文件 main.js。该文件包含我的模块声明、 Controller 、指令、过滤器和
我是一名优秀的程序员,十分优秀!