UI Custom Component - ApexCharts Library - Data not getting into the page

Hi team,

I am trying to create a custom component based on ApexCharts library. Viewing the material we have about the topic I came with the following code.

import React, { useState } from "react";
import Chart from "./lib/react-apexcharts/node_modules/react-apexcharts/src/react-apexcharts.jsx";

function App({ component }) {
  const [options, setOptions] = useState({
    chart: {
      id: "basic-bar"
    },
    xaxis: {
      categories: component.categories
    }
  });
  const [series, setSeries] = useState([
    {
      name: component.name,
      data: component.data
    }
  ]);

  return (
    <div className="app">
      <div className="row">
        <div className="mixed-chart">
          <Chart options={options} series={series} type={component.type} width={component.width} />
        </div>
      </div>
    </div>
  );
}

export default App;

I have linked “categories”, “type”, “seriesName” and “width” in the properties tab of the custom component builder and seems to be working. However, “data” is not getting passed in to the page once I load it. The console is displaying that “data is undefined”. So far, the config in properties I am using is the following:

Categories:
Reference in Code: categories
Value type: JSON
Default value: [ { “categories”: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999] }]

Type
Reference in Code: type
Value type: Select
Default value: line

Type
Reference in Code: type
Value type: Select
Default value: line

Width
Reference in Code: width
Value type: number
Default value: 500

Width
Reference in Code: width
Value type: number
Default value: 500

Name
Reference in Code: name
Value type: text
Default value: series 1

Data
Reference in Code: data
Value type: JSON
Default value: [{ “data”: [30, 40, 45, 50, 49, 60, 70, 91]}]

I am wondering what I am missing here, and this may be a very small detail in the code. I have tried different versions with {useState} and without the hook, but I am still not being able to get the data to populate the charts.

I would highly appreciate any suggestion.
My application ID is the following in case you need to take a look:3C3C18D3-7C64-9678-FFD9-EBB40D336C00

Thanks a lot,
Regards,
Cristian

Hi @Cristian_Ignacio_Barria_Zapata

when you use such construction:

 const [series, setSeries] = useState([
    {
      name: component.name,
      data: component.data
    }
  ]);

the object series will contain data from the first render and then if you change the component.data it still be the same

in case if you want to change the series object you need to use the useEeffect hook

 const [series, setSeries] = useState([]);

useEffect(()=>{
setSeries([
    {
      name: component.name,
      data: component.data
    }
  ])
},[component.name, component.data]);

same for options

Thanks for the advice Vladimir, but I am still encountering the same error “act_devtools_backend.js:2655 It is a possibility that you may have not included ‘data’ property in series.
o”

try to print series and check what it contains

console.log('series', series)

So, this is the code I am using now, but the screen doesn´t show anything at all now

import React, { useState, useEffect } from 'react';
import Chart from "./lib/react-apexcharts/node_modules/react-apexcharts/src/react-apexcharts.jsx";

function App({ component }) {
  const [options, setOptions] = useState([]);
  useEffect(() => {
    setOptions([
      {
        chart: {
          id: "basic-bar"
        },
        xaxis: {
          categories: component.categories
        }
      }
    ]);
  }, [component.categories]);

  
  
  const [series, setSeries] = useState([]);
    useEffect(()=>{
    setSeries([
        {
          name: component.name,
          data: component.data
        }
      ])
    },[component.name, component.data]);
    
    console.log('series', series)

  return (
    <div className="app">
      <div className="row">
        <div className="mixed-chart">
          <Chart options={options} series={series} type={component.type} width={component.width} />
        </div>
      </div>
    </div>
  );
}

export default App;

Console is showing me some items in series, on where previously wasn´t displaying anything, but I am receiving the following error

act_devtools_backend.js:2655 TypeError: Cannot read properties of undefined (reading ‘type’)
at t.value (index.js:2:80681)
at t.value (index.js:2:88828)
at new t (index.js:2:475557)
at l.componentDidMount (index.js:2:505466)
at Yi (sdk.js:14:916453)
at dk (sdk.js:14:933552)
at q.unstable_runWithPriority (sdk.js:14:967907)
at gg (sdk.js:14:874998)
at Uj (sdk.js:14:930132)
at Lj (sdk.js:14:926151)
overrideMethod @ react_devtools_backend.js:2655
Ni @ sdk.js:14
Pi.Z.callback @ sdk.js:14
Eg @ sdk.js:14
Yi @ sdk.js:14
dk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Uj @ sdk.js:14
Lj @ sdk.js:14
(anonymous) @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
jg @ sdk.js:14
ig @ sdk.js:14
Jg @ sdk.js:14
Oh @ sdk.js:14
(anonymous) @ sdk.js:14
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
Promise.then (async)
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
fk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Oj @ sdk.js:14
(anonymous) @ sdk.js:14
V @ sdk.js:14
_e.port1.onmessage @ sdk.js:14
index.js:2 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘type’)
at t.value (index.js:2:80681)
at t.value (index.js:2:88828)
at new t (index.js:2:475557)
at l.componentDidMount (index.js:2:505466)
at Yi (sdk.js:14:916453)
at dk (sdk.js:14:933552)
at q.unstable_runWithPriority (sdk.js:14:967907)
at gg (sdk.js:14:874998)
at Uj (sdk.js:14:930132)
at Lj (sdk.js:14:926151)
value @ index.js:2
value @ index.js:2
t @ index.js:2
componentDidMount @ index.js:2
Yi @ sdk.js:14
dk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Uj @ sdk.js:14
Lj @ sdk.js:14
(anonymous) @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
jg @ sdk.js:14
ig @ sdk.js:14
Jg @ sdk.js:14
Oh @ sdk.js:14
(anonymous) @ sdk.js:14
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
Promise.then (async)
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
fk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Oj @ sdk.js:14
(anonymous) @ sdk.js:14
V @ sdk.js:14
_e.port1.onmessage @ sdk.js:14

