Skip to content

Commit

Permalink
fix: switch to using a dataView to hide columns
Browse files Browse the repository at this point in the history
PR: #391
  • Loading branch information
davewelsh authored and rakannimer committed Sep 22, 2024
1 parent 67b536c commit 2d91574
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/components/GoogleChartDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,30 @@ export class GoogleChartDataTableInner extends React.Component<
dataTable = google.visualization.arrayToDataTable([]);
}
const columnCount = dataTable.getNumberOfColumns();
for (let i = 0; i < columnCount; i += 1) {
const columnID = this.getColumnID(dataTable, i);
if (this.state.hiddenColumns.includes(columnID)) {
const previousColumnLabel = dataTable.getColumnLabel(i);
const previousColumnID = dataTable.getColumnId(i);
const previousColumnType = dataTable.getColumnType(i);
dataTable.removeColumn(i);
dataTable.addColumn({
label: previousColumnLabel,
id: previousColumnID,
type: previousColumnType,
});
}
}

const viewColumns = Array(columnCount)
.fill(0)
.map((c, i) => {
const columnID = this.getColumnID(dataTable, i);
if (this.state.hiddenColumns.includes(columnID)) {
return {
label: dataTable.getColumnLabel(i),
type: dataTable.getColumnType(i),
calc: () => null,
};
} else {
return i;
}
});
const chart = googleChartWrapper.getChart();
if (googleChartWrapper.getChartType() === "Timeline") {
chart && chart.clearChart();
}
googleChartWrapper.setChartType(chartType);
googleChartWrapper.setOptions(options || {});
googleChartWrapper.setDataTable(dataTable);
const viewTable = new google.visualization.DataView(dataTable);
viewTable.setColumns(viewColumns);
googleChartWrapper.setDataTable(viewTable);
googleChartWrapper.draw();
if (this.props.googleChartDashboard !== null) {
this.props.googleChartDashboard.draw(dataTable);
Expand Down
27 changes: 27 additions & 0 deletions stories/LegendToggle.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { Chart } from "../src";
import * as lineChartData from "../sandboxes/line-chart/default/App";

export default {
title: "Legend Toggle",
component: Chart,
parameters: {
layout: "centered",
},
args: {
chartType: "LineChart",
width: 800,
height: 600,
data: lineChartData.data,
legend_toggle: true,
},
};

export function Default(args) {
return <Chart {...args} />;
}

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

0 comments on commit 2d91574

Please sign in to comment.