- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定还需要实现什么才能使线条理顺。我添加了一个函数来对数据数组中的日期进行排序,并且认为可以解决这个问题。
data = data.sort(sortByDateAscending);
console.log(JSON.stringify(data))
但是图表上的数据线仍然显得有点疯狂,我是否需要将时间对象格式化为特定格式?
感谢任何帮助。这是一个jsfiddle,提前致谢。 https://jsfiddle.net/xvkmtvha
最佳答案
您应该使用排序后的数据数组创建 dataGroup。
data = data.sort(sortByDateAscending); //Sort data array first
var dataGroup = d3.nest()
.key(function(d) {
return d.Brand;
})
.entries(data); //Apply sorted data
var data = [{
"Brand": "Lexus",
"Time": "2017-05-08 16",
"Count": 8263
}, {
"Brand": "Toyota",
"Time": "2017-05-08 09",
"Count": 25382
}, {
"Brand": "Toyota",
"Time": "2017-05-08 06",
"Count": 17543
}, {
"Brand": "Toyota",
"Time": "2017-05-08 10",
"Count": 25758
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 12",
"Count": 2052
}, {
"Brand": "Acura",
"Time": "2017-05-08 10",
"Count": 9
}, {
"Brand": "Acura",
"Time": "2017-05-08 07",
"Count": 4
}, {
"Brand": "Lexus",
"Time": "2017-05-08 12",
"Count": 11653
}, {
"Brand": "Toyota",
"Time": "2017-05-08 21",
"Count": 16199
}, {
"Brand": "Honda",
"Time": "2017-05-08 23",
"Count": 179
}, {
"Brand": "Lexus",
"Time": "2017-05-08 13",
"Count": 11334
}, {
"Brand": "Honda",
"Time": "2017-05-08 17",
"Count": 600
}, {
"Brand": "Lexus",
"Time": "2017-05-08 05",
"Count": 7365
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 18",
"Count": 2147
}, {
"Brand": "Acura",
"Time": "2017-05-08 14",
"Count": 8
}, {
"Brand": "Acura",
"Time": "2017-05-08 18",
"Count": 3
}, {
"Brand": "Lexus",
"Time": "2017-05-08 20",
"Count": 7694
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 03",
"Count": 1173
}, {
"Brand": "Lexus",
"Time": "2017-05-08 17",
"Count": 8579
}, {
"Brand": "Toyota",
"Time": "2017-05-08 20",
"Count": 21008
}, {
"Brand": "Acura",
"Time": "2017-05-08 16",
"Count": 10
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 07",
"Count": 2430
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 07",
"Count": 2795
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 06",
"Count": 2614
}, {
"Brand": "Lexus",
"Time": "2017-05-08 08",
"Count": 11712
}, {
"Brand": "Honda",
"Time": "2017-05-08 05",
"Count": 188
}, {
"Brand": "Honda",
"Time": "2017-05-08 10",
"Count": 623
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 08",
"Count": 2255
}, {
"Brand": "Toyota",
"Time": "2017-05-08 07",
"Count": 21790
}, {
"Brand": "Acura",
"Time": "2017-05-08 11",
"Count": 9
}, {
"Brand": "Toyota",
"Time": "2017-05-08 22",
"Count": 12093
}, {
"Brand": "Honda",
"Time": "2017-05-08 15",
"Count": 560
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 09",
"Count": 1903
}, {
"Brand": "Lexus",
"Time": "2017-05-08 04",
"Count": 6166
}, {
"Brand": "Lexus",
"Time": "2017-05-08 02",
"Count": 5636
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 12",
"Count": 1714
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 10",
"Count": 2288
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 20",
"Count": 285
}, {
"Brand": "Acura",
"Time": "2017-05-08 13",
"Count": 7
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 14",
"Count": 1432
}, {
"Brand": "Toyota",
"Time": "2017-05-08 17",
"Count": 22696
}, {
"Brand": "Lexus",
"Time": "2017-05-08 15",
"Count": 8459
}, {
"Brand": "Toyota",
"Time": "2017-05-08 15",
"Count": 21139
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 16",
"Count": 1622
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 22",
"Count": 302
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 03",
"Count": 2195
}, {
"Brand": "Toyota",
"Time": "2017-05-08 03",
"Count": 8151
}, {
"Brand": "Acura",
"Time": "2017-05-08 12",
"Count": 6
}, {
"Brand": "Acura",
"Time": "2017-05-08 15",
"Count": 8
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 21",
"Count": 1702
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 20",
"Count": 2004
}, {
"Brand": "Toyota",
"Time": "2017-05-08 23",
"Count": 9162
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 00",
"Count": 1917
}, {
"Brand": "Toyota",
"Time": "2017-05-08 18",
"Count": 24969
}, {
"Brand": "Honda",
"Time": "2017-05-08 03",
"Count": 79
}, {
"Brand": "Lexus",
"Time": "2017-05-08 09",
"Count": 11751
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 05",
"Count": 2218
}, {
"Brand": "Lexus",
"Time": "2017-05-08 03",
"Count": 5742
}, {
"Brand": "Toyota",
"Time": "2017-05-08 11",
"Count": 26282
}, {
"Brand": "Lexus",
"Time": "2017-05-08 00",
"Count": 4583
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 23",
"Count": 1731
}, {
"Brand": "Lexus",
"Time": "2017-05-08 07",
"Count": 10854
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 13",
"Count": 1670
}, {
"Brand": "Lexus",
"Time": "2017-05-08 11",
"Count": 11539
}, {
"Brand": "Honda",
"Time": "2017-05-08 08",
"Count": 515
}, {
"Brand": "Toyota",
"Time": "2017-05-08 13",
"Count": 25338
}, {
"Brand": "Honda",
"Time": "2017-05-08 20",
"Count": 605
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 23",
"Count": 696
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 04",
"Count": 2077
}, {
"Brand": "Acura",
"Time": "2017-05-08 09",
"Count": 6
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 17",
"Count": 442
}, {
"Brand": "Honda",
"Time": "2017-05-08 02",
"Count": 44
}, {
"Brand": "Toyota",
"Time": "2017-05-08 14",
"Count": 23355
}, {
"Brand": "Lexus",
"Time": "2017-05-08 23",
"Count": 4717
}, {
"Brand": "Honda",
"Time": "2017-05-08 16",
"Count": 618
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 00",
"Count": 946
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 11",
"Count": 1636
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 19",
"Count": 2161
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 01",
"Count": 2199
}, {
"Brand": "Honda",
"Time": "2017-05-08 07",
"Count": 403
}, {
"Brand": "Honda",
"Time": "2017-05-08 18",
"Count": 771
}, {
"Brand": "Honda",
"Time": "2017-05-08 12",
"Count": 553
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 21",
"Count": 249
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 06",
"Count": 2285
}, {
"Brand": "Lexus",
"Time": "2017-05-08 06",
"Count": 9152
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 11",
"Count": 2248
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 02",
"Count": 2351
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 16",
"Count": 612
}, {
"Brand": "Honda",
"Time": "2017-05-08 09",
"Count": 515
}, {
"Brand": "Lexus",
"Time": "2017-05-08 10",
"Count": 11559
}, {
"Brand": "Toyota",
"Time": "2017-05-08 12",
"Count": 25999
}, {
"Brand": "Lexus",
"Time": "2017-05-08 19",
"Count": 8963
}, {
"Brand": "Honda",
"Time": "2017-05-08 14",
"Count": 559
}, {
"Brand": "Acura",
"Time": "2017-05-08 08",
"Count": 13
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 10",
"Count": 1660
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 09",
"Count": 2579
}, {
"Brand": "Toyota",
"Time": "2017-05-08 00",
"Count": 7609
}, {
"Brand": "Toyota",
"Time": "2017-05-08 05",
"Count": 12576
}, {
"Brand": "Acura",
"Time": "2017-05-08 17",
"Count": 14
}, {
"Brand": "Honda",
"Time": "2017-05-08 13",
"Count": 626
}, {
"Brand": "Honda",
"Time": "2017-05-08 22",
"Count": 256
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 13",
"Count": 1931
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 02",
"Count": 1351
}, {
"Brand": "Lexus",
"Time": "2017-05-08 18",
"Count": 9252
}, {
"Brand": "Lexus",
"Time": "2017-05-08 14",
"Count": 10184
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 19",
"Count": 333
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 22",
"Count": 1508
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 08",
"Count": 2884
}, {
"Brand": "Toyota",
"Time": "2017-05-08 19",
"Count": 24708
}, {
"Brand": "Honda",
"Time": "2017-05-08 04",
"Count": 91
}, {
"Brand": "Toyota",
"Time": "2017-05-08 08",
"Count": 24665
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 18",
"Count": 438
}, {
"Brand": "Toyota",
"Time": "2017-05-08 16",
"Count": 20871
}, {
"Brand": "Toyota",
"Time": "2017-05-08 04",
"Count": 9447
}, {
"Brand": "Toyota",
"Time": "2017-05-08 01",
"Count": 7178
}, {
"Brand": "Lexus",
"Time": "2017-05-08 01",
"Count": 5186
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 17",
"Count": 1793
}, {
"Brand": "Lexus",
"Time": "2017-05-08 22",
"Count": 4946
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 15",
"Count": 902
}, {
"Brand": "Lexus",
"Time": "2017-05-08 21",
"Count": 6131
}, {
"Brand": "Honda",
"Time": "2017-05-08 00",
"Count": 102
}, {
"Brand": "Honda",
"Time": "2017-05-08 19",
"Count": 705
}, {
"Brand": "Honda",
"Time": "2017-05-08 21",
"Count": 388
}, {
"Brand": "Honda",
"Time": "2017-05-08 06",
"Count": 332
}, {
"Brand": "Honda",
"Time": "2017-05-08 01",
"Count": 67
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 15",
"Count": 1597
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 04",
"Count": 1341
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 01",
"Count": 1304
}, {
"Brand": "Honda",
"Time": "2017-05-08 11",
"Count": 625
}, {
"Brand": "Chrysler",
"Time": "2017-05-08 05",
"Count": 1719
}, {
"Brand": "Toyota",
"Time": "2017-05-08 02",
"Count": 7867
}, {
"Brand": "General Motors Truck Company (GMC)",
"Time": "2017-05-08 14",
"Count": 1691
}];
data.forEach(function(d) {
d.Time = d3.timeParse("%Y-%m-%d %H")(d.Time)
//d.Time = d3.timeFormat("%m/%d/%Y %H:%M:%S %p")
var formatTime = d3.timeFormat("%m/%d/%Y %H:%M:%S %p");
formatTime(d.Time);
});
function sortByDateAscending(a, b) {
// Dates will be cast to numbers automagically:
return a.Time - b.Time;
}
data = data.sort(sortByDateAscending);
var dataGroup = d3.nest() //d3 method that groups data by Brand
.key(function(d) {
return d.Brand;
})
.entries(data);
//var color = d3.scale.category10();
var vis = d3.select("#visualisation"),
WIDTH = 1000,
HEIGHT = 500,
MARGINS = {
top: 50,
right: 20,
bottom: 50,
left: 50
},
xScale = d3.scaleTime().range([MARGINS.left, WIDTH - MARGINS.right]).domain([d3.min(data, function(d) { //set up x-axis based on data
return d.Time;
}), d3.max(data, function(d) {
return d.Time;
})]),
yScale = d3.scaleLinear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([d3.min(data, function(d) { //set up y-axis based on data
return d.Count;
}), d3.max(data, function(d) {
return d.Count;
})]),
xAxis = d3.axisBottom()
.scale(xScale),
yAxis = d3.axisLeft()
.scale(yScale)
vis.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
.call(xAxis);
vis.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(" + (MARGINS.left) + ",0)")
.call(yAxis);
var lineGen = d3.line()
.x(function(d) {
return xScale(d.Time);
})
.y(function(d) {
return yScale(d.Count);
})
.curve(d3.curveLinear);
var color = d3.scaleOrdinal(d3.schemeCategory10); // set the colour scale
dataGroup.forEach(function(d, i) { //iterate over the dataGroup and create line graph for each brand
vis.append('svg:path')
.attr('d', lineGen(d.values))
.style("stroke", function() { // Add the colours dynamically
return d.color = color(d.key);
})
.attr('stroke-width', 2)
.attr("id", 'tag' + d.key.replace(/\s+/g, '')) // assign ID
.attr('fill', 'none');
lSpace = WIDTH / dataGroup.length; //define the legend space based on number of brands
vis.append("text")
.attr("x", (lSpace / 2) + i * lSpace)
.attr("y", HEIGHT)
.style("fill", function() { // Add the colours dynamically
return d.color = color(d.key);
})
.attr("class", "legend")
.on('click', function() {
var active = d.active ? false : true,
opacity = active ? 0 : 1;
// Hide or show the elements based on the ID
d3.select("#tag" + d.key.replace(/[ )(]/g, ''))
.style("opacity", opacity);
// Update whether or not the elements are active
d.active = active;
})
.text(d.key);
// text label for the x axis
vis.append("text")
.attr("transform",
"translate(" + (WIDTH / 2) + " ," +
(HEIGHT + 30) + ")")
.style("text-anchor", "middle")
.text("Date");
// text label for the y axis
vis.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - MARGINS.left) //set y to "5"
.attr("x", 10 - (HEIGHT / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Count");
});
.axis path {
fill: none;
stroke: #777;
shape-rendering: crispEdges;
}
.axis text {
font-family: Lato;
font-size: 13px;
}
.legend {
font-size: 14px;
font-weight: bold;
cursor: pointer;
<title>D3 Test</title>
<script src="https://d3js.org/d3.v4.js"></script>
<body>
<svg id="visualisation" width="1000" height="600"></svg>
</body>
关于javascript - 图表上的 d3 数据线迷失方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43884746/
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我试图用这种形式简单地获取数字 28 integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s 但我得到了两个数字11 和
最近一直在学习D语言。我一直对运行时感到困惑。 从我能收集到的关于它的信息中,(这不是很多)我知道它是一种有助于 D 的一些特性的运行时。像垃圾收集一样,它与您自己的程序一起运行。但是既然 D 是编译
想问一下这两个正则表达式有区别吗? \d\d\d 与 \d{3} 我已经在我的本地机器上使用 Java 和 Windows 操作系统对此进行了测试,两者都工作正常并且结果相同。但是,当在 linux
我正在学习 Go,而且我坚持使用 Go 之旅(exercise-stringer.go:https://tour.golang.org/methods/7)。 这是一些代码: type IPAddr
我在Java正则表达式中发现了一段令我困惑的代码: Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); 要编译的字符串是: String string
我在 ruby 代码上偶然发现了这个。我知道\d{4})\/(\d\d)\/(\d\d)\/(.*)/是什么意思,但是\1-\2-\3-\4 是什么意思? 最佳答案 \1-\2-\3-\4 是 b
我一直在努力解决这个问题,这让我很恼火。我了解 D 运行时库。它是什么,它做什么。我也明白你可以在没有它的情况下编译 D 应用程序。就像 XoMB 所做的那样。好吧,XoMB 定义了自己的运行时,但是
我有两个列表列表,子列表代表路径。我想找到所有路径。 List> pathList1 List> pathList2 当然是天真的解决方案: List> result = new ArrayList>
我需要使用 Regex 格式化一个字符串,该字符串包含数字、字母 a-z 和 A-Z,同时还包含破折号和空格。 从用户输入我有02-219 8 53 24 输出应该是022 198 53 24 我正在
目标是达到与this C++ example相同的效果: 避免创建临时文件。我曾尝试将 C++ 示例翻译为 D,但没有成功。我也尝试过不同的方法。 import std.datetime : benc
tl;dr:你好吗perfect forwarding在 D? 该链接有一个很好的解释,但例如,假设我有这个方法: void foo(T)(in int a, out int b, ref int c
有什么方法可以在 D 中使用abstract auto 函数吗? 如果我声明一个类如下: class MyClass { abstract auto foo(); } 我收到以下错误: mai
有没有人为内存中重叠的数组切片实现交集?算法在没有重叠时返回 []。 当 pretty-print (使用重叠缩进)内存中重叠的数组切片时,我想要这个。 最佳答案 如果您确定它们是数组,那么只需取 p
我已经开始学习 D,但我在使用 Andrei Alexandrescu 所著的 The D Programming Language 一书中提供的示例时遇到了一些麻烦。由于 int 和 ulong 类
如何创建一个不可变的类? 我的目标是创建一个实例始终不可变的类。现在我只是用不可变的方法和构造函数创建了一个“可变”类。我将其称为 mData,m 表示可变。然后我创建一个别名 alias immut
不久前我买了《The D Programming Language》。好书,很有教育意义。但是,我在尝试编译书中列出的语言功能时遇到了麻烦:扩展函数。 在这本书中,Andrei 写了任何可以像这样调用
我在 D http://www.digitalmars.com/d/2.0/lazy-evaluation.html 中找到了函数参数的惰性求值示例 我想知道如何在 D 中实现可能的无限数据结构,就像
这个问题在这里已经有了答案: 12 年前关闭。 Possible Duplicate: Could anyone explain these undefined behaviors (i = i++
当前是否可以跨模块扫描/查询/迭代具有某些属性的所有函数(或类)? 例如: source/packageA/something.d: @sillyWalk(10) void doSomething()
我是一名优秀的程序员,十分优秀!