Console is displaying the following for “series”

series length: 0[[Prototype]]: Array(0)at: ƒ at()concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()findLast: ƒ findLast()findLastIndex: ƒ findLastIndex()flat: ƒ flat()flatMap: ƒ flatMap()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()length: 0name: "pop"arguments: (…)caller: (…)[[Prototype]]: ƒ ()[[Scopes]]: Scopes[0]push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toReversed: ƒ toReversed()toSorted: ƒ toSorted()toSpliced: ƒ toSpliced()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()with: ƒ with()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {at: true, copyWithin: true, entries: true, fill: true, find: true, …}[[Prototype]]: Object
index.js:2 series length: 0[[Prototype]]: Array(0)at: ƒ at()concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()findLast: ƒ findLast()findLastIndex: ƒ findLastIndex()flat: ƒ flat()flatMap: ƒ flatMap()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toReversed: ƒ toReversed()toSorted: ƒ toSorted()toSpliced: ƒ toSpliced()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()with: ƒ with()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {at: true, copyWithin: true, entries: true, fill: true, find: true, …}[[Prototype]]: Object
index.js:2 series

should options be an array?

also try to print options as well before passing it into the library

For options I am trying to customize just the categories for now, because here is where Apexcharts takes all the functionalities of the chart and there are many items to be considered to put it in a single array. That´s why I was only trying to intervine the “categories” section (this is a JSON type of value in the builder)

Ok, so error in console is the same

3react_devtools_backend.js:2655 TypeError: Cannot read properties of undefined (reading ‘type’)
at t.value (index.js:2:80681)
at t.value (index.js:2:88828)
at new t (index.js:2:475557)
at l.componentDidMount (index.js:2:505466)
at Yi (sdk.js:14:916453)
at dk (sdk.js:14:933552)
at q.unstable_runWithPriority (sdk.js:14:967907)
at gg (sdk.js:14:874998)
at Uj (sdk.js:14:930132)
at Lj (sdk.js:14:926151)
overrideMethod @ react_devtools_backend.js:2655
Ni @ sdk.js:14
Pi.Z.callback @ sdk.js:14
Eg @ sdk.js:14
Yi @ sdk.js:14
dk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Uj @ sdk.js:14
Lj @ sdk.js:14
(anonymous) @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
jg @ sdk.js:14
ig @ sdk.js:14
Jg @ sdk.js:14
Oh @ sdk.js:14
(anonymous) @ sdk.js:14
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
Promise.then (async)
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
fk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Oj @ sdk.js:14
(anonymous) @ sdk.js:14
V @ sdk.js:14
_e.port1.onmessage @ sdk.js:14
index.js:2 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘type’)
at t.value (index.js:2:80681)
at t.value (index.js:2:88828)
at new t (index.js:2:475557)
at l.componentDidMount (index.js:2:505466)
at Yi (sdk.js:14:916453)
at dk (sdk.js:14:933552)
at q.unstable_runWithPriority (sdk.js:14:967907)
at gg (sdk.js:14:874998)
at Uj (sdk.js:14:930132)
at Lj (sdk.js:14:926151)
value @ index.js:2
value @ index.js:2
t @ index.js:2
componentDidMount @ index.js:2
Yi @ sdk.js:14
dk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Uj @ sdk.js:14
Lj @ sdk.js:14
(anonymous) @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
jg @ sdk.js:14
ig @ sdk.js:14
Jg @ sdk.js:14
Oh @ sdk.js:14
(anonymous) @ sdk.js:14
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
Promise.then (async)
asyncGeneratorStep @ sdk.js:14
_next @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
(anonymous) @ sdk.js:14
fk @ sdk.js:14
q.unstable_runWithPriority @ sdk.js:14
gg @ sdk.js:14
Oj @ sdk.js:14
(anonymous) @ sdk.js:14
V @ sdk.js:14
_e.port1.onmessage @ sdk.js:14

what do both console.log show you, are there valid (acceptable) data for the lib?

The console log is displaying me this:
options length: 0[[Prototype]]: Array(0)
index.js:2 series length: 0[[Prototype]]: Array(0)
index.js:2 data length: 0[[Prototype]]: Array(0)at: ƒ at()concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()findLast: ƒ findLast()findLastIndex: ƒ findLastIndex()flat: ƒ flat()flatMap: ƒ flatMap()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toReversed: ƒ toReversed()toSorted: ƒ toSorted()toSpliced: ƒ toSpliced()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()with: ƒ with()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {at: true, copyWithin: true, entries: true, fill: true, find: true, …}[[Prototype]]: Object
index.js:2 options
index.js:2 series
index.js:2 data
index.js:2 options
index.js:2 series
index.js:2 data length: 0[[Prototype]]: Array(0)

I am sorry but it’s tough to understand what’s going on here in the log above

we can help you with our custom component but we can provide you assistance with issues not related to our product

I can recommend getting the chart working with static data and then finishing the component by getting data from codeless/settings

try the following one:

import React, { useState } from "react";
import Chart from "./lib/react-apexcharts/node_modules/react-apexcharts/src/react-apexcharts.jsx";

const options={
    chart: {
      id: "basic-bar"
    },
    xaxis: {
      categories: [1,2,3,4]
    }
  }

const series =[
{
  name:'serie-name',
      data: [12,32,44,553,32]
}
]

export default function ChatComponent({ component }) {
  
  return (
    <div className="app">
      <div className="row">
        <div className="mixed-chart">
          <Chart options={options} series={series} type="line" width="400px" />
        </div>
      </div>
    </div>
  );
}