0

I'm creating an interactive viz with React and I would like to add a slider in my viz.

import "./styles.css";
import React from "react";
import Sunburst from "react-zoomable-sunburst";
import { data } from "./data";
import { Slider } from "@mui/material";

class App extends React.Component {
  onSelect(event) {
    console.log(event);
  }
  render() {
    return (
      <div className="App">
        <Sunburst
          width="400"
          height="400"
          data={data.a}
          count_member="size"
          labelFunc={(node) => node.data.name}
          _debug={false}
        />
        <Slider
          aria-label="Year"
          defaultValue={2016}
          valueLabelDisplay="auto"
          step={1}
          marks
          min={2016}
          max={2020}
        />
      </div>
    );
  }
}

export default App;

Above is my code. I added my data from 2016 to 2020 in data.js file and I want my viz to change according to the year as I move my slider.

  a: {
    name: "2020",
    children: [
      {
        name: "Campus",
        children: [
          {
            name: "Liabilities",
            children: [
              {
                name: "Current Liabilities",
                children: [
                  { name: "Accounts payable", size: 53010 },
                  { name: "Accrued salaries", size: 23554 },
                  { name: "Unearned revenue", size: 253322 },
                  { name: "Commercial paper", size: 326008 },
                  { name: "Current portion of long-term debt", size: 112431 },
                  { name: "Funds held for others", size: 2500 },
                  { name: "Other current liabilities", size: 71036 }
                ]
              }

This is a part of my code in data.js and I named 2016 data as a, 2017 as b, and so on. I wanted to name as 2016, 2017... instead of a,b... but this didn't work since data.2016 didn't work. (So I made it data.a in the code above) So currently, my slider doesn't work. Is there any way to make dictionary or other method in React to resolve this problem?

Thanks.

1
  • instead of using data.2016, how about data["2016"]? Meanwhile, I suggest using data.json instead of data.js, inside data.json, you could formally use a dictionary object for like {"2016": {...}} Commented Feb 23, 2022 at 1:47

1 Answer 1

1

use

data['2016']

That should work.

Sign up to request clarification or add additional context in comments.

1 Comment

I did data['2016'] and it comes out with this error. /src/App.js: JSX value should be either an expression or a quoted JSX text (17:15)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.