Skip to content

Commit 8baecdd

Browse files
authored
DM-7824: merge hiistogram doc and bug fixes, pull request #197 from
DM-7824: Histogram doc and bug fixes
2 parents b105dc0 + e3e18a2 commit 8baecdd

File tree

6 files changed

+106
-19
lines changed

6 files changed

+106
-19
lines changed

docs/firefly-api-overview.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ Firefly React components can be found in `firefly.ui` module.
9494
| ---------- | ---- | ----------- |
9595
| div | string or Object | a div element or a string id of the div element |
9696

97+
<br>
98+
*Example:*
99+
```js
100+
const props = {
101+
// data is an array of rows,
102+
// first col - nPoints or number of points in the bin, second col - binMin, third col - binMax
103+
// supports variable length bins
104+
data : [
105+
[1,1,10],
106+
[10,10,100],
107+
[100,100,1000],
108+
[1000,1000,10000],
109+
[100,10000,100000],
110+
[10,100000,1000000],
111+
[1,1000000,10000000]
112+
],
113+
width: 400,
114+
height: 200,
115+
logs: 'xy',
116+
desc: 'Both X and Y are using log scale',
117+
binColor: '#336699'
118+
};
119+
// render Histogram component in the div with 'histogramHere' id
120+
firefly.util.renderDOM('histogramHere', firefly.ui.Histogram, props);
121+
```
122+
97123

98124
### Dispatching and watching actions
99125

src/firefly/html/demo/ffapi-2images.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,4 @@ <h3>Firefly - new API demo</h3>
328328
}
329329
</script>
330330

331-
<script type="text/javascript" src="http://localhost:8080/firefly/firefly_loader.js"></script>
331+
<script type="text/javascript" src="../firefly_loader.js"></script>

src/firefly/html/demo/ffapi-highlevel-charttest.html

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,51 @@ <h2>
2323
</h2>
2424
</div>
2525

26-
<div id="chartHere" style="display: inline-block; width: 800px; height: 550px; border: solid 1px;"></div>
27-
28-
26+
<div id="histogramHere" style="width: 800px; height: 550px; border: solid 1px"></div>
27+
<br/><br/>
28+
<div id="chartHere" style="width: 800px; height: 550px; border: solid 1px;"></div>
2929

3030
<script type="text/javascript">
3131
{
3232
onFireflyLoaded= function(firefly) {
3333

34+
// show histogram data no options, no connection to the table)
35+
const props = {
36+
// data is an array of rows,
37+
// first col - nPoints or number of points in the bin, second col - binMin, third col - binMax
38+
// (supports variable length bins
39+
data : [
40+
[1,1,10],
41+
[10,10,100],
42+
[100,100,1000],
43+
[1000,1000,10000],
44+
[100,10000,100000],
45+
[10,100000,1000000],
46+
[1,1000000,10000000]
47+
],
48+
width: 800,
49+
height: 550,
50+
logs: 'xy',
51+
desc: 'Both X and Y are using log scale',
52+
binColor: '#336699'
53+
};
54+
firefly.util.renderDOM('histogramHere', firefly.ui.Histogram, props);
3455

56+
// show scatter chart
3557
firefly.debug= true;
3658
chartParams = {
3759
source: 'http://web.ipac.caltech.edu/staff/roby/demo/WiseDemoTable.tbl',
3860
xCol: 'ra1',
3961
yCol: 'dec1'
4062
};
63+
firefly.showXYPlot('chartHere', chartParams);
64+
4165

4266

43-
firefly.showXYPlot('chartHere', chartParams);
4467
}
4568
}
4669

70+
4771
</script>
4872

4973

src/firefly/html/demo/ffapi-highlevel-test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,6 @@
501501
</script>
502502

503503

504-
<script type="text/javascript" src="http://localhost:8080/firefly/firefly_loader.js"></script>
504+
<script type="text/javascript" src="../firefly_loader.js"></script>
505505

506506

src/firefly/js/api/ApiBuild.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {TablesContainer} from '../tables/ui/TablesContainer.jsx';
3535
import {TablePanel} from '../tables/ui/TablePanel.jsx';
3636
import {ChartsContainer} from '../charts/ui/ChartsContainer.jsx';
3737
import {ChartsTableViewPanel} from '../charts/ui/ChartsTableViewPanel.jsx';
38+
import {Histogram} from '../charts/ui/Histogram.jsx';
3839
import {PopupMouseReadoutMinimal} from '../visualize/ui/PopupMouseReadoutMinimal.jsx';
3940
import {PopupMouseReadoutFull} from '../visualize/ui/PopupMouseReadoutFull.jsx';
4041

@@ -186,6 +187,7 @@ export function buildLowlevelAPI() {
186187
TablePanel,
187188
ChartsContainer,
188189
ChartsTableViewPanel,
190+
Histogram,
189191
PopupMouseReadoutMinimal,
190192
PopupMouseReadoutFull
191193
};

