Automatically deactivate linear regression for non-numerical selections
@@ -175,6 +175,10 @@ export default class ScatterPlot extends React.Component {
this.setState(this.setup());
};
+ checkForNumericalSelections = () =>
+ this.handleType(this.state.toolY, this.state.columnY) !== "ordinal" &&
+ this.handleType(this.state.toolX, this.state.columnX) !== "ordinal";
+
// --------------------rendering-----------------------------
renderData = () => {
let array = [];
@@ -219,13 +223,13 @@ export default class ScatterPlot extends React.Component {
this.lineCount = array.length;
this.dataArray = array;
- const areSelectionsNumeric =
- this.handleType(this.state.toolY, this.state.columnY) !== "ordinal" &&
- this.handleType(this.state.toolX, this.state.columnX) !== "ordinal";
- if (
- this.state.regression !== regressionOptions.hidden &&
- areSelectionsNumeric
- ) {
+ const isRegressionEnabled =
+ this.state.regression !== regressionOptions.hidden;
+ const areSelectionsNumerical = this.checkForNumericalSelections();
+ if (isRegressionEnabled) {
+ if (!areSelectionsNumerical) {
+ setParam({ regression: regressionOptions.hidden });
+ } else {
const regression = calcRegression.linear(
array.map((data) => [data.x, data.y]),
);
@@ -248,15 +252,22 @@ export default class ScatterPlot extends React.Component {
JSON.parse,
).concat(endPoints);
- const unitX = this.props.tools[this.state.toolX].columns[this.state.columnX].unit;
- const unitY = this.props.tools[this.state.toolY].columns[this.state.columnY].unit;
+ const unitX = this.props.tools[this.state.toolX].columns[
+ this.state.columnX
+ ].unit;
+ const unitY = this.props.tools[this.state.toolY].columns[
+ this.state.columnY
+ ].unit;
const helpText = `Estimation technique: ordinary least squares (OLS)
Predictor variable (X-Axis) in ${unitX}: ${this.state.nameX}
Response variable (Y-Axis) in ${unitY}: ${this.state.nameY}
Regression coefficient: ${regression.equation[0]}
Intercept: ${regression.equation[1]}
Equation: ${regression.string}
- Coefficient of Determination: ${regression.r2}`.replace(/^ +/gm, "");
+ Coefficient of Determination: ${regression.r2}`.replace(
+ /^ +/gm,
+ "",
+ );
this.regressionData = {
regression,
text: helpText,
@@ -264,6 +275,7 @@ export default class ScatterPlot extends React.Component {
lowerConfidenceBorderData: confidenceIntervalBorders.lowerBorderData,
};
}
+ }
};
setMinMaxValues = (array) => {
@@ -352,7 +364,15 @@ export default class ScatterPlot extends React.Component {
{renderSetting(
"Regression",
this.state.regression,
- (ev) => setParam({ regression: ev.target.value }),
+ (ev) => {
+ if (this.checkForNumericalSelections()) {
+ setParam({ regression: ev.target.value });
+ } else {
+ alert(
+ "Regressions are only available for numerical selections.",
+ );
+ }
+ },
regressionOptions,
this.state.regression !== regressionOptions.hidden &&
this.regressionData
@@ -609,6 +629,7 @@ export default class ScatterPlot extends React.Component {
}
/>
{this.state.regression !== regressionOptions.hidden &&
+ this.checkForNumericalSelections() &&
this.regressionData &&
this.renderRegressionAndConfidenceIntervals()}
{this.state.value ? <Hint value={this.state.value} /> : null}
