ApexCharts barchart vertical categories logic in ReactJs?

拟墨画扇 提交于 2020-01-16 17:27:28

问题


I am having a problem trying to figure out how to display the correct data to the correct category. My data is in this format from a json API:

{
  "name" : "product1",
  "records": "5",
  "month" : "Jan",
},
{
  "name" : "product1",
  "records": "10",
  "month" : "Feb",
},
{
  "name" : "product1",
  "records": "20",
  "month" : "March",
},
{
  "name" : "product2",
  "records": "5",
  "month" : "Feb",
}

an example of the data model.

this.state = {
  barChart: { 
   options: {
      plotOptions: {
        xaxis: {
          categories: []
       }}}
        series: [{name: [], data: []}}

This is the state in ReactJs from apexcharts I've been spending couple of days and I've tried to sort it based on alphabet: which the data was wrong displayed in the graph. I was reading the documentation but didn't figure out how to do, or how to get the logic right. the categories cannot repeat so: [Jan, Feb, March] and the data[records] has to be correct in it's own category.


回答1:


The following code will create a series object for each Product. Each Product will have its own data array. Where each number corresponds sequentially to a month. A 0 value is added in place for a month that was not utilized in the data-set for that product.

Example dataset:

let data = [{
  "name" : "product1",
  "records": "5",
  "month" : "Jan",
},
{
  "name" : "product1",
  "records": "10",
  "month" : "Feb",
},
{
  "name" : "product1",
  "records": "20",
  "month" : "Mar",
},
{
  "name" : "product2",
  "records": "5",
  "month" : "Feb",
},
{
  "name" : "product1",
  "records": "5",
  "month" : "May",
},
{
  "name" : "product2",
  "records": "5",
  "month" : "Jun",
}
]

This creates an array of months that were used in the data-set. No duplicates. We will use it to map the corresponding data-values of each product in their specific month.

Creating Categories:

let months = data.map((item) => item.month).filter((item, index, array) => array.indexOf(item) == index)

Creating Series:

const productTotals = data.reduce((obj, curr) => {
    if(!obj[curr.name]){
        obj[curr.name] = []
    }

    obj[curr.name][months.indexOf(curr.month)] = parseInt(curr.records)
    return obj
}, {})

const series = Object.entries(productTotals).map(([name, prodArr]) => {
    return {
        name: name,
        data: months.map((month, monthIndex) => {
            if(!prodArr[monthIndex]){
                return 0
            } else {
                return prodArr[monthIndex]
            }

        })
    }

})

Then simply update the properties with your new series data and categories.

this.state = {
  barChart: { 
   options: {
      plotOptions: {
        xaxis: {
          categories: [...months]
       }}}
        series: [...series]}


来源:https://stackoverflow.com/questions/56288819/apexcharts-barchart-vertical-categories-logic-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!