在数据地图中创建垂直图例

时间:2016-11-27 20:45:24

标签: javascript d3.js legend datamaps

我正在使用DataMaps来制作一个等值线,但在修改图例方面遇到了问题。我想将图例垂直放在地图的右侧。文档不清楚如何执行此操作。如何创建堆叠/垂直图例?

我使用以下代码创建图例:

var legend_params = {
  legendTitle: "Some Test Data",
};
map.legend(legend_params);

这是试图修改它的CSS:

.datamaps {
  position: relative;
  display: inline-block;
  text-align: center;
}

.datamaps-legend {
    color: white;
    right: -100px;
    top: 0;
    position: relative;
    display: inline-block;
    text-align: center;
 }

.datamaps-legend dl {
  text-align: center;
  display: inline-block;
  position: static;
}

enter image description here Here's my jsfiddle example.

1 个答案:

答案 0 :(得分:3)

我认为数据地图中没有水平图例的选项。我将它用于一个项目,最后重写了图例功能,使其符合要求。我能够使用CSS将图例堆叠/垂直和地图右侧。颜色和值没有排列。如果值是静态的,则可以通过定位特定的dt / dd(:nth-​​child或:nth-​​of-type)元素并根据需要添加填充来对其进行排列。如果值是动态的,则需要更聪明的解决方案。我为.datamaps-legend dt添加了css,并在.datamaps-legend中修改了top。 fiddle

中的示例
.datamaps-legend dt{
  clear:both;
}
.datamaps-legend {
    color: white;
    right: -100px;
    top: 128px;
    position: relative;
    display: inline-block;
    text-align: center;
 }