How to show duplicate values in the domain

淺唱寂寞╮ 提交于 2020-01-21 08:57:47

问题


I am trying to create a bar chart based on the values that are coming, it should show all the label as it is. The bar chart is grouped in nature.

I am able to create most of it, but somehow value labels are not coming properly.

["July 02,2017", "July 09,2017", "July 02,2017", "July 12,2017"]

Currently scale.ordinal is removing the duplicate values and making scale set like:

["July 02,2017", "July 09,2017", "July 12,2017"] 

Which is disturbing the chart view.

How can I show duplicate values in x-axis as it is?

JsFiddle


回答1:


D3 uses the mathematical concept of domain and image for the domain and range of the scales. Therefore, domain values should be unique.

A workaround in your case is creating another property in your data array, like this, which uses the index of each object to create a property conveniently named index:

data.forEach(function(d, i) {
    d.index = i
})

Then, use that property (which has unique values) for the domain of your x scale.

Also, you'll have to change the ticks for the x scale, which you can do with tickFormat:

.tickFormat(function(d, i) {
    return data[i].Groups
});

Here is the updated fiddle: http://jsfiddle.net/wujtnomh/



来源:https://stackoverflow.com/questions/45076350/how-to-show-duplicate-values-in-the-domain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!