highcharts-demo icon indicating copy to clipboard operation
highcharts-demo copied to clipboard

get data from mongodb

Open aligos opened this issue 9 years ago • 65 comments

please help, i want to create charts with package meteor add maazalik:highcharts, using columnDemo https://github.com/jhuenges/highcharts-demo/blob/master/client/demos/columnDemo.js but i did'nt know how to get data from databes to this charts, i am using meteor example todos. https://github.com/aligos/pomodoro/blob/master/client/templates/chart.js

aligos avatar Jan 25 '16 11:01 aligos

Can you check if this helps? If not, I can provide you with some more help.

jhuenges avatar Jan 25 '16 17:01 jhuenges

actually i want to ask example like that :smile:

aligos avatar Jan 26 '16 03:01 aligos

Okay, I have to see if I have some time later today.

jhuenges avatar Jan 26 '16 08:01 jhuenges

I have managed to make monthly count, https://github.com/aligos/pomodoro/blob/master/client/templates/chart.js but I want to make per category as an example here, https://github.com/jhuenges/highcharts-demo/blob/master/client/demos/columnDemo.js

aligos avatar Jan 26 '16 10:01 aligos

Can you give me an example of how your data looks (Todos.findOne({...}))? I am not 100% sure i understand what you want.

jhuenges avatar Jan 26 '16 17:01 jhuenges

this ss https://i.imgur.com/IY9DwZ7.png

and this my route https://github.com/aligos/pomodoro/blob/master/lib/router.js

aligos avatar Jan 27 '16 02:01 aligos

Try this:

var chart;
/*
 * Function to draw the column chart
 */
function builtColumn(chartData) {

  chart = $('#container-column').highcharts({

    chart: {
      type: 'column'
    },

    title: {
      text: 'Monthly Todos'
    },

    credits: {
      enabled: false
    },

    xAxis: {
      categories: [
        'Jan',
        'Feb',
        'Mar',
        'Apr',
        'May',
        'Jun',
        'Jul',
        'Aug',
        'Sep',
        'Oct',
        'Nov',
        'Dec'
      ]
    },

    yAxis: {
      min: 0,
      title: {
        text: 'Todos'
      }
    },

    tooltip: {
      headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
      pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
        '<td style="padding:0"><b>{point.y:f}</b></td></tr>',
      footerFormat: '</table>',
      shared: true,
      useHTML: true
    },

    plotOptions: {
      column: {
        pointPadding: 0.2,
        borderWidth: 0
      }
    },

    series: chartData

  });
}

/*
 * Call the function to built the chart when the template is rendered
 */
Template.columnDemo.onRendered(function() {
  Tracker.autorun(function() {
    var data = Todos.find().fetch();

    // if there is no data, abort!
    if (!data || data === []) return;

    // Group data by listId
    data = _.groupBy(data, function(todo) {
      return todo.listId
    });

    // Array that contains the data for the chart
    var chartData = [];
    // Iterate over each list
    _.each(data, function(list) {
      // Create an object which contains the name and data for the list
      var listData = {};
      // Get the name/id of the list
      // This can be changed to use an actual name
      listData.name = list[0].listId;
      // Count the numbers of todos per month
      listData.data = _.pluck(list, 'bulan');
      listData.data = _.countBy(listData.data, function(bulan) {
        return bulan
      });
      // Add default values to all of the month - just in case there is no todo
      // so that the chart is still able to display everything correctly
      listData.data = _.defaults(listData.data, {
        Jan: 0,
        Feb: 0,
        Mar: 0,
        Apr: 0,
        May: 0,
        Jun: 0,
        Jul: 0,
        Aug: 0,
        Sep: 0,
        Oct: 0,
        Nov: 0,
        Dec: 0
      });
      listData.data = _.toArray(listData.data);
      chartData.push(listData);
    });

    // if there is no chart, built it
    if (!chart) {
      builtColumn(chartData);
      return;
    }

    // Otherwise, simply update the data if there is chartData
    if (chartData.length !== 0) {
      chart.highcharts().series[0].update({
        data: chartData
      });
    }
  });
});

