Chart .dimension(...)。group(...)。columns不是函数(d3.js v4.2.8,dc.js,React)

时间:2016-11-09 20:38:17

标签: reactjs d3.js dc.js

在d3 v3中构建dc.js数据表后,想要在d3 v4.2.8中构建相同的数据表(为折线图做了,请参阅DC.js to React conversion)。保持错误bitRateTableChart.dimension (...)。group(...)。columns不是函数(...)。似乎没有正确定义列。 TableChart.js的代码:

import React, { PropTypes } from 'react';
import ReactDOM from "react-dom";
import * as d3 from 'd3';
import dc from "dc";
import * as crossfilter from 'crossfilter';
import {Jumbotron } from 'react-bootstrap'; 
import functionDCHelper from './functionDCHelper';
import {scaleTime, scaleLinear} from 'd3-scale';


class TableChart extends React.Component {
    componentDidMount() {
        var bitRateTableChart = dc.compositeChart(this.refs.tableChart);
        var  dateDimension  = functionDCHelper.generateValues(this.props.data);
        bitRateTableChart /* dc.dataTable('.dc-data-table', 'chartGroup') */
       .dimension(dateDimension)    //  .dimension(dateDimension)
       // Data table does not use crossfilter group but rather a closure as a grouping function
       .group(function (d) {
           var format = d3.format('02d');
           return d.bitdate.getFullYear() + '/' + (d.bitdate.getMonth() ); //+ 1
      })
       // (_optional_) max number of records to be shown, `default = 25`
       .columns([
          'CHANNEL_ID',
           'BITRATE'
       ]) 

       .ordering(function (d ) { return d.bitdate; })
       // (_optional_) custom renderlet to post-process chart using [D3](http://d3js.org)
       .on('renderlet', function (table) {
          // d3.select(.tableChart).selectAll('.info');
       })
       // .size(15)
        bitRateTableChart.render();
    }

    render() {
        return( <div  ref="tableChart"> 
                </div>)

    }

}


export default TableChart;

