Pregunta

I prepare a charts with dc.js. I need to show it on two place. I try to give same id for two div but only one chart is shown. Therefore currently I have to make two separate charts with same data, same everything. the one div with different id to show the second chart which is exact look of first one. How do I avoid repetitive codes for the second chart. I try chart2 = chart1; but it does not work.

¿Fue útil?

Solución

Probably the best way is to put all your initialization in a function and then call it once with each instance. Something like:

function initChart(chart) {
    chart.dimension(...)
        .group(...)
        ...
    ;
}

var chart1 = dc.barChart("#id1"), chart2 = dc.barChart("#id1");
initChart(chart1);
initChart(chart2);

Keep in mind that if you use the same dimension they will not filter each other, so you may want to do that part separately, depending on your app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top