jhuenges avatar Jan 27 '16 21:01 jhuenges

thank you very much, you really helped Cool bro

aligos avatar Jan 28 '16 03:01 aligos

i cannot change listId to name :cry:

aligos avatar Jan 28 '16 03:01 aligos

You could try to do this in the _.each()-block:

listData.name = Lists.findOne(list[0].listId).name;

or

var listSource = Lists.findOne(list[0].listId)
if (listSource) {
  listData.name = listSource.name;
} else {
  listData.name = "unknown list"
}

jhuenges avatar Jan 28 '16 08:01 jhuenges

work again :smile: I have tried from this morning but do not succeed

aligos avatar Jan 28 '16 08:01 aligos

Do you know why i have to make reload page to get chart?, before i use datetimepicker and need reload page too

aligos avatar Jan 28 '16 09:01 aligos

I am not quite sure :/ Maybe post this question on the Meteor forum.

jhuenges avatar Jan 28 '16 15:01 jhuenges

@jhuenges sorry before, when i add data at February, but in chart show in January, but in mongo Feb you can try this https://github.com/aligos/todomoro.git

aligos avatar Feb 23 '16 09:02 aligos

Can you add console.log(listData); here, here, here and here?

Please give me the output and I will try to find a solution.

jhuenges avatar Feb 23 '16 21:02 jhuenges

wait a minute :smile:

aligos avatar Feb 23 '16 21:02 aligos

Object {name: "Change this default list name", data: Array[3]} _colorIndex: 0 data: Array[12] name: "Change this default list name" proto: Object

SS http://i.imgur.com/YdfjGmu.png

aligos avatar Feb 23 '16 21:02 aligos

Oh my bad! Can you use listData.data?

jhuenges avatar Feb 24 '16 19:02 jhuenges

do you mean console.log(listData.data)?

aligos avatar Feb 24 '16 22:02 aligos

this is when i console.log(listData.data)

http://i.imgur.com/qxlHeLk.png

aligos avatar Feb 24 '16 22:02 aligos

Okay, got it! Please exchange these lines with the following code:

var monthLookup = {
        Jan: 0,
        Feb: 1,
        Mar: 2,
        Apr: 3,
        May: 4,
        Jun: 5,
        Jul: 6,
        Aug: 7,
        Sep: 8,
        Oct: 9,
        Nov: 10,
        Dec: 11
};
// This is the data used to display the values in the chart
// Each 0 represents a month
var monthArray = [0,0,0,0,0,0,0,0,0,0,0,0];

// Iterate over listData.data to assign the values to the correct "month-slot"
for(var key in listData.data) {
  if(monthLookup[key]) {
    monthArray [monthLookup[key]] = listData.data[key];
  }
}

// Reassign the array
listData.data = monthArray;

I hope this helps

jhuenges avatar Feb 25 '16 18:02 jhuenges

Okay, got it! Please exchange these lines with the following code:

var monthLookup = {
        Jan: 0,
        Feb: 1,
        Mar: 2,
        Apr: 3,
        May: 4,
        Jun: 5,
        Jul: 6,
        Aug: 7,
        Sep: 8,
        Oct: 9,
        Nov: 10,
        Dec: 11
};
// This is the data used to display the values in the chart
// Each 0 represents a month
var monthArray = [0,0,0,0,0,0,0,0,0,0,0,0];

// Iterate over listData.data to assign the values to the correct "month-slot"
for(var key in listData.data) {
  if(monthLookup[key]) {
    monthArray[monthLookup[key]] = listData.data[key];
  }
}

// Reassign the array
listData.data = monthArray;

I hope this helps

jhuenges avatar Feb 25 '16 18:02 jhuenges

January data can not change the array http://i.imgur.com/jRyA6IY.png http://i.imgur.com/hd7SFPW.png

aligos avatar Feb 25 '16 22:02 aligos

I guess you need to take another look at this part

...

