d3.js stack数据结构

为了更好的记忆stack的数据结构。
把简单的事情弄好就是不简单,不不容易的事情做好就是不容易,有一天你感觉很难,因为你没有看过别人的实现。
将形状堆叠起来,就像分段条形图那样。
* d3.stack - 创建一个新的堆叠生成器。
* stack - 为给定数据生成堆叠数据。
* stack.keys - 设置键访问器。
* stack.value - 设置值访问器。
* stack.order - 设置排序访问器。
* stack.offset - 设置偏移访问器。
* d3.stackOrderAscending - 将最小值放在底部。
* d3.stackOrderDescending - 将最大值放在底部。
* d3.stackOrderInsideOut - 将最大值放在中部。
* d3.stackOrderNone - 使用给定的系列顺序。
* d3.stackOrderReverse - 系列给定的系列相反的顺序。
* d3.stackOffsetExpand - 标准化为0=1之间。
* d3.stackOffsetNone - 应用零基准。
* d3.stackOffsetSilhouette - 将流图居中在0附近。
* d3.stackOffsetWiggle - 流图最小摆动。
复制代码
在示例一个例子
var tem = [
 {
     'state':'columnOne',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1
 },
 {
     'state':'columnTwo',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1
 },
 {
     'state':'columnThree',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1
 },
 {
     'state':'columnFor',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1
 },
 {
     'state':'columnFive',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1
 }
]
var Keys = [ "d", "e", "f"]
转化之后:var result = d3.stack().keys(Keys)(tem)
Stack 数据处理其实就是把多维的数据tem做下统计,按 Keys中的"d"拉出来一个分组,按"e"拉出来一个分组,按"f"拉出来一个分组,
每个小组里边的数组的长度和tem的长度相同
假设 a = result[0][0]; b = result[0][1];
 
(a 的[0]就是起始坐标,[1]就是终止坐标,[‘data’]就是tem[0])
(b 的[0]就是起始坐标,[1]就是终止坐标,[‘data’]就是tem[1])

复制代码
第一个需要知道的

第二个需要知道的

第三个需要知道的

第四个需要知道的

第五个展示效果

我们需要明白的就是注意的横坐标是什么,你的纬度是什么,这里的纬度是state。

完整代码
<!DOCTYPE html>
<style>

.axis .domain {
  display: none;
}

</style>
<div>结论: legend是一个重要的功能点,</div>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleBand()
    .rangeRound([0, width])
    .paddingInner(0.05)
    .align(0.1);

var y = d3.scaleLinear()
    .rangeRound([height, 0]);

var z = d3.scaleOrdinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

  var tem = [
 {
     'state':'columnOne',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1,
     total: 6
 },
 {
     'state':'columnTwo',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1,
     total: 6
 },
 {
     'state':'columnThree',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1,
     total: 6
 },
 {
     'state':'columnFor',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1,
     total: 6
 },
 {
     'state':'columnFive',
     'a': 1,
     'b': 1,
     'c': 1,
     'd': 1,
     'e': 1,
     'f': 1,
     total: 6
 }
]
  data = tem
  keys = ['d','e','f']
  data.sort(function(a, b) { return b.total - a.total; });

  x.domain(data.map(function(d) { return d.state; }));
  y.domain([0, d3.max(data, function(d) { return d.total; })]).nice();
  z.domain(keys);
  let tt = d3.stack().keys(keys)(data)
  console.log(tt)
  g.append("g")
    .selectAll("g")
    .data(tt)
    .enter().append("g")
    .attr("fill", function(d) { 
        return  z(d.key);
         })
    .selectAll("rect")
    .data(function(d) { return d; })
    .enter().append("rect")
      .attr("x", function(d) { return x(d.data.state); })
      .attr("y", function(d) {  return y(d[1]); })
      .attr("height", function(d) { return y(d[0]) - y(d[1]); })
      .attr("width", x.bandwidth());

  g.append("g")
      .attr("class", "axisX")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x))
      .append("text")
      .attr("font-size", 20)
      

  g.append("g")
      .attr("class", "axis")
      .call(d3.axisLeft(y).ticks(null, "s"))
    .append("text")
      .attr("x", 2)
      .attr("y", y(y.ticks().pop()) + 0.5)
      .attr("dy", "0.32em")
      .attr("fill", "#000")
      .attr("font-weight", "bold")
      .attr("text-anchor", "start")
      .text("Population");

  var legend = g.append("g")
      .attr("font-family", "sans-serif")
      .attr("font-size", 10)
      .attr("text-anchor", "end")
    .selectAll("g")
    .data(keys.slice().reverse())
    .enter().append("g")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
      .attr("x", width - 19)
      .attr("width", 19)
      .attr("height", 19)
      .attr("fill", z);

  legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9.5)
      .attr("font-size", 20)
      .attr("dy", "0.32em")
      .text(function(d) { return d; });

</script>

复制代码
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值