【问题标题】:dc.js chart redrawing with new group function with click eventdc.js 图表重绘与新的组功能与点击事件
【发布时间】:2016-01-28 11:13:42
【问题描述】:

当其他图表库单击事件时,我正在尝试使用新组重绘 dc.js 图表。但它不会重绘。

这是我的 javascript 代码:

 var donemChart = dc.rowChart('div#donem'),
        hospitalTypeChart = dc.rowChart('div#hospital_type'),
        ckysHospitalClassChart = dc.rowChart('div#ckys_hospital_class'),
        ckysHospitalTypeChart = dc.rowChart('div#ckys_hospital_type'),
        hospitalHealthRegionChart = dc.rowChart('div#hospital_health_region'),
        hospitalRoleChart = dc.rowChart('div#hospital_role'),
        hospitalsChart = dc.rowChart('div#hospitals');




    d3.json('tsim.json',function(data){

        var ndx = crossfilter(data);
        var all = ndx.groupAll();




        //define crossfilter dimensions

        var donemDim = ndx.dimension(function(d){ return d.DONEM}),
                hospitalTypeDim = ndx.dimension(function(d){ return d.KURUM_TURU}),
                ckysHospitalClassDim = ndx.dimension(function(d){ return d.CKYS_KURUM_TURU}),
                ckysHospitalTypeDim = ndx.dimension(function(d){ return d.CKYS_KURUM_TIPI}),
                hospitalHealthRegionDim = ndx.dimension(function(d){ return d.SAGLIK_BOLGESI}),
                hospitalRoleDim = ndx.dimension(function(d){ return d.HASTANE_ROL}),
                hospitalsDim = ndx.dimension(function(d){return d.HASTANE}) ;

        var donemGroup = donemDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            hospitalTypeGroup = hospitalTypeDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            ckysHospitalClassGroup = ckysHospitalClassDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            ckysHospitalTypeGroup = ckysHospitalTypeDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            hospitalRoleGroup = hospitalRoleDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            hospitalsGroup = hospitalsDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
            hospitalHealthRegionGroup = hospitalHealthRegionDim.group().reduceSum(function(d){return d.toplam_muayene_say});







        donemChart
                .width(400)
                .height(300)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(donemGroup)
                .dimension(donemDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();

        hospitalTypeChart
                .width(400)
                .height(300)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(hospitalTypeGroup)
                .dimension(hospitalTypeDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return d.value;
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();

        ckysHospitalClassChart
                .width(400)
                .height(300)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(ckysHospitalClassGroup)
                .dimension(ckysHospitalClassDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();

        ckysHospitalTypeChart
                .width(400)
                .height(500)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(ckysHospitalTypeGroup)
                .dimension(ckysHospitalTypeDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();


        hospitalHealthRegionChart
                .width(400)
                .height(500)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(hospitalHealthRegionGroup)
                .dimension(hospitalHealthRegionGroup)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();

        hospitalRoleChart
                .width(400)
                .height(500)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(hospitalRoleGroup)
                .dimension(hospitalRoleDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();


        hospitalsChart
                .width(1000)
                .height(25000)
                .margins({top: 0, left: 0, right: 0, bottom: 0})
                .group(hospitalsGroup)
                .dimension(hospitalsDim)
                .ordinalColors(['#E89F00'])
                .label(function (d) {
                    return d.key;
                })

                .title(function (d) {
                    return "";
                })
                .ordering(function(d) { return -d.value; })
                .valueAccessor(function(p) { return p.value; })
                .elasticX(true)
                .xAxis().tickFormat();

        chart.addListener("clickGraphItem", function(event){

            if(event.item.dataContext.d_type==1){

                var donemGroup = donemDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        hospitalTypeGroup = hospitalTypeDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        ckysHospitalClassGroup = ckysHospitalClassDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        ckysHospitalTypeGroup = ckysHospitalTypeDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        hospitalRoleGroup = hospitalRoleDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        hospitalsGroup = hospitalsDim.group().reduceSum(function(d){return d.toplam_muayene_say}),
                        hospitalHealthRegionGroup = hospitalHealthRegionDim.group().reduceSum(function(d){return d.toplam_muayene_say});

                dc.redrawAll();



            }else if(event.item.dataContext.d_type==2){

                var donemGroup = donemDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        hospitalTypeGroup = hospitalTypeDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        ckysHospitalClassGroup = ckysHospitalClassDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        ckysHospitalTypeGroup = ckysHospitalTypeDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        hospitalRoleGroup = hospitalRoleDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        hospitalsGroup = hospitalsDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say}),
                        hospitalHealthRegionGroup = hospitalHealthRegionDim.group().reduceSum(function(d){return d.toplam_A_grubu_ameliyat_say});


                dc.redrawAll();
            }





            dc.redrawAll();


        });








        dc.renderAll();






    });

当单击图表时,我在图表侦听器函数中检查数据类型后获取数据类型。并且我将维度与新组分组。当它被 consol 记录时,新组显示但它不会影响重绘图表.

我能解决这个问题吗?我们如何解决这个问题?

谢谢

【问题讨论】:

    标签: javascript jquery d3.js dc.js crossfilter


    【解决方案1】:

    您刚刚将hospitalTypeGroup 变量的引用(例如)重新分配给了不同的对象。但是图表仍然持有对旧对象的引用。您必须使用hospitalTypeChart.group(hospitalTypeGroup) 重置hospitalTypeChart 上的组,然后重新渲染。 (我认为在很多情况下重绘在这里都行不通。)

    【讨论】:

      【解决方案2】:

      您需要提醒 dc.js 已更改的组。

      仅创建具有相同名称的新维度和组不会替换它们。您还需要为每个图表适当地调用.group()

      另外,我不建议在每次单击某些内容时创建新组(或维度)。 Crossfilter 将随着每个新组而减慢速度,因为它们都需要响应每个过滤器操作。相反,请提前创建组并在点击处理程序中应用它们。

      【讨论】:

      • Gordon,谢谢,但是我们如何使用 crossfilter 过滤功能呢?
      • 不确定你在问什么。
      • 您能否举例说明或链接有关该问题的示例?
      猜你喜欢
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多