dc.js - 使用 dc.js 和 crossfilter 的面积图
问题描述
我使用 dc.js 和 crossfilter 创建了一个折线图。该图表目前如下所示:
必需:我希望左上角的活动非活动图例位于图表下方(底部)的中心位置,x 轴刻度标签应该从一月到十二月开始。我在下面添加我的代码。谢谢你。
const dateNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var data = [
{date: "2011-11-14T16:17:54Z", quantity: 2, active: 1000, inactive: 100, type: "tab", city: " "},
{date: "2011-11-14T16:20:19Z", quantity: 2, active: 190, inactive: 100, type: "tab", city: "Berlin"},
{date: "2011-11-14T16:28:54Z", quantity: 1, active: 300, inactive: 200, type: "visa", city: " "},
{date: "2011-11-14T16:30:43Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: "Amsterdam"},
{date: "2011-11-14T16:48:46Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: " "},
{date: "2011-11-14T16:53:41Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: " "},
{date: "2011-11-14T16:54:06Z", quantity: 1, active: 100, inactive: 0, type: "cash", city: " "},
{date: "2011-11-14T16:58:03Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: " "},
{date: "2011-11-14T17:07:21Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: " "},
{date: "2011-11-14T17:22:59Z", quantity: 2, active: 90, inactive: 0, type: "tab", city: " "},
{date: "2011-11-14T17:25:45Z", quantity: 2, active: 200, inactive: 0, type: "cash", city: " "},
{date: "2011-11-14T17:29:52Z", quantity: 1, active: 200, inactive: 100, type: "visa", city: ""}
];
data.forEach(function(d){
var tempDate = new Date(d.date);
d.date = tempDate;
})
var facts = crossfilter(data);
var all = facts.groupAll();
//table
var dateDimension = facts.dimension(function(d){ return d.date; });
//line chart
var dateGroup = dateDimension.group().reduceSum(function(d){ return d.active; });
var dateGroupTip = dateDimension.group().reduceSum(function(d){ return d.inactive; });
var minDate = dateDimension.bottom(1)[0].date;
var maxDate = dateDimension.top(1)[0].date;
var lineChart = dc.lineChart("#test")
.width(700)
.height(200)
.brushOn(false)
.margins({top:10,bottom:30,right:10,left:70})
.dimension(dateDimension)
.group(dateGroupTip,"inactive")
.stack(dateGroup,"active")
.renderHorizontalGridLines(true)
.renderArea(true)
.renderDataPoints(true)
// // .interpolate('basis')
// lineChart.xAxis().ticks(d3.timeMonth, 1)
// lineChart.xAxis().tickFormat(d3.timeFormat('%b'))
.clipPadding(data.length)
.legend(dc.legend().y(10).x(0).itemHeight(12).gap(5))
// .xAxis()
// .ticks(d3.time.month, 7)
// .tickFormat(d3.time.format('%e'));
// .xAxis().tickFormat(d3.time. format('%B'))
// .xUnits(d3.time.months)
.x(d3.scaleLinear().domain([minDate,maxDate]))
lineChart.yAxis().ticks(6);
lineChart.xAxis().ticks(12);
dc.renderAll();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="dc.css"/>
<script src="d3.js"></script>
<script src="crossfilter.js"></script>
<script src="dc.js"></script>
</head>
<body>
<div id="test"></div>
<!-- <script src="app.js"></script> -->
<script src="play.js"></script>
</body>
</html>
解决方案
看起来您已经了解了大部分内容,并且对 D3 第 3 版到第 4 版的 API 更改感到困惑?
除了这些更改(例如d3.time.months
更改为d3.timeMonths
. 你不是唯一一个——这些变化给我们所有人带来了很多困惑。
最后的步骤是
- 使用时间尺度:
.x(d3.scaleTime().domain([minDate,maxDate]))
d3.timeMonth
在维度定义和minDate
/maxDate
计算中,使用 将日期向下舍入到月初,例如var dateDimension = facts.dimension(function(d){ return d3.timeMonth(d.date); });
- 告诉图表如何计算要显示的点数:
.xUnits(d3.timeMonths)
- 格式化 x 轴刻度:
lineChart.xAxis().tickFormat(d3.timeFormat('%B'))
您拥有其中的大部分,只是注释掉了。可能是因为您找到了使用 D3v3 API 的示例,并且它们导致了错误?
至于图例,dc.js 在这里没有做任何复杂的事情 - 您只需手动布置它,在图表上设置边距以留出足够的空间,并在图例上设置x
和y
以将图例放在您想要的位置.
我找到
lineChart
.margins({top:10,bottom:60,right:25,left:40});
.legend(dc.legend().y(165).x(325).itemHeight(12).gap(5))
效果很好,但是您必须对其进行调整以适应口味(不幸的是,当您的数据更改导致事物大小发生变化时)。
这是一个工作小提琴。. 我冒昧地更改了您的示例数据,以便它包含所需的月份。
如果您的数据跨越多年,您将在此设计中遇到麻烦,但我想您可以跨过那座桥。
推荐阅读
- node.js - Mongoose 填充虚拟嵌套数组
- google-data-studio - 如何在谷歌数据工作室中显示最后可用的数据
- html - 无法在 Atom(mac)上使用相对路径(但可以使用直接路径)在 html 中加载图像
- mongodb - 使用 arrayFilters 更新查询
- php - 无法在 Kannel 的 SMS 正文中发送 Hash Tag 符号 (#)
- mysql - 用于在 Google Workspace 中创建数据库的应用脚本代码
- python - 回复特定用户的 Twitter 机器人
- python - 使用 pymox 模拟导入的方法
- java - Java 8 - 下载需要通过 HTTPS 进行身份验证的文件时遇到问题。在卷曲中工作正常
- pytest - 为什么被测函数在初始化类而不是在 pytest 中使用模拟?