Skip to content

Commit

Permalink
Feat: pointseries count (elastic#245)
Browse files Browse the repository at this point in the history
* feat: add labeling to math functions

* feat: auto-select first column when using count
  • Loading branch information
w33ble authored Nov 22, 2017
1 parent 340c897 commit aa70e99
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
26 changes: 18 additions & 8 deletions public/expression_types/arg_types/datacolumn/datacolumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,24 @@ const DatacolumnArgInput = ({ onValueChange, columns, mathValue, setMathFunction
const inputRefs = {};
const valueNotSet = val => !val || val.length === 0;
const updateFunctionValue = () => {
// if setting size, auto-select the first column if no column is already set
if (inputRefs.fn.value === 'size') {
const col = inputRefs.column.value || columns[0] && columns[0].name;
if (col) return onValueChange(`${inputRefs.fn.value}(${col})`);
}

// inputRefs.column is the column selection, if there is no value, do nothing
if (valueNotSet(inputRefs.column.value)) {
setMathFunction(inputRefs.fn.value);
} else if (valueNotSet(inputRefs.fn.value)) {
// inputRefs.fn is the math.js function to use, if it's not set, just use the value input
onValueChange(inputRefs.column.value);
} else {
// inputRefs.fn has a value, so use it as a math.js expression
onValueChange(`${inputRefs.fn.value}(${inputRefs.column.value})`);
return setMathFunction(inputRefs.fn.value);
}

// inputRefs.fn is the math.js function to use, if it's not set, just use the value input
if (valueNotSet(inputRefs.fn.value)) {
return onValueChange(inputRefs.column.value);
}

// inputRefs.fn has a value, so use it as a math.js expression
onValueChange(`${inputRefs.fn.value}(${inputRefs.column.value})`);
};

const column = columns.map(col => col.name).find(colName => colName === mathValue.column) || '';
Expand All @@ -77,7 +85,9 @@ const DatacolumnArgInput = ({ onValueChange, columns, mathValue, setMathFunction
onChange={updateFunctionValue}
>
<option value="" disabled>select column</option>
{ sortBy(columns, 'name').map(column => <option key={column.name} value={column.name}>{column.name}</option>) }
{ sortBy(columns, 'name').map(column => (
<option key={column.name} value={column.name}>{column.name}</option>
)) }
</FormControl>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { FormControl } from 'react-bootstrap';

export const SimpleMathFunction = ({ onChange, value, inputRef }) => {
const options = [
'median',
'mean',
'sum',
'min',
'max',
'mode',
'size',
{ label: 'Value', value: '' },
{ label: 'Average', value: 'mean' },
{ label: 'Sum', value: 'sum' },
{ label: 'Count', value: 'size' },
{ label: 'Max', value: 'max' },
{ label: 'Min', value: 'min' },
{ label: 'Median', value: 'median' },
{ label: 'Mode', value: 'mode' },
];

const onSelect = (ev) => onChange(ev.target.value);
Expand All @@ -24,8 +25,9 @@ export const SimpleMathFunction = ({ onChange, value, inputRef }) => {
onChange={onSelect}
inputRef={inputRef}
>
<option value="">value</option>
{ options.map(option => (<option key={option} value={option}>{option}</option>))}
{ options.map(option => (
<option key={option.value} value={option.value}>{option.label}</option>
))}
</FormControl>
</div>
);
Expand Down

0 comments on commit aa70e99

Please sign in to comment.