Skip to content

Commit cca2bc2

Browse files
authored
Merge pull request #118 from Caltech-IPAC/dm-6287_refactor
DM-6287 Charts refactoring
2 parents c4ea90f + 992036f commit cca2bc2

25 files changed

+730
-651
lines changed

src/firefly/js/api/ApiBuild.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {isString} from 'lodash';
66
import {dispatchOnAppReady} from '../core/AppDataCntlr.js';
77

88
// Used for dispatch and action type constants
9-
import * as TableStatsCntlr from '../visualize/TableStatsCntlr.js';
10-
import * as HistogramCntlr from '../visualize/HistogramCntlr.js';
11-
import * as XYPlotCntlr from '../visualize/XYPlotCntlr.js';
9+
import * as TableStatsCntlr from '../charts/TableStatsCntlr.js';
10+
import * as HistogramCntlr from '../charts/HistogramCntlr.js';
11+
import * as XYPlotCntlr from '../charts/XYPlotCntlr.js';
1212
import * as TablesCntlr from '../tables/TablesCntlr.js';
1313
import * as ReadoutCntlr from '../visualize/MouseReadoutCntlr.js';
1414
import * as ImPlotCntlr from '../visualize/ImagePlotCntlr.js';
@@ -33,8 +33,8 @@ import {ExpandedModeDisplay} from '../visualize/iv/ExpandedModeDisplay.jsx';
3333
import {ApiExpandedDisplay} from '../visualize/ui/ApiExpandedDisplay.jsx';
3434
import {TablesContainer} from '../tables/ui/TablesContainer.jsx';
3535
import {TablePanel} from '../tables/ui/TablePanel.jsx';
36-
import {ChartsContainer} from '../visualize/ChartsContainer.jsx';
37-
import {ChartsTableViewPanel} from '../visualize/ChartsTableViewPanel.jsx';
36+
import {ChartsContainer} from '../charts/ui/ChartsContainer.jsx';
37+
import {ChartsTableViewPanel} from '../charts/ui/ChartsTableViewPanel.jsx';
3838
import {PopupMouseReadoutMinimal} from '../visualize/ui/PopupMouseReadoutMinimal.jsx';
3939
import {PopupMouseReadoutFull} from '../visualize/ui/PopupMouseReadoutFull.jsx';
4040

src/firefly/js/api/ApiExpandedView.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import sCompare from 'react-addons-shallow-compare';
77

88
import {flux} from '../Firefly.js';
99
import {TablesContainer} from '../tables/ui/TablesContainer.jsx';
10-
import {ChartsContainer} from '../visualize/ChartsContainer.jsx';
10+
import {ChartsContainer} from '../charts/ui/ChartsContainer.jsx';
1111
import {ApiExpandedDisplay} from '../visualize/ui/ApiExpandedDisplay.jsx';
1212
import {dispatchChangeExpandedMode, ExpandType} from '../visualize/ImagePlotCntlr.js';
1313
import {dispatchSetLayoutMode, getExpandedMode, LO_MODE, LO_VIEW} from '../core/LayoutCntlr.js';

src/firefly/js/api/ApiUtilChart.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
33
*/
44

5-
import * as XYPlotCntlr from '../visualize/XYPlotCntlr.js';
5+
import * as XYPlotCntlr from '../charts/XYPlotCntlr.js';
66
//import * as HistogramCntlr from '../visualize/HistogramCntlr.js';
77
//import * as TableStatsCntlr from '../visualize/TableStatsCntlr.js';
88

9-
export {uniqueChartId} from '../visualize/ChartUtil.js';
9+
export {uniqueChartId} from '../charts/ChartUtil.js';
1010