functionDcHelper.js的代码:

                import crossfilter from 'crossfilter';
            import * as d3 from 'd3';
            import dc from 'dc';

            var minDate,min15,bitrateWeekMinIntervalGroupMove,maxDate,minIntervalWeekBitrateGroup,dateDimension,dateFormat,numberFormat,maxbit;

            function nonzero_min(chart) {
                dc.override(chart, 'yAxisMin', function () {
                    var min = d3.min(chart.data(), function (layer) {            
                        return d3.min(layer.values, function (p) {
                            return p.y + p.y0;
                        });
                    });
                    return dc.utils.subtract(min, chart.yAxisPadding());
                });
                return chart;
            }
            // 15 Min Interval - copied from https://github.com/mbostock/d3/blob/master/src/time/interval.js
            var d3_date = Date;
            function d3_time_interval(local, step, number) {
                function round(date) {
                    var d0 = local(date), d1 = offset(d0, 1);
                    return date - d0 < d1 - date ? d0 : d1;
                }
                function ceil(date) {
                    step(date = local(new d3_date(date - 1)), 1);
                    return date;
                }
                function offset(date, k) {
                    step(date = new d3_date(+date), k);
                    return date;
                }
                function range(t0, t1, dt) {
                    var time = ceil(t0), times = [];
                    if (dt > 1) {
                        while (time < t1) {
                            if (!(number(time) % dt)) times.push(new Date(+time));
                            step(time, 1);
                        }
                    } else {
                        while (time < t1) times.push(new Date(+time)), step(time, 1);
                    }
                    return times;
                }
                function range_utc(t0, t1, dt) {
                    try {
                        d3_date = d3_date_utc;
                        var utc = new d3_date_utc();
                        utc._ = t0;
                        return range(utc, t1, dt);
                    } finally {
                        d3_date = Date;
                    }
                }
                local.floor = local;
                local.round = round;
                local.ceil = ceil;
                local.offset = offset;
                local.range = range;
                var utc = local.utc = d3_time_interval_utc(local);
                utc.floor = utc;
                utc.round = d3_time_interval_utc(round);
                utc.ceil = d3_time_interval_utc(ceil);
                utc.offset = d3_time_interval_utc(offset);
                utc.range = range_utc;
                return local;
            }
            function d3_time_interval_utc(method) {
                return function (date, k) {
                    try {
                        d3_date = d3_date_utc;
                        var utc = new d3_date_utc();
                        utc._ = date;
                        return method(utc, k)._;
                    } finally {
                        d3_date = Date;
                    }
                };
            }
            // generalization of d3.time.minute copied from- https://github.com/mbostock/d3/blob/master/src/time/minute.js
            function n_minutes_interval(nmins) {
                var denom = 6e4 * nmins;
                return d3_time_interval(function (date) {
                    return new d3_date(Math.floor(date / denom) * denom);
                }, function (date, offset) {
                    date.setTime(date.getTime() + Math.floor(offset) * denom); // DST breaks setMinutes
                }, function (date) {
                    return date.getMinutes();
                });
            }


            min15 = n_minutes_interval(15);
            dateFormat = d3.timeFormat('%Y/%m/%d/%H:%M');
            //dateFormat = d3.timeParse ('%Y/%m/%d/%H:%M');
            // parse the date / time 
            //var dateFormat = d3.timeParse("%d-%b-%y");

            numberFormat = d3.format('d');

            //### Crossfilter Dimensions 
            function generateValues(data) {

                data.forEach(function (d) {
                    d.bitdate = new Date(d.DATETIME);    //d.DATETIME = dateFormat.parse(d.DATETIME);
                   // d.month = d3.time.month(d.bitdate);
                  //  d.week = d3.time.week(d.bitdate);
                    d.BITRATE = String(d.BITRATE).match(/\d+/); //d.BITRATE = +d.BITRATE;

                });

                var crossFilteredData = crossfilter(data);
                var all = crossFilteredData.groupAll();
                // Dimension by full date
                 dateDimension = crossFilteredData.dimension(function (d) {
                    return d.bitdate;
                 });


                 maxbit = d3.max(data, function (d) { return +d["BITRATE"]; }); //alert(maxbit);  

                //Group bitrate per week, 15 minInterval - maintain running tallies 
                 bitrateWeekMinIntervalGroupMove = dateDimension.group(min15).reduce(
                   /* callback for when data is added to the current filter results */
                   function (p, v) {
                       ++p.count;
                       p.BITRATE = +v.BITRATE;
                       p.total += +v.BITRATE;
                       p.avg = p.count ? Math.round(p.total / p.count) : 0;
                       return p;
                   },
                   /* callback for when data is removed from the current filter results */
                   function (p, v) {
                       --p.count;
                       p.BITRATE = +v.BITRATE;
                       p.total -= +v.BITRATE;
                       p.avg = p.count ? Math.round(p.total / p.count) : 0;
                       return p;
                   },
                   /* initialize p */
                   function () {
                       return {
                           count: 0,
                           bitrate: 0,
                           total: 0,
                           avg: 0
                       };
                   }
                );


                try {
                    minDate = dateDimension.bottom(1)[0].DATETIME;
                } catch(err) {
                    minDate = new Date("2016-06-14 0:00"); 
                }


                try {
                    maxDate = dateDimension.top(1)[0].DATETIME;
                } catch(err) {
                     maxDate = new Date("2016-06-18 23:55");
                }

                return {
                    min15, minDate, maxDate, bitrateWeekMinIntervalGroupMove,minIntervalWeekBitrateGroup, dateDimension, maxbit
                };
            }

            export default {
                generateValues,
                nonzero_min,
                dateFormat,
                numberFormat
            };

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据Gordon的回答,这里是dataTable的当前代码(dc.js,crossfilter,d3 v4.2.8,React,):

                import React, { PropTypes } from 'react';
                import ReactDOM from "react-dom";
                import * as d3 from 'd3';
                import dc from "dc";
                import * as crossfilter from 'crossfilter';
                import {Jumbotron } from 'react-bootstrap'; 
                import functionDCHelper from './functionDCHelper';
                import {scaleTime, scaleLinear} from 'd3-scale';

                class TableChart extends React.Component {
                    componentDidMount() {
                        var bitRateTableChart = dc.dataTable(this.refs.tableChart);
                        var { dateDimension } = functionDCHelper.generateValues(this.props.data);
                        bitRateTableChart
                       .dimension(dateDimension) 
                       // Data table does not use crossfilter group but rather a closure as a grouping function
                       .group(function (d) {
                           var format = d3.format('02d');
                           return d.bitdate.getFullYear() + '/'  + format((d.bitdate.getMonth() + 1));
                      })
                       .columns(
                       [
                         'DATETIME',
                         'CHANNEL_ID',
                         'BITRATE'
                       ]) 
                       .ordering(function (d ) { return d.bitdate; })   /// sortBy(function (d) { return d.bitdate; })
                       // (_optional_) custom renderlet to post-process chart using [D3](http://d3js.org)
                       .on('renderlet', function (table) {
                           table.selectAll('.dc-table-group').classed('info', true);
                       })
                       .size(15)
                        bitRateTableChart.render();// dc.renderAll();
                    }
                    render() {
                        return( 
                            <div className="row">
                               <div className="table table-hover dc-data-table dc-table-row" ref="tableChart"> 
                            </div>
                        </div>
                        )
                    }
                }

                export default TableChart;