显示来自Meteor.users的用户信息,并以Highcharts显示

时间:2017-11-30 13:47:35

标签: javascript mongodb meteor highcharts

我是Meteor的新手,这就是我想做的事情:我有一些用户会回答不同类型的问题:感觉答案是我将为每个用户存储在mongoDB中的答案。 现在这是我的HTML:

<p>Select a user to see infos, see answers' progress</p>
{{#each allUsers}}

  <div class="patinf">
    <br>
    <h5><i class="fa fa-user"></i> {{lastName}} {{firstName}}</h5> 
    <label>
      <input type="radio" class="radio-inline" name="{{this.id}}" id="show" value="show" required>
      <span><i class="fa fa-bar-chart" aria-hidden="true"></i> Show answers</span>
    </label> 
    <label>
      <input type="radio" class="radio-inline" name="{{this.id}}" id="hide" value="hide" checked>
        <span><i class="fa fa-times" aria-hidden="true"></i> Hide</span
    </label>
  </div>
  {{#if show}}
    <div class="form-group">
      <label for="something" class="control-label">Answers' Progress</label>
      <div id="something">
        <p>Feelings:</p>
        <ul>
          {{#each allFeelings}}
            <li>{{feel}}</li>
          {{/each}}
        </ul>
        <p>Answers:</p>
        <ul>
          {{#each allAnswers}}
            <li>{{answer}}</li>
          {{/each}}
        </ul>
        <br>
       </div>
       <div id="something" style=" min-width: 310px; height: 400px; margin: 0auto">
         {{> highchartsHelper chartId="feels" chartWidth="100%" charHeight="100%" chartObject=topGenresChart}}
         <br>
         {{> highchartsHelper chartId="answers" chartWidth="100%" charHeight="100%" chartObject=topGenresChart}}
       </div>
     </div>
   {{/if}}
 {{/each}}

这是我的js文件:

Meteor.subscribe('patientInfos');

Template.patients.helpers({
  allAnswers:function(){
    return Quests.find({"answer": {$ne:null}});
  },
  answer: function(){
    return this.answer;
  }
});

Template.patients.helpers({
  allFeelings:function(){
    return Quests.find({"feel": {$ne:null}});
  },
  feel: function(){
    return this.feel;
  }
});

Template.patients.helpers({
  allUsers: function() {
    return Meteor.users.find({});
  },
  id: function(){
    ret
  },
  firstName: function() {
    return this.profile.firstName;
  },
  lastName: function() {
    return this.profile.lastName;
  }
});


Template.patients.onRendered(function () {
  Session.set('show', false);
});

Template.patients.events({
  'change #hide': function (event) {
    Session.set('show', false);
  },
  'change #show': function (event) {
    Session.set('show', true);
  }
});

Template.patients.helpers({
  show: function() {
    return Session.get('show');
  },
});

Template.patients.topGenresChart = function() {
    return {
        chart: {
            type: 'areaspline'
        },
        title: {
        text: 'Answers Progress'
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        verticalAlign: 'top',
        x: 150,
        y: 100,
        floating: true,
        borderWidth: 1,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    xAxis: {
        categories: [
            'W1',
            'W2',
            'W3',
            'W4',
            'W5',
            'W6',
            'W7'
        ],
        plotBands: [{ // last week
            from: 5.5,
            to: 7,
            color: 'rgba(68, 170, 213, .2)'
        }]
    },
    yAxis: {
        title: {
            text: 'Answers'
        },
        categories: [
            '0',
            '1',
            '2',
            '3',
            '4',
            '5',
            '6'
        ],
    },
    tooltip: {
        shared: true,
        valueSuffix: ' points'
    },
    credits: {
        enabled: false
    },
    plotOptions: {
        areaspline: {
            fillOpacity: 0.5
        }
    },
    series: [{
        name: 'Question 1 Progress',
        data: [3, 4, 3, 5, 1, 5, 6]
    }, {
        name: 'Question 2 Progress',
        data: [1, 3, 2, 3, 2, 5, 4]
    }]}
};

现在问题是如何将来自Quests.answer的数据放入series.data中,考虑到答案是10个数字的数组。 如何使用#each函数一次显示一个用户的答案数据:实际上如果我选择show我将在每个用户下看到所有用户的答案。

任务就像:

Schemas.Quests = new SimpleSchema
createdAt:
    type: Date
    autoValue: ->
        if this.isInsert
            new Date()
answer:
    type: [Number]
    optional:true

feel:
    type: Number
    optional:true

userId:
    type: String

patient: 
    type: String
    optional:true

感觉可能是一个数字:0 1 2。 答案是一个10项的数组,数字从0到6。

编辑:

MongoDB元素的示例: - Meteor.users

createdAt: Tue Dec 05 2017 10:56:24 GMT+0100 
__proto__: Object
emails : Array(1)
      0 : {address: "denise@test.it", verified: false}
      length : 1
      __proto__ : Array(0)
profile :
      firstName : "Denise"
      lastName : "Blabla"
      __proto__ : Object
services :
      password: {bcrypt: "$2a$10$ddJ8F.k2uJ2lZaDfvMNEdObxdMXwAdxSSQRYtHRG6Juoh8HVtC8Ju"}
      resume : {loginTokens: Array(0)}
      __proto__ : Object
_id: "RTS2LR2jaBjidEiB7"
__proto__:Object
length : 4
__proto__: Array(0)

这是任务的一个例子:

0 : 
  answer : (10) ["1", "5", "6", "5", "1", "5", "5", "6", "0", "2"]
  patient : "Denise Blabla"
  userId : "RTS2LR2jaBjidEiB7"
  _id : "7NwjGmGyz7zjbzBqC"
  __proto__ : Object
1 :
  feel : "0"
  patient : "Denise Blabla"
  userId : "RTS2LR2jaBjidEiB7"
  _id : "5KtDQof3o9gt8CYJg"
  __proto__ : Object
2 :
  answer : (10) ["0", "4", "4", "0", "4", "5", "0", "1", "3", "6"]
  patient : "Denise Blabla"
  userId : "RTS2LR2jaBjidEiB7"
  _id : "7t46pAihMBNWwmYpN"
  __proto__ : Object

我想要的是在表格中显示这样的内容:

Denise Blabla
  Week   ||  Q1  ||  Q2  ||  ...  ||  Q10  ||  Feel  
  Week 1 ||  6   ||  4   ||  ...  ||  1    ||   0
  Week 2 ||  3   ||  1   ||  ...  ||  5    ||   2

Anne Smith
  Week   ||  Q1  ||  Q2  ||  ...  ||  Q10  ||  Feel 
  Week 1 ||  5   ||  5   ||  ...  ||  1    ||   1
  Week 2 ||  3   ||  2   ||  ...  ||  3    ||   0

1 个答案:

答案 0 :(得分:1)

问题的第一部分,显示模板中相关集合Quests的数据:

Template.patients.helpers({
  allAnswers() {
    return Quests.find({ userId: this._id, answer: { $exists: true }}).map((el) => el.answer);
  },
  allFeelings() {
    return Quests.find({ userId: this._id, feel: { $exists: true }}).map((el) => el.feel);
  }
});

这基本上是使用userId作为匹配键在相关集合上进行内联 join

您的高保真将以类似的方式完成:

Template.patients.topGenresChart = function() {
    return {
      ... chart parameters as you had them before

    series: [{
        name: 'Question 1 Progress',
        data: () => { Quests.find({ userId: this._id, answer: { $exists: true }}).map((el) => el.answer)}
    }, {
        name: 'Question 2 Progress',
        data: () => { Quests.find({ userId: this._id, feel: { $exists: true }}).map((el) => el.feel)}
    }]}
};

然而:

  1. 在您的问题中,您说answer是一个包含10个数字的数组,但您的架构已将其定义为String。你的意思是[String]
  2. 您说feel是一个数字0 1 2,但您的模板正在使用{{#each}}进行迭代。
  3. 你在两个系列中想要绘制的图表一点也不清楚。您的示例数据包含7个元素的数组,但是Quests中应该包含哪些数据?
  4. 您指的是相同的图表数据:chartObject=topGenresChart两次来自{{> highchartsHelper ...}} - 这意味着即使您为每个图表提供不同的ID,您也会两次显示完全相同的图表。你的意图是什么?
  5. 无论如何,答案应该足以让你开始。

    另请注意,您的feelanswer助手是多余的。您可以直接在模板中使用{{feel}}{{answer}},而无需通过帮助程序,因为这些帮助程序分别只返回this.feelthis.answer