简体   繁体   English

Dc.js:条形图,条形重叠

[英]Dc.js: Bar-chart, bars are overlapping

I'm currently working on a dc.js barchart that is working perfectly, except on one thing: 我目前正在工作正常的dc.js条形图,除了以下方面:

the X Axis represents several dates, but in a same day if an event has a bigger associated value than the previous one, it overlaps it. X轴代表多个日期,但是在同一天中,如果一个事件的关联值大于前一个,则它会与之重叠。

Here is an example: 这是一个例子: 图片

For example: on the 22th of February , it is possible to see that an orange bar is hidden by a blue one. 例如:2月22日,可能会看到橙色条被蓝色条隐藏。 So, is there a method that fixes this automatically? 那么,有没有一种方法可以自动解决此问题? I've been looking through the Dc documentation, but i haven't found anything. 我一直在浏览Dc文档,但没有发现任何东西。

EDIT: 编辑:

I'm posting the piece of code for the chart, so you can see what scales and methods i am using. 我正在为图表发布代码,因此您可以看到我正在使用的比例和方法。

chart
          .width(680)
          .height(380)
          .x(d3.scaleTime().domain([firstDate,lastDate]))
          .xUnits(function(){return Object.keys(data.data).length;})
          .brushOn(false)
          .xAxisLabel('Aulas')
          .yAxisLabel('presencas')
          .dimension(DimensionHoras)
          .barPadding(0.5)
          .outerPadding(1)
          .group(sumGroup)
          .centerBar(true)
          .legend(dc.legend().legendText(function(d, i) { return console.log(d);}))
          .on('pretransition', function(chart) {
            chart.selectAll("rect.bar").on("click", function (d) {
              console.log(d);
              var yy = data.data.filter(function(h){
                return h.data_hora_ini == d.x
              });

             jQuery.each(yy,  function(i, val){
                        graficoAssiduidade(val.data_hora_ini, val.cd_curso, val.uc_cod, "Data: " + val.dataSemFormato + ", " + val.hor_nome_turno);

              });

                chart.filter(null)
                .filter(d.data.key)
                .redrawGroup();
            });
        });

          chart.on("renderlet", function(chart){
              chart.selectAll("rect.bar").style("fill", function(d){

                var filtro = data.data.filter(function(h){return h.data_hora_ini == d.x});
               // console.log(d);
                if(filtro[0].hor_nome_turno == "PL1")   
                 return "#6BAED6";
            else if(filtro[0].hor_nome_turno == "PL2")
                 return "orange";
            else if(filtro[0].hor_nome_turno == "PL3")
                 return "yellow";
            else if(filtro[0].hor_nome_turno == "TP1")
                 return "lightgreen";

                });
          });

I don't think this has to do with the size of the values, but simply that the bars are wider than the x values they represent, which are days. 我认为这与值的大小无关,而只是条形图比它们所代表的x值(即天)宽。

Right now, with the way you specified .xUnits : 现在,以您指定.xUnits的方式:

.xUnits(function(){return Object.keys(data.data).length;})

your chart is calculating the bar width as if there were no empty space at all, and the bars are allowed to take up all of it. 您的图表正在计算钢筋宽度,就好像根本没有空白空间一样,钢筋被允许占用所有宽度。

But since you probably want the gaps where there is no data, the bars should only be a day wide. 但是,由于您可能想要没有数据的空白,因此条形图应该只有一天的时间。 To be perfectly correct, you should use 为了完全正确,您应该使用

.xUnits(d3.timeDays)

But of course, now all the bars are going to be very narrow. 但是,当然,现在所有的限制都将变得非常狭窄。 I'm not sure of a good way around that. 我不确定是否有解决这个问题的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM