Skip to content

Commit

Permalink
Adding plotBounds option to CurveNode, see #259
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Mar 8, 2023
1 parent 062adbb commit 036ce12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 15 additions & 1 deletion js/common/view/CurveNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import PickOptional from '../../../../phet-core/js/types/PickOptional.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import Bounds2 from '../../../../dot/js/Bounds2.js';

// dateset types associated with LinePlot and ScatterPlot
type LinePlotDataSet = ( Vector2 | null )[];
Expand All @@ -55,6 +56,10 @@ type SelfOptions = {
// boundsMethod to be used with bamboo plots. Override this where performance optimization is needed.
// See https://github.com/phetsims/calculus-grapher/issues/210 and https://github.com/phetsims/calculus-grapher/issues/226
plotBoundsMethod?: PathBoundsMethod;

// When plotBoundsMethod is 'none', plotBounds will be required to give correct (potential) bounds for the plot.
// See https://github.com/phetsims/calculus-grapher/issues/259
plotBounds?: Bounds2 | null;
};

export type CurveNodeOptions = SelfOptions &
Expand Down Expand Up @@ -114,7 +119,8 @@ export default class CurveNode extends Node {
radius: 1
},

plotBoundsMethod: 'accurate'
plotBoundsMethod: 'accurate',
plotBounds: null

}, providedOptions );

Expand Down Expand Up @@ -143,6 +149,14 @@ export default class CurveNode extends Node {
}, options.discontinuousPointsScatterPlotOptions ) );
this.addChild( this.discontinuousPointsScatterPlot );

assert && assert( options.plotBoundsMethod !== 'none' || options.plotBounds !== null,
'plotBounds must be provided when plotBoundsMethod is none' );
if ( options.plotBounds ) {
this.continuousLinePlot.localBounds = options.plotBounds;
this.discontinuousLinePlot.localBounds = options.plotBounds;
this.discontinuousPointsScatterPlot.localBounds = options.plotBounds;
}

// For debug purposes
if ( CalculusGrapherQueryParameters.allPoints ) {
this.allPointsScatterPlot = new ScatterPlot( chartTransform, this.getAllPointsScatterPlotDataSet(), options.allPointsScatterPlotOptions );
Expand Down
11 changes: 8 additions & 3 deletions js/common/view/GraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type SelfOptions = {
chartRectangleOptions?: PickOptional<ChartRectangleOptions, 'fill' | 'stroke'>;

// options function to create the CurveNode associated with this graph
createCurveNode?: ( chartTransform: ChartTransform, chartRectangleFill: TPaint ) => CurveNode;
createCurveNode?: ( chartTransform: ChartTransform, chartRectangleFill: TPaint, plotBounds: Bounds2 ) => CurveNode;

// label that appears in the upper-left corner of the graph
labelNode?: Node;
Expand Down Expand Up @@ -138,8 +138,9 @@ export default class GraphNode extends Node {
const options = optionize<GraphNodeOptions, StrictOmit<SelfOptions, 'labelNode'>, NodeOptions>()( {

// SelfOptions
createCurveNode: ( chartTransform: ChartTransform, chartRectangleFill: TPaint ) => new CurveNode( curve, chartTransform, {
createCurveNode: ( chartTransform: ChartTransform, chartRectangleFill: TPaint, plotBounds: Bounds2 ) => new CurveNode( curve, chartTransform, {
plotBoundsMethod: CalculusGrapherConstants.PLOT_BOUNDS_METHOD, // see https://github.com/phetsims/calculus-grapher/issues/210
plotBounds: plotBounds, // see https://github.com/phetsims/calculus-grapher/issues/259
stroke: graphType.strokeProperty,
discontinuousPointsScatterPlotOptions: {
fill: chartRectangleFill
Expand Down Expand Up @@ -185,7 +186,7 @@ export default class GraphNode extends Node {

this.chartRectangle = new ChartRectangle( this.chartTransform, options.chartRectangleOptions );

this.curveNode = options.createCurveNode( this.chartTransform, options.chartRectangleOptions.fill! );
this.curveNode = options.createCurveNode( this.chartTransform, options.chartRectangleOptions.fill!, this.getChartBounds() );

this.curveLayerVisibleProperty = new BooleanProperty( true, {
tandem: options.tandem.createTandem( 'curveLayerVisibleProperty' ),
Expand Down Expand Up @@ -326,6 +327,10 @@ export default class GraphNode extends Node {
} );
}

protected getChartBounds(): Bounds2 {
return this.chartRectangle.getShape().bounds;
}

/**
* Resets all
*/
Expand Down
4 changes: 3 additions & 1 deletion js/common/view/OriginalGraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ export default class OriginalGraphNode extends GraphNode {

// Creates PhET-iO element 'graphsNode.originalCurveNode'
const originalCurveNodeTandem = providedOptions.tandem.createTandem( 'originalCurveNode' );
const createOriginalCurveNode = ( chartTransform: ChartTransform, chartRectangleFill: TPaint ) =>
const createOriginalCurveNode = ( chartTransform: ChartTransform, chartRectangleFill: TPaint, plotBounds: Bounds2 ) =>
new TransformedCurveNode( originalCurve, curveManipulationProperties, chartTransform, {
plotBoundsMethod: CalculusGrapherConstants.PLOT_BOUNDS_METHOD, // see https://github.com/phetsims/calculus-grapher/issues/210
plotBounds: plotBounds, // see https://github.com/phetsims/calculus-grapher/issues/259
stroke: graphType.strokeProperty,
discontinuousPointsScatterPlotOptions: {
fill: chartRectangleFill
Expand Down Expand Up @@ -155,6 +156,7 @@ export default class OriginalGraphNode extends GraphNode {
// Create a predictCurveNode
this.predictCurveNode = new TransformedCurveNode( predictCurve, curveManipulationProperties, this.chartTransform, {
plotBoundsMethod: CalculusGrapherConstants.PLOT_BOUNDS_METHOD, // see https://github.com/phetsims/calculus-grapher/issues/210
plotBounds: this.getChartBounds(), // see https://github.com/phetsims/calculus-grapher/issues/259
isInteractiveProperty: predictEnabledProperty,
visibleProperty: predictEnabledProperty,
stroke: CalculusGrapherColors.predictCurveStrokeProperty,
Expand Down

0 comments on commit 036ce12

Please sign in to comment.