Skip to content

Commit 01e21d4

Browse files
authored
feat: allow initialization from a DataTable (#766)
1 parent ce40b81 commit 01e21d4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/react-google-charts/src/utils/GoogleChartInternal.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export class GoogleChartInternal {
127127
);
128128
}
129129
if (data) {
130-
if (Array.isArray(data)) {
130+
if (data instanceof google.visualization.DataTable) {
131+
dataTable = data;
132+
} else if (Array.isArray(data)) {
131133
dataTable = google.visualization.arrayToDataTable(data);
132134
} else {
133135
dataTable = new google.visualization.DataTable(data);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from "react";
2+
import { Chart, GoogleDataTable, GoogleViz } from "../src";
3+
import * as barChartData from "../../../sandboxes/bar-chart/default/App";
4+
5+
export default {
6+
title: "DataTable",
7+
component: Chart,
8+
parameters: {
9+
layout: "centered",
10+
},
11+
args: {
12+
chartType: "BarChart",
13+
width: 800,
14+
height: 600,
15+
},
16+
};
17+
18+
export function Default({ data, chartType, ...rest }) {
19+
const [dataTable, setDataTable] = React.useState<GoogleDataTable | null>(null);
20+
21+
const handleGoogleChartLoaded = (google: GoogleViz) => {
22+
const dataTable = google.visualization.arrayToDataTable(data);
23+
24+
setDataTable(dataTable);
25+
};
26+
27+
return <Chart onLoad={handleGoogleChartLoaded} data={dataTable ?? []} chartType={chartType} {...rest} />;
28+
}
29+
30+
Default.args = {
31+
data: barChartData.data,
32+
options: barChartData.options,
33+
};

0 commit comments

Comments
 (0)