Thursday, 1 June 2017

MongoDB & AMCharts

I am trying to create an AMchart with some values from my mongoDB using mongoose and Node.js. Since Node.js is asynchronous I can't pass my values from the query to the chart, If I put the chart creation inside the same function scope as the query when I do the calling in the handlebars I get nothing since the chart creation is inside the query, but this is the onñy way I have been able to retrieve the data. My code is as follows:

<script>
  var Edad = require('../models/edad');
        var data = [];
        Edad.find().lean().exec(function(err, docs) {
                if (err) throw err;
                data = docs;
                for (var i in data) {
                        delete data[i]._id;
                        delete data[i].__v;
                }
        });
var chart = AmCharts.makeChart( "chartdiv", {
  "type": "serial",
  "theme": "patterns",
  "dataProvider": data,
  "valueAxes": [ {
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  } ],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [ {
    "balloonText": "Edad: [[category]]: <b> Numero Vacunas: [[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "vacunas"
  } ],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "edad",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0,
    "tickPosition": "start",
    "tickLength": 20
  },
  "export": {
    "enabled": true
  }

} );
</script>
<div id="main-wrapper">
        <div class="container">
            <article class="box post">
                        <header>
                                <h2>Vacunas Por Edad</h2>
                                <p>Estadisticas para el numero de vacunas aplicadas por cada grupo de edad de los pacientes</p>
                        </header>
            <div id="chartdiv"></div>
        </article>
        </div>
</div>

I Need the data from the JSON "data" to be the value for "data provider" in the chart creation, any help would be much appreciated



via Juan David Ospina

No comments:

Post a Comment