2017-08-29 41 views
0

我正在尝试为我的图表创建明智的数据数组。结合每个月的所有销售和购买价值动态月数据集

谁能帮助我获得预期的产出将会非常有帮助。我重视我与我的预期输出试图创建图表

如果可能的话,指导我一些Java脚本功能,这可以是有用的,我

http://jsfiddle.net/qjsgy6a6/2/

var mainData = [ 
    { 
    "date": "2017-01-03", 
    "month": "JAN", 
    "sales": "200", 
    "purchase": "1000" 
    }, 
    { 
    "date": "2017-01-18", 
    "month": "JAN", 
    "sales": "800", 
    "purchase": "2500" 
    }, 
    { 
    "date": "2017-01-22", 
    "month": "JAN", 
    "sales": "400", 
    "purchase": "2100" 
    }, 
    { 
    "date": "2017-02-20", 
    "month": "FEB", 
    "sales": "40", 
    "purchase": "90" 
    }, 
    { 
    "date": "2017-02-28", 
    "month": "FEB", 
    "sales": "970", 
    "purchase": "2100" 
    }, 
    { 
    "date": "2017-02-29", 
    "month": "FEB", 
    "sales": "3300", 
    "purchase": "2900" 
    }, 
    { 
    "date": "2017-03-20", 
    "month": "MAR", 
    "sales": "600", 
    "purchase": "900" 
    } 
] 


// Expected Output - how can I achieve this 

{ 
    "data": [ 
    { 
     "data": [ 
     { 
      "event": "sales", 
      "inventory": [ 
      { 
       "value": "200" //Jan 
      }, 
      { 
       "value": "40" //Feb 
      }, 
      { 
       "value": "600" //Mar 
      } 
      ] 
     }, 
     { 
      "event": "purchase", 
      "inventory": [ 
      { 
       "value": "1000" 
      }, 
      { 
       "value": "90" 
      }, 
      { 
       "value": "900" 
      } 
      ] 
     } 
     ] 
    }, 
    { 
     "data": [ 
     { 
      "event": "sales", 
      "inventory": [ 
      { 
       "value": "800" 
      }, 
      { 
       "value": "970" 
      } 
      ] 
     }, 
     { 
      "event": "purchase", 
      "inventory": [ 
      { 
       "value": "2500" 
      }, 
      { 
       "value": "2100" 
      } 
      ] 
     } 
     ] 
    }, 
    { 
     "data": [ 
     { 
      "event": "sales", 
      "inventory": [ 
      { 
       "value": "400" 
      }, 
      { 
       "value": "3300" 
      } 
      ] 
     }, 
     { 
      "event": "purchase", 
      "inventory": [ 
      { 
       "value": "2100" 
      }, 
      { 
       "value": "2900" 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 
+0

令人困惑的是:你的数据分布在JAN,FEB和MAR上,但是你的数据分布在Q1,Q2和Q3。这是故意的吗? – trincot

+0

@trincot对不起我的错误,我刚刚发布了我的图表数据的一部分..更新了我的小提琴。任何方式从mainData对象获得预期的输出 – user4324324

回答

1

您可以使用小提琴这个功能:

function transformData(data) { 
 
    return { 
 
     data: data.reduce ((acc, item) => { 
 
      let i = acc.months.get(item.month) || 0; 
 
      acc.data[i] = acc.data[i] || { 
 
       data: [{ 
 
        event: "sales", 
 
        inventory: [] 
 
       }, { 
 
        event: "purchase", 
 
        inventory: [] 
 
       }] 
 
      }; 
 
      acc.data[i].data[0].inventory.push({ value: item.sales }); 
 
      acc.data[i].data[1].inventory.push({ value: item.purchase }); 
 
      acc.months.set(item.month, i+1); 
 
      return acc; 
 
     }, { months: new Map, data: [] }).data 
 
    }; 
 
} 
 

 
// Input 
 
var data = [ 
 
    { 
 
    "date": "2017-01-03", 
 
    "month": "JAN", 
 
    "sales": "200", 
 
    "purchase": "1000" 
 
    }, 
 
    { 
 
    "date": "2017-01-18", 
 
    "month": "JAN", 
 
    "sales": "800", 
 
    "purchase": "2500" 
 
    }, 
 
    { 
 
    "date": "2017-01-22", 
 
    "month": "JAN", 
 
    "sales": "400", 
 
    "purchase": "2100" 
 
    }, 
 
    { 
 
    "date": "2017-02-20", 
 
    "month": "FEB", 
 
    "sales": "40", 
 
    "purchase": "90" 
 
    }, 
 
    { 
 
    "date": "2017-02-28", 
 
    "month": "FEB", 
 
    "sales": "970", 
 
    "purchase": "2100" 
 
    }, 
 
    { 
 
    "date": "2017-02-29", 
 
    "month": "FEB", 
 
    "sales": "3300", 
 
    "purchase": "2900" 
 
    }, 
 
    { 
 
    "date": "2017-03-20", 
 
    "month": "MAR", 
 
    "sales": "600", 
 
    "purchase": "900" 
 
    } 
 
]; 
 

 
// Conversion 
 
var result = transformData(data); 
 

 
// Output 
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

谢谢!它为我工作 – user4324324