// Iterate over listData.data to assign the values to the correct "month-slot"
for(var key in listData.data) {
  if(monthLookup[key]) {
    monthArray[monthLookup[key]] = listData.data[key];
  }
}

...

I am gone for 3 weeks, so I cant help you right now.

jhuenges avatar Feb 26 '16 05:02 jhuenges

hi i'm trying to use your example @jhuenges console.log on client side return

Exception from Tracker recompute function: meteor.js:913:11 TypeError: chart.highcharts(...).series[0] is undefined

and i get : no data to display in my template

Eveorder.find().count() ->

24

var chart;
/*
 * Function to draw the column chart
 */
function builtColumn(chartData) {

  chart = $('#container-column').highcharts({

    chart: {
      type: 'column'
    },

    title: {
      text: 'Monthly Todos'
    },

    credits: {
      enabled: false
    },

    xAxis: {
      categories: [
        'Jan',
        'Feb',
        'Mar',
        'Apr',
        'May',
        'Jun',
        'Jul',
        'Aug',
        'Sep',
        'Oct',
        'Nov',
        'Dec'
      ]
    },

    yAxis: {
      min: 0,
      title: {
        text: 'Todos'
      }
    },

    tooltip: {
      headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
      pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
        '<td style="padding:0"><b>{point.y:f}</b></td></tr>',
      footerFormat: '</table>',
      shared: true,
      useHTML: true
    },

    plotOptions: {
      column: {
        pointPadding: 0.2,
        borderWidth: 0
      }
    },

    series: chartData

  });
}


Template.evemarket.onCreated(function() {
  this.subscribe('evemarketpublish');
});

/*
 * Call the function to built the chart when the template is rendered
 */
Template.evemarket.onRendered(function() {
  Tracker.autorun(function() {
    var data = Eveorder.find().fetch();
    //console.log("eveorderfind"+data);

    // if there is no data, abort!
    if (!data || data === []) return;

    // Group data by listId
    data = _.groupBy(data, function(todo) {
      return todo.listId
    });

    // Array that contains the data for the chart
    var chartData = [];
    // Iterate over each list
    _.each(data, function(list) {
      // Create an object which contains the name and data for the list
      var listData = {};
      // Get the name/id of the list
      // This can be changed to use an actual name
      listData.name = list[0].listId;
      // Count the numbers of todos per month
      listData.data = _.pluck(list, 'bulan');
      listData.data = _.countBy(listData.data, function(bulan) {
        return bulan
      });
      // Add default values to all of the month - just in case there is no todo
      // so that the chart is still able to display everything correctly
      listData.data = _.defaults(listData.data, {
        Jan: 0,
        Feb: 0,
        Mar: 0,
        Apr: 0,
        May: 0,
        Jun: 0,
        Jul: 0,
        Aug: 0,
        Sep: 0,
        Oct: 0,
        Nov: 0,
        Dec: 0
      });
      listData.data = _.toArray(listData.data);
      chartData.push(listData);
    });

    // if there is no chart, built it
    if (!chart) {
      builtColumn(chartData);
      return;
    }

    // Otherwise, simply update the data if there is chartData
    if (chartData.length !== 0) {
      chart.highcharts().series[0].update({
        data: chartData
      });
    }
  });
});

Have you an idea for fixe this ?:o

VeilleurTrytoFix avatar Apr 05 '16 16:04 VeilleurTrytoFix

@keepthefaya I have a couple of questions:

  1. I assume the chart is built correctly? So there is a container with the id="container-column"?
  2. var data = Eveorder.find().fetch(); deliveres data?
  3. In the autorun: is chart defined?

jhuenges avatar Apr 05 '16 17:04 jhuenges

1 : just change template 2 : yes Eveorder.find().count() (client side) ===> 24 3 : i dont understand what do you mean? :octopus:

**client.html**
<template name="evemarket">
    <h2>EveMarket</h2>
       <div class="grapik">
        <div id="container-column" style="min-width: 310px; height: 400px;"></div>
        <div class="title-reload"><span class="icon-reset render-chart"></span></div>
    </div>
