Skip to content

Commit

Permalink
only allowed 3D when 2D is done
Browse files Browse the repository at this point in the history
  • Loading branch information
ndittren committed Apr 23, 2024
1 parent b775091 commit f8a8ed2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
26 changes: 14 additions & 12 deletions media/js/src/simulationOne/scatterPlot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
const x = Math.round(rng() * 100);
const y = Math.round(correlation * x + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);
const z = Math.round(correlation * y + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);

generatedData.push({ x, y, z });
if (plotType === '3D') {
const z = Math.round(correlation * y + Math.sqrt(
1 - Math.pow(correlation, 2)) * rng() * 100);
generatedData.push({ x, y, z });
} else {
generatedData.push({ x, y });
}
}
}

Expand Down Expand Up @@ -52,10 +55,10 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
: undefined;

try {
let response;

if (plotType === '3d') {

response = await axios.post('/calculate_multiple_regression/', {
const response = await axios.post('/calc_multi_regression/', {
x1_values: x_values,
x2_values: y_values,
y_values: z_values,
Expand Down Expand Up @@ -85,11 +88,12 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
});

} else {
response = await axios.post('/calculate_regression/', {
const response = await axios.post('/calc_regression/', {
x_values,
y_values,
});
const { slope, intercept, stderr, rvalue } = response.data;

setSlope(slope);
setIntercept(intercept);
setStderror(stderr);
Expand All @@ -101,7 +105,8 @@ export const ScatterPlot = ({ N, correlation, seed, setAppRvalue,
mode: 'lines',
x: [Math.min(...x_values), Math.max(...x_values)],
// eslint-disable-next-line max-len
y: [slope * Math.min(...x_values) + intercept, slope * Math.max(...x_values) + intercept],
y: [slope * Math.min(...x_values) + intercept, slope * Math.max(
...x_values) + intercept],
marker: { color: 'red' },
});
}
Expand Down Expand Up @@ -203,10 +208,7 @@ ScatterPlot.propTypes = {
setSlope: PropTypes.func,
setIntercept: PropTypes.func,
setStderror: PropTypes.func,
slope: PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.number)
]),
slope: PropTypes.number,
stderror: PropTypes.number,
intercept: PropTypes.number,
plotType: PropTypes.oneOf(['2d', '3d']).isRequired,
Expand Down
14 changes: 12 additions & 2 deletions media/js/src/simulationOne/simulationOne.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SimulationOne = () => {
const [plotType, setPlotType] = useState('2d');
const [slopes, setSlopes] = useState([]);
const [stderrs, setStderrs] = useState([]);
const [is2DCompleted, setIs2DCompleted] = useState(false);


const saveGraphData = async() => {
Expand All @@ -45,6 +46,10 @@ export const SimulationOne = () => {
});
};

const handle2DCompletion = () => {
setIs2DCompleted(true);
};

const handleNChange = (e) => {
setN(parseInt(e.target.value));
};
Expand Down Expand Up @@ -315,7 +320,9 @@ export const SimulationOne = () => {
tvalue={tvalue}
hypothesizedSlope={hypothesizedSlope}
n={N}
appRvalue={appRvalue} />
appRvalue={appRvalue}
is2DCompleted={is2DCompleted}
on2DCompletion={handle2DCompletion} />
)}
</>
)}
Expand All @@ -335,7 +342,10 @@ export const SimulationOne = () => {
<a className={
plotType === '3d'
? 'active nav-link'
: 'nav-link'}
: (!is2DCompleted
? 'nav-link disabled'
: 'nav-link')
}
onClick={() => handlePlotTypeChange('3d')}
href='#'>3D</a>
</li>
Expand Down
7 changes: 6 additions & 1 deletion media/js/src/simulationOne/simulationOneQuiz.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { MultipleChoiceQuestion } from './multipleChoiceQuestion';
import PropTypes from 'prop-types';

export const SimulationOneQuiz = ({
appRvalue, tvalue, hypothesizedSlope, n
appRvalue, tvalue, hypothesizedSlope, n, is2DCompleted, on2DCompletion
}) => {
const [selectedOption, setSelectedOption] = useState(null);
const [completedChoices, setCompletedChoices] = useState([]);

const handleChoiceCompletion = () => {
setCompletedChoices([...completedChoices, selectedOption]);
setSelectedOption(null);
if (allChoicesCompleted) {
on2DCompletion();
}
};

const allChoicesCompleted = ['A', 'B', 'C'].every(
Expand Down Expand Up @@ -122,4 +125,6 @@ SimulationOneQuiz.propTypes = {
tvalue: PropTypes.string.isRequired,
hypothesizedSlope: PropTypes.any.isRequired,
n: PropTypes.any.isRequired,
is2DCompleted: PropTypes.bool.isRequired,
on2DCompletion: PropTypes.func.isRequired,
};
4 changes: 2 additions & 2 deletions metricsmentor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@

re_path(r'^course/(?P<pk>\d+)/$', views.CourseDetailView.as_view(),
name='course-detail-view'),
path('calculate_regression/', views.calculate_regression,
path('calc_regression/', views.calculate_regression,
name='calculate_regression'),
path('calculate_multiple_regression/', views.calculate_multiple_regression,
path('calc_multi_regression/', views.calculate_multiple_regression,
name='calculate_multiple_regression'),
path('calculate_pvalue/', views.calculate_pvalue,
name='calculate_pvalue'),
Expand Down

0 comments on commit f8a8ed2

Please sign in to comment.