Skip to content

Commit

Permalink
feat: allow initialization from a DataTable (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grsmto authored Oct 26, 2024
1 parent ce40b81 commit 01e21d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export class GoogleChartInternal {
);
}
if (data) {
if (Array.isArray(data)) {
if (data instanceof google.visualization.DataTable) {
dataTable = data;
} else if (Array.isArray(data)) {
dataTable = google.visualization.arrayToDataTable(data);
} else {
dataTable = new google.visualization.DataTable(data);
Expand Down
33 changes: 33 additions & 0 deletions packages/react-google-charts/stories/DataTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import { Chart, GoogleDataTable, GoogleViz } from "../src";
import * as barChartData from "../../../sandboxes/bar-chart/default/App";

export default {
title: "DataTable",
component: Chart,
parameters: {
layout: "centered",
},
args: {
chartType: "BarChart",
width: 800,
height: 600,
},
};

export function Default({ data, chartType, ...rest }) {
const [dataTable, setDataTable] = React.useState<GoogleDataTable | null>(null);

const handleGoogleChartLoaded = (google: GoogleViz) => {
const dataTable = google.visualization.arrayToDataTable(data);

setDataTable(dataTable);
};

return <Chart onLoad={handleGoogleChartLoaded} data={dataTable ?? []} chartType={chartType} {...rest} />;
}

Default.args = {
data: barChartData.data,
options: barChartData.options,
};

0 comments on commit 01e21d4

Please sign in to comment.