</template>

client.js


var chart;
/*
 * Function to draw the column chart
 */
function builtColumn(chartData) {

  chart = $('#container-column').highcharts({

    chart: {
      type: 'column'
    },

    title: {
      text: 'Monthly Todos'
    },

    credits: {
      enabled: false
    },

    xAxis: {
      categories: [
        'Jan',
        'Feb',
        'Mar',
        'Apr',
        'May',
        'Jun',
        'Jul',
        'Aug',
        'Sep',
        'Oct',
        'Nov',
        'Dec'
      ]
    },

    yAxis: {
      min: 0,
      title: {
        text: 'Todos'
      }
    },

    tooltip: {
      headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
      pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
        '<td style="padding:0"><b>{point.y:f}</b></td></tr>',
      footerFormat: '</table>',
      shared: true,
      useHTML: true
    },

    plotOptions: {
      column: {
        pointPadding: 0.2,
        borderWidth: 0
      }
    },

    series: chartData

  });
}


Template.evemarket.onCreated(function() {
  this.subscribe('evemarketpublish');
});

/*
 * Call the function to built the chart when the template is rendered
 */
Template.evemarket.onRendered(function() {
  Tracker.autorun(function() {
    var data = Eveorder.find().fetch();
    //console.log("eveorderfind"+data);

    // if there is no data, abort!
    if (!data || data === []) return;

    // Group data by listId
    data = _.groupBy(data, function(todo) {
      return todo.listId
    });

    // Array that contains the data for the chart
    var chartData = [];
    // Iterate over each list
    _.each(data, function(list) {
      // Create an object which contains the name and data for the list
      var listData = {};
      // Get the name/id of the list
      // This can be changed to use an actual name
      listData.name = list[0].listId;
      // Count the numbers of todos per month
      listData.data = _.pluck(list, 'bulan');
      listData.data = _.countBy(listData.data, function(bulan) {
        return bulan
      });
      // Add default values to all of the month - just in case there is no todo
      // so that the chart is still able to display everything correctly
      listData.data = _.defaults(listData.data, {
        Jan: 0,
        Feb: 0,
        Mar: 0,
        Apr: 0,
        May: 0,
        Jun: 0,
        Jul: 0,
        Aug: 0,
        Sep: 0,
        Oct: 0,
        Nov: 0,
        Dec: 0
      });
      listData.data = _.toArray(listData.data);
      chartData.push(listData);
    });

    // if there is no chart, built it
    if (!chart) {
      builtColumn(chartData);
      return;
    }

    // Otherwise, simply update the data if there is chartData
    if (chartData.length !== 0) {
      chart.highcharts().series[0].update({
        data: chartData
      });
    }
  });
});

server.js

Meteor.publish('evemarketpublish', function() {
    return Eveorder.find();
});

VeilleurTrytoFix avatar Apr 05 '16 18:04 VeilleurTrytoFix

In these lines:

// Otherwise, simply update the data if there is chartData
    if (chartData.length !== 0) {
      chart.highcharts().series[0].update({
        data: chartData
      });
    }

chart.highcharts().series[0] is not defined. So can you check if chart and chart.highcharts() are working?

jhuenges avatar Apr 05 '16 18:04 jhuenges

I try to also look in my direction but without results at this time, I got in client side console

No data to display" and "Exception from Tracker function Recompute: meteor.js: 913: 11 TypeError. Chart.highcharts (...) series [0] is undefined

chart                     return  -> Object { length: 1, context: HTMLDocument → evemarket, selector: "#container-column", 1 de plus… }
chart.hightcharts()  return  -> [object Object]

VeilleurTrytoFix avatar Apr 06 '16 06:04 VeilleurTrytoFix

series array isset, but array series.length return --> 0

`series * receveid chartData*, i try console.log(charData), he return to me Array [ Object ]

0:Object ->data: Array[13] ->name: undefined ->proto:Object

`

VeilleurTrytoFix avatar Apr 06 '16 06:04 VeilleurTrytoFix