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

PivotGrid - Review react to functional approach #2833

Merged
merged 6 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions JSDemos/Demos/PivotGrid/ChartIntegration/React/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,54 @@ import PivotGrid, {

import sales from './data.js';

class App extends React.Component {
componentDidMount() {
this.pivotGrid.bindChart(this.chart, {
const customizeTooltip = (args) => {
const valueText = (args.seriesName.indexOf('Total') !== -1)
? new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD' }).format(args.originalValue)
: args.originalValue;

return {
html: `${args.seriesName}<div class='currency'>${
valueText}</div>`,
};
};

const App = () => {
const chartRef = React.useRef(null);
const pivotGridRef = React.useRef(null);

React.useEffect(() => {
pivotGridRef.current.instance.bindChart(chartRef.current.instance, {
dataFieldsDisplayMode: 'splitPanes',
alternateDataFields: false,
});
}
}, []);

render() {
return (
<React.Fragment>
<Chart ref={(ref) => { this.chart = ref.instance; }}>
<Size height={320} />
<Tooltip enabled={true} customizeTooltip={customizeTooltip} />
<CommonSeriesSettings type="bar" />
<AdaptiveLayout width={450} />
</Chart>
return (
<React.Fragment>
<Chart ref={chartRef}>
<Size height={320} />
<Tooltip enabled={true} customizeTooltip={customizeTooltip} />
<CommonSeriesSettings type="bar" />
<AdaptiveLayout width={450} />
</Chart>

<PivotGrid
id="pivotgrid"
dataSource={dataSource}
allowSortingBySummary={true}
allowFiltering={true}
showBorders={true}
showColumnTotals={false}
showColumnGrandTotals={false}
showRowTotals={false}
showRowGrandTotals={false}
ref={(ref) => { this.pivotGrid = ref.instance; }}
>
<FieldChooser enabled={true} height={400} />
</PivotGrid>
</React.Fragment>
);
}
}
<PivotGrid
id="pivotgrid"
dataSource={dataSource}
allowSortingBySummary={true}
allowFiltering={true}
showBorders={true}
showColumnTotals={false}
showColumnGrandTotals={false}
showRowTotals={false}
showRowGrandTotals={false}
ref={pivotGridRef}
>
<FieldChooser enabled={true} height={400} />
</PivotGrid>
</React.Fragment>
);
};

const dataSource = new PivotGridDataSource({
fields: [{
Expand Down Expand Up @@ -86,15 +98,4 @@ const dataSource = new PivotGridDataSource({
store: sales,
});

function customizeTooltip(args) {
const valueText = (args.seriesName.indexOf('Total') !== -1)
? new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD' }).format(args.originalValue)
: args.originalValue;

return {
html: `${args.seriesName}<div class='currency'>${
valueText}</div>`,
};
}

export default App;
126 changes: 49 additions & 77 deletions JSDemos/Demos/PivotGrid/DrillDown/React/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,61 @@ import PivotGridDataSource from 'devextreme/ui/pivot_grid/data_source';

import { sales } from './data.js';

class App extends React.Component {
constructor(props) {
super(props);
const App = () => {
const [popupTitle, setPopupTitle] = React.useState('');
const [drillDownDataSource, setDrillDownDataSource] = React.useState(null);
const [popupVisible, setPopupVisible] = React.useState(false);
const dataGridRef = React.useRef(null);

this.state = {
popupTitle: '',
drillDownDataSource: null,
popupVisible: false,
};
this.onCellClick = this.onCellClick.bind(this);
this.onHiding = this.onHiding.bind(this);
this.onShown = this.onShown.bind(this);
this.getDataGridInstance = this.getDataGridInstance.bind(this);
}

render() {
const { drillDownDataSource, popupTitle, popupVisible } = this.state;

return (
<React.Fragment>
<PivotGrid
id="sales"
allowSortingBySummary={true}
allowSorting={true}
allowFiltering={true}
allowExpandAll={true}
showBorders={true}
dataSource={dataSource}
onCellClick={this.onCellClick}
>
<FieldChooser enabled={false} />
</PivotGrid>
<Popup
visible={popupVisible}
width={600}
height={400}
title={popupTitle}
onHiding={this.onHiding}
onShown={this.onShown}
>
<DataGrid
width={560}
height={300}
dataSource={drillDownDataSource}
ref={this.getDataGridInstance}
>
<Column dataField="region" />
<Column dataField="city" />
<Column dataField="amount" dataType="number" format="currency" />
<Column dataField="date" dataType="date" />
</DataGrid>
</Popup>
</React.Fragment>
);
}

getDataGridInstance(ref) {
this.dataGrid = ref.instance;
}

onCellClick(e) {
const onCellClick = React.useCallback((e) => {
if (e.area === 'data') {
const pivotGridDataSource = e.component.getDataSource();
const rowPathLength = e.cell.rowPath.length;
const rowPathName = e.cell.rowPath[rowPathLength - 1];

this.setState({
popupTitle: `${rowPathName || 'Total'} Drill Down Data`,
drillDownDataSource: pivotGridDataSource.createDrillDownDataSource(e.cell),
popupVisible: true,
});
setPopupTitle(`${rowPathName || 'Total'} Drill Down Data`);
setDrillDownDataSource(pivotGridDataSource.createDrillDownDataSource(e.cell));
setPopupVisible(true);
}
}
}, []);

onHiding() {
this.setState({
popupVisible: false,
});
}

onShown() {
this.dataGrid.updateDimensions();
}
}
export default App;
return (
<React.Fragment>
<PivotGrid
id="sales"
allowSortingBySummary={true}
allowSorting={true}
allowFiltering={true}
allowExpandAll={true}
showBorders={true}
dataSource={dataSource}
onCellClick={onCellClick}
>
<FieldChooser enabled={false} />
</PivotGrid>
<Popup
visible={popupVisible}
width={600}
height={400}
title={popupTitle}
onHiding={() => setPopupVisible(false)}
onShown={() => dataGridRef.current.updateDimensions()}
>
<DataGrid
width={560}
height={300}
dataSource={drillDownDataSource}
ref={dataGridRef}
>
<Column dataField="region" />
<Column dataField="city" />
<Column dataField="amount" dataType="number" format="currency" />
<Column dataField="date" dataType="date" />
</DataGrid>
</Popup>
</React.Fragment>
);
};

const dataSource = new PivotGridDataSource({
fields: [{
Expand All @@ -120,3 +90,5 @@ const dataSource = new PivotGridDataSource({
}],
store: sales,
});

export default App;
Loading
Loading