1111
export function loadPlotDataForTbl(tblId, chartId, xyPlotParams) {
1212
XYPlotCntlr.dispatchLoadPlotData(chartId, xyPlotParams, tblId);

src/firefly/js/visualize/ChartUtil.js renamed to src/firefly/js/charts/ChartUtil.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {MetaConst} from '../data/MetaConst.js';
1515
import {getTblById, getColumnIdx, getCellValue} from '../tables/TableUtil.js';
1616
import {Expression} from '../util/expr/Expression.js';
1717
import {logError} from '../util/WebUtil.js';
18-
import {XYPLOT_DATA_KEY} from '../visualize/XYPlotCntlr.js';
19-
import {HISTOGRAM_DATA_KEY} from '../visualize/HistogramCntlr';
18+
import {XYPLOT_DATA_KEY} from './XYPlotCntlr.js';
19+
import {HISTOGRAM_DATA_KEY} from './HistogramCntlr.js';
2020

2121
export const SCATTER = 'scatter';
2222
export const HISTOGRAM = 'histogram';
@@ -69,9 +69,9 @@ function getExpressionValue(strExpr, tableModel, rowIdx) {
6969
export function getChartSpace(chartType) {
7070
switch(chartType) {
7171
case SCATTER:
72-
return flux.getState()[XYPLOT_DATA_KEY];
72+
return get(flux.getState(), XYPLOT_DATA_KEY);
7373
case HISTOGRAM:
74-
return flux.getState()[HISTOGRAM_DATA_KEY];
74+
return get(flux.getState(), HISTOGRAM_DATA_KEY);
7575
default:
7676
logError(`Unknown chart type ${chartType}`);
7777
return undefined;
@@ -97,9 +97,9 @@ export function getTblIdForChartId(chartId) {
9797
export function numRelatedCharts(tblId) {
9898
let numRelated = 0;
9999
let c;
100-
const keys = [XYPLOT_DATA_KEY, HISTOGRAM_DATA_KEY];
100+
const keys = [SCATTER, HISTOGRAM];
101101
keys.forEach( (key) => {
102-
const space = flux.getState()[key];
102+
const space = getChartSpace(key);
103103
for (c in space) {
104104
if (space.hasOwnProperty(c) && space[c].tblId === tblId) {
105105
numRelated++;

src/firefly/js/visualize/ChartsCntlr.js renamed to src/firefly/js/charts/ChartsCntlr.js

Lines changed: 97 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
*/
44

55
import {has, get, isUndefined, omit} from 'lodash';
6+
import shallowequal from 'shallowequal';
67

78
import {flux} from '../Firefly.js';
8-
import {updateSet, updateMerge} from '../util/WebUtil.js';
9+
import {updateDelete, updateSet, updateMerge} from '../util/WebUtil.js';
910

1011
import * as TablesCntlr from '../tables/TablesCntlr.js';
12+
import {reduceXYPlot} from './XYPlotCntlr.js';
13+
import {reduceHistogram} from './HistogramCntlr.js';
1114

1215
export const CHART_SPACE_PATH = 'charts';
1316
export const UI_PREFIX = `${CHART_SPACE_PATH}.ui`;
@@ -62,57 +65,125 @@ export function dispatchDelete(tblId, chartId, chartType, dispatcher= flux.proce
6265
const uniqueId = (chartId, chartType) => { return `${chartType}|${chartId}`; };
6366
const isChartType = (uChartId, chartType) => { return uChartId.startsWith(chartType); };
6467

68+
69+
/*
70+
Possible structure of store:
71+
/ui
72+
expanded: Object - the information about expanded chart
73+
{
74+
chartId: string
75+
tblId: string
76+
chartType: string, ex. 'scatter', 'histogram'
77+
}
78+
/tbl
79+
tblId: Object - the name of this node matches table id
80+
{
81+
uChartId: Object - the name of this node matches 'chartType|chartId'
82+
{
83+
mounted: boolean,
84+
n: number, undefined means 1
85+
}
86+
}
87+
/xyplot - parameters and data for scatter plot, see XYPlotCntlr
88+
/histogram - parameters and data for histogram plot, see HistogramCntlr
89+
*/
90+
91+
export function reducer(state={ui:{}, tbl: {}, xyplot:{}, histogram:{}}, action={}) {
92+
93+
if (!action.type.startsWith(TablesCntlr.DATA_PREFIX) && !action.type.startsWith(CHART_SPACE_PATH)){
94+
return state;
95+
}
96+
97+
const nstate = {...state};
98+
99+
nstate.xyplot = reduceXYPlot(state['xyplot'], action);
100+
nstate.histogram = reduceHistogram(state['histogram'], action);
101+
102+
// generic for all chart types
103+
nstate.ui = reduceUI(state['ui'], action);
104+
// organized by table id
105+
nstate.tbl = reduceByTbl(state['tbl'], action);
106+
107+
if (shallowequal(state, nstate)) {
108+
return state;
109+
} else {
110+
return nstate;
111+
}
112+
}
113+
65114
const chartActions = [CHART_UI_EXPANDED,CHART_MOUNTED,CHART_UNMOUNTED,DELETE];
66115

67-
export function reducer(state={ui:{}}, action={}) {
116+
/**
117+
* @param state - ui part of chart state
118+
* @param action - action
119+
* @returns {*} - updated ui part of the state
120+
*/
121+
function reduceUI(state={}, action={}) {
122+
if (chartActions.indexOf(action.type) > -1) {
123+
const {chartId, tblId, chartType} = action.payload;
124+
switch (action.type) {
125+
case (CHART_UI_EXPANDED) :
126+
return updateSet(state, 'expanded', {chartId, tblId, chartType});
127+
default:
128+
return state;
129+
}
130+
} else {
131+
return state;
132+
}
133+
134+
}
135+
136+
137+
/**
138+
*
139+
* @param state - by table part of chart state, stores info which charts are mounted
140+
* @param action - action
141+
* @returns {*} - new by table part of chart state
142+
*/
143+
function reduceByTbl(state={}, action={}) {
68144
if (action.type === TablesCntlr.TABLE_REMOVE) {
69145
const {tbl_id} = action.payload;
70146
if (has(state, tbl_id)) {
71-
return Object.assign({}, omit(state, [tbl_id]));
147+
return omit(state, [tbl_id]);
72148
}
73149
return state;
74150
} else if (chartActions.indexOf(action.type) > -1) {
75151
const {chartId, tblId, chartType} = action.payload;
76152
const uChartId = uniqueId(chartId, chartType);
77153
switch (action.type) {
78-
79-
case (CHART_UI_EXPANDED) :
80-
//return updateSet(root, 'expanded', {chartId, tblId});
81-
return updateSet(state, 'ui.expanded', {chartId, tblId, chartType});
82154
case (CHART_MOUNTED) :
83-
84-
if (!has(state, ['tbl', tblId])) {
85-
return updateSet(state, ['tbl',tblId], {[uChartId]: {mounted: true}});
155+
if (!has(state, tblId)) {
156+
return updateSet(state, tblId, {[uChartId]: {mounted: true}});
86157
} else {
87-
if (get(state, ['tbl', tblId, uChartId, 'mounted'])) {
158+
if (get(state, [tblId, uChartId, 'mounted'])) {
88159
//other version of the same chart is mounted
89-
let n = get(state, ['tbl', tblId, uChartId, 'n']);
160+
let n = get(state, [tblId, uChartId, 'n']);
90161
n = n ? Number(n) : 1;
91-
return updateMerge(state, ['tbl', tblId, uChartId], {upToDate: true, n: n + 1});
162+
return updateMerge(state, [tblId, uChartId], {upToDate: true, n: n + 1});
92163
} else {
93-
return updateSet(state, ['tbl', tblId, uChartId], {mounted: true});
164+
return updateSet(state, [tblId, uChartId], {mounted: true});
94165
}
95166
}
96167
case (CHART_UNMOUNTED) :
97-
if (has(state, ['tbl', tblId])) {
98-
if (get(state, ['tbl', tblId, uChartId, 'mounted'])) {
99-
let n = get(state, ['tbl', tblId, uChartId, 'n']);
168+
if (has(state, [tblId])) {
169+
if (get(state, [tblId, uChartId, 'mounted'])) {
170+
let n = get(state, [tblId, uChartId, 'n']);
100171
n = n ? Number(n) : 1;
101172
if (n > 1) {
102173
//multiple versions of the same chart are mounted
103-
return updateMerge(state, ['tbl', tblId, uChartId], {n: n - 1});
174+
return updateMerge(state, [tblId, uChartId], {n: n - 1});
175+
} else {
176+
return updateSet(state, [tblId, uChartId], {mounted: false});
104177
}
105-
} else {
106-
return updateSet(state, ['tbl', tblId, uChartId], {mounted: false});
107178
}
108179
}
109180
return state;
110181
case (DELETE) :
111-
if (has(state, ['tbl', tblId, uChartId])) {
112-
if (Object.keys(state['tbl'][tblId]).length > 1) {
113-
return updateSet(state, ['tbl', tblId], omit(state['tbl'][tblId], [uChartId]));
182+
if (has(state, [tblId, uChartId])) {
183+
if (Object.keys(state[tblId]).length > 1) {
184+
return updateDelete(state, tblId, uChartId);
114185
} else {
115-
return updateSet(state, 'tbl', omit(state['tbl'], [tblId]));
186+
return omit(state, [tblId]);
116187
}
117188
}
118189
return state;
@@ -122,6 +193,7 @@ export function reducer(state={ui:{}}, action={}) {
122193
} else {
123194
return state;
124195
}
196+
125197
}
126198

127199

@@ -154,4 +226,4 @@ export function getNumRelatedCharts(tblId, mounted, chartType) {
154226
export function isChartMounted(tblId, chartId, chartType) {
155227
const uChartId = uniqueId(chartId, chartType);
156228
return Boolean(get(flux.getState(), [CHART_SPACE_PATH, 'tbl', tblId, uChartId, 'mounted']));
157-
}
229+
}

src/firefly/js/visualize/ColValuesStatistics.js renamed to src/firefly/js/charts/ColValuesStatistics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default class ColValuesStatistics{
88
this.numpoints = Number(numpoints);
99
this.type = type;
1010
}
11-
};
11+
};

src/firefly/js/visualize/HistogramCntlr.js renamed to src/firefly/js/charts/HistogramCntlr.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {get, has, omit} from 'lodash';
77

88
import {updateSet, updateMerge} from '../util/WebUtil.js';
99
import {doFetchTable, getTblById, isFullyLoaded, makeTblRequest} from '../tables/TableUtil.js';
10+
import {HISTOGRAM, getChartSpace} from './ChartUtil.js';
1011
import * as TablesCntlr from '../tables/TablesCntlr.js';
1112
import {DELETE} from './ChartsCntlr.js';
1213

@@ -34,7 +35,7 @@ import {DELETE} from './ChartsCntlr.js';
3435
*/
3536

3637

37-
export const HISTOGRAM_DATA_KEY = 'histogram';
38+
export const HISTOGRAM_DATA_KEY = 'charts.histogram';
3839
export const LOAD_COL_DATA = `${HISTOGRAM_DATA_KEY}/LOAD_COL_DATA`;
3940
export const UPDATE_COL_DATA = `${HISTOGRAM_DATA_KEY}/UPDATE_COL_DATA`;
4041

@@ -70,7 +71,7 @@ export const loadColData = function(rawAction) {
7071
const {chartId, histogramParams, tblId} = rawAction.payload;
7172
const tblSource = get(getTblById(tblId), 'tableMeta.source');
7273

73-
const chartModel = get(flux.getState(), [HISTOGRAM_DATA_KEY, chartId]);
74+
const chartModel = get(getChartSpace(HISTOGRAM), chartId);
7475
let serverCallNeeded = !chartModel || !chartModel.tblSource || chartModel.tblSource !== tblSource;
7576

7677
if (serverCallNeeded || chartModel.histogramParams !== histogramParams) {
@@ -110,7 +111,7 @@ function getServerCallParameters(histogramParams) {
110111
return serverParams;
111112
}
112113

113-
export function reducer(state={}, action={}) {
114+
export function reduceHistogram(state={}, action={}) {
114115
switch (action.type) {
115116
case (TablesCntlr.TABLE_REMOVE) :
116117
{
@@ -122,13 +123,13 @@ export function reducer(state={}, action={}) {
122123
}
123124
});
124125
return (chartsToDelete.length > 0) ?
125-
Object.assign({}, omit(state, chartsToDelete)) : state;
126+
omit(state, chartsToDelete) : state;
126127
}
127128
case (DELETE) :
128129
{
129130
const {chartId, chartType} = action.payload;
130131
if (chartType === 'histogram' && has(state, chartId)) {
131-
return Object.assign({}, omit(state, [chartId]));
132+
return omit(state, [chartId]);
132133
}
133134
return state;
134135
}
@@ -248,4 +249,4 @@ function fetchColData(dispatch, tblId, histogramParams, chartId) {
248249
console.error(`Failed to fetch histogram data: ${reason}`);
249250
}
250251
);
251-
}
252+
}

src/firefly/js/visualize/TableStatsCntlr.js renamed to src/firefly/js/charts/TableStatsCntlr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,4 @@ function fetchTblStats(dispatch, activeTableServerRequest) {
145145
console.error(`Failed to fetch table statistics: ${reason}`);
146146
}
147147
);
148-
}
148+
}

0 commit comments

Comments
 (0)