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

Refactor the distribution summary component #470

Merged
merged 2 commits into from
Jun 4, 2016
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
94 changes: 24 additions & 70 deletions src/components/distributions/summary/index.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,34 @@
import React, {Component, PropTypes} from 'react'
import React from 'react'

import numberShow from 'lib/numberShower/numberShower'
import ShowIf from 'gComponents/utility/showIf'

import './style.css'

function formatStat(n){
if (n) {
let value = parseFloat(n);
return numberShow(value);
}
}

const PrecisionNumber = ({value, precision}) => {
const number = numberShow(value, precision)
return (
<span>
{number.value}{number.symbol}
{number.power && (<span>{`\u00b710`}<sup>{number.power}</sup></span>)}
</span>
)
}
const PrecisionNumber = ({value, precision, number=numberShow(value, precision)}) => (
<span>
{number.value}{number.symbol}
{number.power && (<span>{`\u00b710`}<sup>{number.power}</sup></span>)}
</span>
)

const UncertaintyRange = ({low, high}) => (
<div className='UncertaintyRange'>
<PrecisionNumber value={low}/> to <PrecisionNumber value={high}/>
const PointDisplay = ({value, precision=6}) => (
<div className='mean'>
<PrecisionNumber value={value} precision={precision}/>
</div>
)

@ShowIf
class DistributionSummarySmall extends Component{
static propTypes = {
stats: PropTypes.object,
}
render () {
const {length, mean, stdev, adjustedConfidenceInterval} = this.props.stats

let low
let high
if (_.isObject(adjustedConfidenceInterval)) {
[low, high] = adjustedConfidenceInterval
}

const precision = length === 1 ? 6 : 2
return (
<div className="DistributionSummary">
<div className='mean'>
<PrecisionNumber value={parseFloat(mean)} precision={precision}/>
</div>
{!!low && !!high && (low !== high) &&
<UncertaintyRange low={low} high={high} />
}
</div>
)
}
}

export default class DistributionSummary extends Component{
static propTypes = {
simulation: PropTypes.object,
}

shouldComponentUpdate(newProps) {
return (_.get(this.props, 'simulation.stats') !== _.get(newProps, 'simulation.stats'))
}

stats(){
return _.get(this.props.simulation, 'stats') || false
}
render () {
return (
<DistributionSummarySmall
showIf={_.isFinite(_.get(this.stats(), 'mean'))}
stats={this.stats()}
/>
)
}
};
const DistributionDisplay = ({mean, adjustedConfidenceInterval: [low, high]}) => (
<div>
<PointDisplay value={mean} precision={2}/>
<div className='UncertaintyRange'>
<PrecisionNumber value={low}/> to <PrecisionNumber value={high}/>
</div>
</div>
)

export const DistributionSummary = ({length, mean, adjustedConfidenceInterval}) => (
<div className="DistributionSummary">
{length === 1 && <PointDisplay value={mean}/>}
{length > 1 && <DistributionDisplay mean={mean} adjustedConfidenceInterval={adjustedConfidenceInterval}/>}
</div>
)
20 changes: 11 additions & 9 deletions src/components/metrics/card/MetricCardViewSection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Icon from 'react-fa'

import Histogram from 'gComponents/simulations/histogram/index'
import MetricName from 'gComponents/metrics/card/name/index'
import DistributionSummary from 'gComponents/distributions/summary/index'
import {DistributionSummary} from 'gComponents/distributions/summary/index'
import StatTable from 'gComponents/simulations/stat_table/index'
import MetricToken from 'gComponents/metrics/card/token/index'
import SensitivitySection from 'gComponents/metrics/card/SensitivitySection/SensitivitySection'
Expand Down Expand Up @@ -56,19 +56,19 @@ export default class MetricCardViewSection extends Component {
}

render() {
const {canvasState,
const {canvasState: {metricCardView, metricClickMode},
metric,
inSelectedCell,
onChangeName,
onOpenModal,
jumpSection,
onMouseDown,
showSensitivitySection
showSensitivitySection,
} = this.props

const errors = this._errors()
const {canvasState: {metricCardView, metricClickMode}} = this.props
const {guesstimate} = metric
const stats = _.get(metric, 'simulation.stats')
const showSimulation = this.showSimulation()
const shouldShowStatistics = this._shouldShowStatistics()
const hasGuesstimateDescription = !_.isEmpty(guesstimate.description)
Expand All @@ -91,10 +91,10 @@ export default class MetricCardViewSection extends Component {

<div className='MetricTokenSection'>
<MetricToken
readableId={metric.readableId}
anotherFunctionSelected={anotherFunctionSelected}
onOpenModal={onOpenModal}
hasGuesstimateDescription={hasGuesstimateDescription}
readableId={metric.readableId}
anotherFunctionSelected={anotherFunctionSelected}
onOpenModal={onOpenModal}
hasGuesstimateDescription={hasGuesstimateDescription}
/>
</div>

Expand All @@ -121,7 +121,9 @@ export default class MetricCardViewSection extends Component {
{showSimulation &&
<div className='StatsSectionBody'>
<DistributionSummary
simulation={metric.simulation}
length={stats.length}
mean={stats.mean}
adjustedConfidenceInterval={stats.adjustedConfidenceInterval}
/>
</div>
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/metrics/card/token/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const MetricReadableId = ({readableId}) => (

const MetricExpandButton = ({onOpenModal}) => (
<span
className='hover-toggle hover-icon'
onMouseDown={onOpenModal}
data-select='false'
className='hover-toggle hover-icon'
onMouseDown={onOpenModal}
data-select='false'
>
<Icon name='expand'/>
</span>
Expand Down
7 changes: 5 additions & 2 deletions src/components/metrics/modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {Component} from 'react'

import Modal from 'react-modal'

import DistributionSummary from 'gComponents/distributions/summary/index'
import {DistributionSummary} from 'gComponents/distributions/summary/index'
import DistributionEditor from 'gComponents/distributions/editor/index'
import Histogram from 'gComponents/simulations/histogram/index'
import GuesstimateDescription from './description'
Expand Down Expand Up @@ -67,6 +67,7 @@ export default class MetricModal extends Component {

const {closeModal, metric} = this.props
const sortedSampleValues = _.get(metric, 'simulation.sample.sortedValues')
const stats = _.get(metric, 'simulation.stats')
const guesstimate = metric.guesstimate
return(
<Modal
Expand Down Expand Up @@ -94,7 +95,9 @@ export default class MetricModal extends Component {
<div className='col-sm-9 mean subsection'>
{showSimulation &&
<DistributionSummary
simulation={metric.simulation}
length={stats.length}
mean={stats.mean}
adjustedConfidenceInterval={stats.adjustedConfidenceInterval}
/>
}
</div>
Expand Down
12 changes: 10 additions & 2 deletions src/modules/simulations/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import e from 'lib/engine/engine'
import {sampleMean, sampleStdev, percentile, cutoff, sortDescending} from 'lib/dataAnalysis.js'

function addStats(simulation){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of this code is in another PR

if (!_.has(simulation, 'sample.values.length') || (simulation.sample.values.length <= 1)) {
return simulation
if (!_.has(simulation, 'sample.values.length') || (simulation.sample.values.length === 0)) {
return
} else if (simulation.sample.values.length === 1) {
simulation.stats = {
mean: simulation.sample.values[0],
stdev: 0,
length: 1,
}
simulation.sample.sortedValues = simulation.sample.values
return
}

const sortedValues = sortDescending(simulation.sample.values)
Expand Down