src/firefly/js/charts/ui/Histogram.jsx

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,30 @@ function shadeColor(color, percent) {
1919
return `#${(0x1000000+(Math.round((t-R)*p)+R)*0x10000+(Math.round((t-G)*p)+G)*0x100+(Math.round((t-B)*p)+B)).toString(16).slice(1)}`;
2020
}
2121

22+
function padRight(num) {
23+
return num+Math.abs(num*Math.pow(10,-9));
24+
}
25+
26+
function padLeft(num) {
27+
return num-Math.abs(num*Math.pow(10,-9));
28+
}
29+
2230

2331
export class Histogram extends React.Component {
2432

33+
/**
34+
* @summary React Component to display histogram.
35+
*
36+
* @param {Object} props
37+
* @param {Array.number[]} props.data - array of numbers [0] - nInBin, [1] - binMin, [2] - binMax
38+
* @param {number} props.width - width of the chart in pixels
39+
* @param {number} props.height - height of the chart in pixels
40+
* @param {string} [props.logs] - can have values 'x', 'y', or 'xy'
41+
* @param {string} [props.binColor='#d1d1d1'] - darker bin color
42+
* @param {string} props.desc - description
43+
* @public
44+
* @memberof firefly.ui
45+
*/
2546
constructor(props) {
2647
super(props);
2748
this.setChartConfig = this.setChartConfig.bind(this);
@@ -32,7 +53,7 @@ export class Histogram extends React.Component {
3253
const {data, width, height, logs, reversed, desc, binColor} = this.props;
3354
// should rerender only if data or bin color has changed
3455
// otherwise just change the existing chart
35-
if (data != nextProps.data || binColor !== nextProps.binColor) { return true; }
56+
if (data !== nextProps.data || binColor !== nextProps.binColor) { return true; }
3657
const chart = this.refs.chart && this.refs.chart.getChart();
3758
if (chart) {
3859
let doUpdate = false;
@@ -112,9 +133,12 @@ export class Histogram extends React.Component {
112133
* @return {Boolean} f data points are set, false if no points are present
113134
*/
114135
setChartConfig(config) {
136+
137+
// how many significant digits should we preserve? ~12?
138+
// EPSILON 2^(-52)
115139
const TINY_OFFSET = 100*Number.EPSILON;
116140

117-
const {binColor, data}= this.props;
141+
const {binColor, logs, data}= this.props;
118142

119143
if (!data || data.length < 1) {
120144
return false;
@@ -145,14 +169,24 @@ export class Histogram extends React.Component {
145169
}
146170

147171
try {
172+
173+
// what should be the minimum y value?
174+
let minY = 0;
175+
if (logs && logs.includes('y') && data.length>0) {
176+
const minYData = data.reduce((minVal, row) => {
177+
return Math.min(minVal, row[0]);
178+
}, data[0][0]);
179+
minY = minYData/10;
180+
}
148181
let lastBinMax = data[0][1];
149182
if (areaPlot) {
183+
150184
// point before the first one
151185
points.push({
152186
name: '',
153187
range: '',
154-
x: lastBinMax - 2*TINY_OFFSET,
155-
y: 0
188+
x: padLeft(lastBinMax),
189+
y: minY
156190
});
157191
}
158192
data.forEach(function (value, index) {
@@ -172,14 +206,14 @@ export class Histogram extends React.Component {
172206
points.push({
173207
name: gapCenterStr,
174208
range: gapRangeStr,
175-
x: lastBinMax + TINY_OFFSET,
176-
y: 0
209+
x: padRight(lastBinMax),
210+
y: minY
177211
});
178212
points.push({
179213
name: gapCenterStr,
180214
range: gapRangeStr,
181-
x: data[index][1] - TINY_OFFSET,
182-
y: 0
215+
x: padLeft(data[index][1]),
216+
y: minY
183217
});
184218
}
185219
lastBinMax = data[index][2];
@@ -203,7 +237,7 @@ export class Histogram extends React.Component {
203237
name: centerStr,
204238
range: rangeStr,
205239
// x - binmax
206-
x: (xrange > TINY_OFFSET) ? (data[index][2] - TINY_OFFSET) : (data[index][1] + TINY_OFFSET),
240+
x: (xrange > TINY_OFFSET) ? padLeft(data[index][2]) : padRight(data[index][1]),
207241
// y - number of points in the bin
208242
y: data[index][0]
209243
});
@@ -234,10 +268,11 @@ export class Histogram extends React.Component {
234268
points.push({
235269
name: '',
236270
range: '',
237-
x: lastBinMax + 2*TINY_OFFSET,
238-
y: 0
271+
x: padRight(lastBinMax),
272+
y: minY
239273
});
240274
}
275+
config.yAxis.min = minY;
241276
}
242277
catch(e) {
243278
error = e;
@@ -270,8 +305,8 @@ export class Histogram extends React.Component {
270305
renderTo: 'container',
271306
type: chartType,
272307
alignTicks: false,
273-
width: Number(width),
274-
height: Number(height),
308+
width: width? Number(width) : undefined,
309+
height: height? Number(height) : undefined,
275310
borderColor: '#a5a5a5',
276311
borderWidth: 1
277312
},

0 commit comments

Comments
 (0)