简体   繁体   English

如何使用 dc.js 移动 x 轴位置?

[英]How to move the x-axis position using dc.js?

I have this:我有这个:

我所拥有的截图

But I want the x axis to run along y=0 , eg但我希望 x 轴沿y=0运行,例如

在此处输入图像描述

And ideally I'd either have the tick labels on top ie in the blue bit, or where they were at the bottom of the chart.理想情况下,我要么在顶部有刻度标签,即在蓝色位中,要么在图表底部的位置。

EDIT: how the chart is created.编辑:如何创建图表。

I'm using something like:我正在使用类似的东西:

var                                                                                     
  ndx = crossfilter(data),                                                              
  dataDimension = ndx.dimension(d => d.period),                                         
  ordinals = data.map(d => d.period),                                                   
  lossGroup = dataDimension.group().reduceSum(d => d.loss),                             
  offsets = lossGroup.all().map(d => -d.value),                                         
  chart;                                                                                
                                                                                        
// The data is like {                                                                   
//   period: Date (start of period, e.g. month),                                        
//   start: Integer (number at start of period/end of last period)                      
//   loss: Integer (number lost during period)                                          
//   gain: Integer (number gained during period)                                        
// }                                                                                    
                                                                                        
chart = dc.barChart(chartElement)                                                       
  .dimension(dataDimension)                                                             
  // The first group is the loss                                                        
  .group(lossGroup)                                                                     
  .stack(dataDimension.group().reduceSum(d => d.start), 'carryforward')                 
  .stack(dataDimension.group().reduceSum(d => d.gain), 'gain')                          
  .stackLayout(d3.layout.stack().offset(layers => offsets))                             
  .x(d3.scale.ordinal(ordinals).domain(ordinals))                                       
  .xUnits(dc.units.ordinal)                                                             
  .elasticY(true)                                                                       
  .renderLabel(false)                                                                   
  // The first group is the loss                                                        
  .title(item => 'Loss: ' + item.value)                                                 
  .title('carryforward', item => 'Sustained: ' + item.value)                            
  .title('gain', item => 'Gain: ' + item.value)                                         
  .renderTitle(true);                                                                   
  dc.renderAll();                     

Hmm, I guess you are using .stackLayout() to get the negative stacking.嗯,我猜你正在使用.stackLayout()来获得负堆叠。 Since dc.js doesn't “look inside” this setting, I don't think there is anything built-in to offset the axis.由于 dc.js 没有“查看”这个设置,我不认为有任何内置的东西可以偏移轴。 You would probably need to use a pretransition handler to move it.您可能需要使用pretransition转换处理程序来移动它。

As for moving the tick labels, you could use .title() instead, like in this example .至于移动刻度标签,您可以使用.title()代替,就像在这个例子中一样。 And then set the tick label to empty.然后将刻度标签设置为空。

Best I can think of, not really an answer but more than a comment.我能想到的最好的,不是真正的答案,而不仅仅是评论。 :-) :-)

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

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