如何同时显示总和和计数

时间:2019-04-13 10:45:25

标签: dc.js crossfilter

我使用交叉过滤器api创建了一个维度和一组。我想在DC合成图中显示每组的计数和每组的总和。但是在左右y轴上,它仅显示和。

 var data = [
             {"class" : 'class I' , "donation": 400},
             {"class" : 'Class II' , "donation" : 500}];
 var donationCrossfilter = crossfilter(data);
 var classDim = donationCrossfilter.dimension(function(d){ return d.class});
 var classGrp = classDim.group();
 var compositechart = dc.compositeChart("#compositechart");
 compositechart
   .x(d3.scaleBand())
   .xUnits(dc.units.ordinal)
   .margins({top:5 , right:60 , left : 60 , bottom:20})
   .elasticY(true)
   .group(classGrp)
   .dimension(classDim)
   .rightY(d3.scaleLinear())
   .xAxis().tickSize(0);
 compositechart.compose(
         [
          dc.lineChart(compositechart)
            .group(classGrp)
            .valueAccessor(function(d){ return d.value} ),
          dc.barChart(compositechart)
            .group(classGrp.reduceSum(function(d){ return d.donation}))
            .valueAccessor(function(d){ return d.value})
            .useRightYAxis(true)
          ]);

 compositechart.render();

在y轴的左侧应显示每个类的计数为1,1,在y轴的右侧应显示每个类捐赠的总和400,500

1 个答案:

答案 0 :(得分:1)

学习交叉过滤器时,这有点令人惊讶:似乎应该使用编程的功能样式,但是API实际上是必不可少的。

因此,当您选择Traceback (most recent call last): File "/some/path/to/file/main.py", line 36, in <module> model.fit(data, labels, epochs=10, batch_size=32) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/training.py", line 806, in fit shuffle=shuffle) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/training.py", line 2503, in _standardize_user_data self._set_inputs(cast_inputs) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper result = method(self, *args, **kwargs) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/training.py", line 2773, in _set_inputs outputs = self.call(inputs, training=training) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/sequential.py", line 256, in call outputs = layer(inputs, **kwargs) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/base_layer.py", line 594, in __call__ self._maybe_build(inputs) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/engine/base_layer.py", line 1713, in _maybe_build self.build(input_shapes) File "/anaconda3/lib/python3.7/site- packages/tensorflow/python/keras/layers/core.py", line 963, in build 'dtype %s' % (dtype,)) reduceCount时,实际上会更改现有组对象以使用该归约方法,并返回相同的对象。

在您的示例中,用sum代替count,两个子图表都显示该和。

要获得两组,一组计数和一组求和,请执行以下操作:

reduceSum

我认为选择这种风格是为了提高效率。由于每个组将继续存在并跟踪所选内容的更改,因此您不希望这些功能创建新的组。