Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chart.load()'s json structure/type mismatches chart.generate (constructor) #1848

Closed
tony opened this issue Jan 2, 2021 · 1 comment
Closed
Labels

Comments

@tony
Copy link

tony commented Jan 2, 2021

The data structure for json is incompatible between the constructor and load()

Description

The types for data.json can cause issues when chart.load is invoked on subsequent react prop updates

ERROR in /home/t/work/cv/packages/react/src/App.tsx
./src/App.tsx
[tsl] ERROR in /home/t/work/cv/packages/react/src/App.tsx(126,18)
      TS2345: Argument of type 'Data' is not assignable to parameter of type '{ url?: string | undefined; json?: [{ [key: string]: string; }] | undefined; rows?: PrimitiveArray[] | undefined; columns?: PrimitiveArray[] | undefined; data?: DataArray | undefined; ... 12 more ...; done?: (() => any) | undefined; }'.
  Types of property 'json' are incompatible.
    Type '{} | undefined' is not assignable to type '[{ [key: string]: string; }] | undefined'.
      Type '{}' is not assignable to type '[{ [key: string]: string; }]'.

chart.d.ts

export interface Chart {
  load(this: Chart, args: {
    url?: string;
    json?: [{ [key: string]: string }];
    rows?: PrimitiveArray[];
    columns?: PrimitiveArray[];
    data?: DataArray;
    names?: { [key: string]: string };
    xs?: { [key: string]: string };
    classes?: { [key: string]: string };
    categories?: string[];                                                                                                                                                                                                                       axes?: { [key: string]: string };
    colors?: { [key: string]: string };
    headers?: { [key: string]: string };                                                                                                                                                                                                         keys?: { [key: string]: string };
    mimeType?: string;
    type?: string;
    types?: { [key: string]: string };
    unload?: boolean | ArrayOrString;
    done?: () => any;
  }): void;
}
export interface ChartOptions {
  /**
   * Parse a JSON object for data.
   */
  json?: {};
}
export const Chart: React.FC<bb.ChartOptions> = ({ data, ...props }) => {
  //| Parameters<bb.Chart["load"]>
  const ref = React.useRef<HTMLDivElement>(null);
  const [chart, setChart] = React.useState<bb.Chart | null>(null);
  React.useEffect(() => {
    if (!chart) {
      const newChart = bb.generate({ bindto: ref.current, data, ...props });
      setChart(newChart);
      return () => newChart.unload();
    }
  }, [props, data]);

  React.useEffect(() => {
    if (!chart || !data) {
      return;
    }
    if (chart && chart?.load && data) {
      chart.load(data);
    }
  }, [data]);

  React.useLayoutEffect(() => {
    if (chart?.resize) {
      chart.resize();
    }
  });

  return <div ref={ref} />;
};

Steps to check or reproduce

@netil
Copy link
Member

netil commented Jan 8, 2021

Note that data object interface within .load({data: ... }), isn't same as the .generate({ data: ... }).

chart.load({
    data: [
      // equivalent as: columns: [["data1", 30, 200, 100]]
      {"data1": 30}, {"data1": 200}, {"data1": 100}

      // or
      // equivalent as: columns: [["data1", 10, 20], ["data2", 13, 30]]
      // {"data1": 10, "data2": 13}, {"data1": 20, "data2": 30}}
    ]
});

I'll be fix the type definition to reflect above interface.

netil added a commit to netil/billboard.js that referenced this issue Jan 8, 2021
Fix wrong data type definition.

Ref naver#1848
@netil netil closed this as completed in 7108e7b Jan 8, 2021
github-actions bot pushed a commit that referenced this issue Jan 8, 2021
# [2.2.0-next.4](2.2.0-next.3...2.2.0-next.4) (2021-01-08)

### Bug Fixes

* **color:** fix color callback mismatch ([86ef214](86ef214)), closes [#1847](#1847)
* **types:** fix .load() data type ([7108e7b](7108e7b)), closes [#1848](#1848)

### Features

* **gauge:** added support for drawing gauge from 'startingAngle' to 'arcLength' ([25954ad](25954ad)), closes [#1633](#1633) [#1803](#1803) [#1849](#1849)
github-actions bot pushed a commit that referenced this issue Jan 26, 2021
# [2.2.0](2.1.4...2.2.0) (2021-01-26)

### Bug Fixes

* **api:** fix incorrect legend text positioning ([f1ede3f](f1ede3f)), closes [#1888](#1888)
* **api:** fix zoom for timesries axis ([0421a50](0421a50)), closes [#1868](#1868)
* **arc:** fix setting color value ([fad9e86](fad9e86)), closes [#1857](#1857) [#1847](#1847)
* **axis:** fix incorrect tick interval calculation ([1a96f3e](1a96f3e)), closes [#1896](#1896)
* **axis:** rotated horizontal xAxisHeight is calculated correctly after loading new data ([ef2754f](ef2754f)), closes [#1786](#1786) [#1787](#1787)
* **bar:** fix bar width on zoom ([25e987a](25e987a)), closes [#1907](#1907)
* **bar:** fix bar width rendering for 'total' data key ([eacaecb](eacaecb)), closes [#1818](#1818)
* **browser:** fix referencing global order ([8f84cb3](8f84cb3)), closes [#1778](#1778)
* **browser:** fix retrieving global ([3474ac0](3474ac0)), closes [#1826](#1826)
* **color:** fix color callback mismatch ([86ef214](86ef214)), closes [#1847](#1847)
* **data:** fix data.order to work for arc types ([3a716a0](3a716a0)), closes [#1863](#1863)
* **eventrect:** fix resizing event rect element after .load() ([12bf547](12bf547)), closes [#1864](#1864)
* **eventRect:** fix data.onclick work for scatter/bubble ([109c87d](109c87d)), closes [#1795](#1795)
* **point:** fix usePoint defs id value ([6df4653](6df4653)), closes [#1887](#1887)
* **radar:** fix data label text position ([c69d674](c69d674)), closes [#1871](#1871)
* **text:** data labels are working in Internet Explorer again ([865224e](865224e)), closes [#1877](#1877)
* **tooltip:** fix arc's tooltip interaction ([a8586a3](a8586a3)), closes [#1859](#1859)
* **types:** fix .load() data type ([7108e7b](7108e7b)), closes [#1848](#1848)

### Features

* **api:** make return values for axis.labels() ([efa5174](efa5174)), closes [#1865](#1865)
* **data:** Intent to ship data.label multiline ([8903aa6](8903aa6)), closes [#1784](#1784)
* **data.labels.colors:** Add callback support ([ac1affa](ac1affa)), closes [#1845](#1845)
* **gauge:** added support for drawing gauge from 'startingAngle' to 'arcLength' ([25954ad](25954ad)), closes [#1633](#1633) [#1803](#1803) [#1849](#1849)
* **gauge:** Intent to ship gauge.background ([632c600](632c600)), closes [#1804](#1804)
* **pie:** Intent to ship pie.outerRadius ([ca67418](ca67418)), closes [#1825](#1825)
* **point:** Intent to ship point.opacity ([fc5ad35](fc5ad35)), closes [#1867](#1867)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants