0

Router:

router.get('/:id', isLoggedIn, function (req, res) {
    Answer.findOne({ post: req.params.id }, function (error, answer) {
        Post.findOne({ _id: answer.post }, function (err, post) {
            res.render('post/stats', { title: post.ask });
        });
    });
});

Chart function:

var chart = new Chart($("#answerChart"), {
  type: 'pie',
  data: {
    datasets: [{
      label: "",
      backgroundColor: ["#3e95cd", "#8e5ea2"]
    }]
  },
  options: {
    title: {
      display: true,
      text: '{{ title }}'
    }
  }
});

chart.data.labels[0] = "Yes";
chart.data.labels[1] = "Test";
chart.data.datasets[0].data[0] = 5;
chart.data.datasets[0].data[1] = 3;

And I need to retrieve data dynamically from mongoose but I don't know how to do that.

1
  • Basically two things. 1. You should implement an additional API endpoint which responds desired data, 2. You should implement AJAX call to fetch that data and plug it into chart.js library. Commented Sep 11, 2017 at 8:01

1 Answer 1

1

you can stringify in nodejs code as below

router.get('/doughnut', function(req, res) {
  data = [65, 59, 20, 81, 56, 55, 40, 62, 85, 76, 65, 81];
  console.log('doughnut data',data);
  res.render('dashboard/doughnut', {
    title:'My First Doughnut Chart', 
    datai: JSON.stringify(data)
  });
});

and in .pug template you parse it like
var idata = JSON.parse('!{datai}'); 

https://github.com/hiralyshah/chartjs_mongoose_mongoDB

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.