dc.js icon indicating copy to clipboard operation
dc.js copied to clipboard

Is there any way to retrieve the chart object by its id

Open Smmw opened this issue 9 years ago • 1 comments

I am adding rowcharts according the user input, append needed div elements, then add rowchart to this element, but I do not know how to retrieve this chart when when handle the label, when I use dc.rowChart("#rowchart_" + checkedValues[i]), it worked but it will throw me error, about the dimensions, and also I cannot set the reset link for the chart.

Is there ant way to retrieve the chart object?

Appreciate any help!!! Thanks!

   for(var i = 0; i < checkedValues.length; i++){
        $(".rowCharts").append("<div id='rowchart_" + checkedValues[i] + "' class='col-sm-12 col-md-12 col-lg-5'></div>");
        var chart = dc.rowChart("#rowchart_" + checkedValues[i]);
        var dimension = ndx.dimension(function (d) { return d[checkedValues[i]]; });
        var group = dimension.group();

        chart
            .width(400)
            .height(300)
            .margins({ top: 20, left: 10, right: 10, bottom: 20 })
            .dimension(dimension)
            .group(group)
            .label(function (d) { 
                if (chart.hasFilter() && !chart.hasFilter(d.key)) { 
                    return d.key + '(0%)'; 
                }
                var label = d.key;
                if (all.value()) { 
                    label += '(' + (d.value / all.value() * 100).toFixed(2) + '%)'; 
                }
                return label;
             })
            .title(function (d) { return d.value; })
            .on('filtered.monitor', chartFilteredCallback)
            .elasticX(true)
            .rowsCap(10)
            .ordering(function (d) { return -d.value })
            .xAxis().ticks(5);
            $("#rowchart_" + checkedValues[i]).html('<strong>' + checkedValues[i] + '</strong><span class="reset" style="display: none;">Selected: <span class="filter"></span></span><a class="reset" href="javascript:redrawAll(chart);" style="display: none;"> reset</a>');

    }

Updated code, the last line is the problem.

function redrawAll(dcChart) {
    dcChart.filterAll();
    dc.redrawAll();
    callDrawForceLayout();
}

Smmw avatar Nov 29 '17 07:11 Smmw

@Smmw discovered the right way to do this (just keep an array or map of charts) and posted the answer on the SO question:

https://stackoverflow.com/questions/47527936/is-there-any-way-to-retrieve-the-chart-object-by-its-id-in-dc-js

gordonwoodhull avatar Dec 21 '17 04:12 gordonwoodhull