CartoDB – Including Chart Based on Fields in CartoDB Infowindow

cartocarto-jscartocss

I would like to make graphs, pie charts, etc based on fields in my table within my infowindows. Is there a way to accomplish this?

Best Answer

In cartodb.js you have an example integration with google charts. Other charting libraries should work in the same way. You can see it working here.

The key is to call the chart function inside the infowindow template, note draw_chart call:

 var INFOWINDOW_TEMPLATE = [
        '<div class="cartodb-popup v2">',
        '  <a href="#close" class="cartodb-popup-close-button close">x</a>',
        '   <div class="cartodb-popup-content-wrapper">',
        '     <div class="cartodb-popup-content">',
        '       <div id="chart_div">',
        '       <script>',
          '       draw_chart([{{content.data.pop_min}}, {{content.data.pop_max}}], "{{content.data.name}}");',
        '       </scr' + 'ipt>',
        '       </div>',
        '     </div>',
        '   </div>',
        '   <div class="cartodb-popup-tip-container"></div>',
        '</div>'].join('');
Related Question