From 036ce1203a1e3aa0b3bf76879caaa0bf1c07b7a9 Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Wed, 8 Mar 2023 10:04:47 -0700 Subject: [PATCH] Adding plotBounds option to CurveNode, see https://github.com/phetsims/calculus-grapher/issues/259 --- js/common/view/CurveNode.ts | 16 +++++++++++++++- js/common/view/GraphNode.ts | 11 ++++++++--- js/common/view/OriginalGraphNode.ts | 4 +++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/js/common/view/CurveNode.ts b/js/common/view/CurveNode.ts index 6b5f1792..41c33f66 100644 --- a/js/common/view/CurveNode.ts +++ b/js/common/view/CurveNode.ts @@ -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 )[]; @@ -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 & @@ -114,7 +119,8 @@ export default class CurveNode extends Node { radius: 1 }, - plotBoundsMethod: 'accurate' + plotBoundsMethod: 'accurate', + plotBounds: null }, providedOptions ); @@ -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 ); diff --git a/js/common/view/GraphNode.ts b/js/common/view/GraphNode.ts index 2c797ddc..667c8280 100644 --- a/js/common/view/GraphNode.ts +++ b/js/common/view/GraphNode.ts @@ -93,7 +93,7 @@ type SelfOptions = { chartRectangleOptions?: PickOptional; // 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; @@ -138,8 +138,9 @@ export default class GraphNode extends Node { const options = optionize, 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 @@ -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' ), @@ -326,6 +327,10 @@ export default class GraphNode extends Node { } ); } + protected getChartBounds(): Bounds2 { + return this.chartRectangle.getShape().bounds; + } + /** * Resets all */ diff --git a/js/common/view/OriginalGraphNode.ts b/js/common/view/OriginalGraphNode.ts index 78b5deda..21eba316 100644 --- a/js/common/view/OriginalGraphNode.ts +++ b/js/common/view/OriginalGraphNode.ts @@ -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 @@ -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,