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

Extract a common interface for scatter and line chart #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jeffsantos
Copy link

Hi all.

I was trying to port to GT Plotter some charts from the Alexander Bergel book, Agile Artificial Intelligence in Pharo, originally made using Roassal. One of the first book examples use a composite chart that mixes a scatter plot with a line chart. I tried to reproduce that in GT, but I realized that GtPlotterCompositeChart class only works with GtPlotterLineChart objects.

Comparing GtPlotterLineChart class definition with GtPlotterScatterChart I could notice many common instance variables and methods (many of them, just accessors on these variables). Thus, I decided to extract the common behavior to a new super class that I called GtPlotterXYChart.

Beyond the extraction of the common behavior, I need to do a small fix on methods axisXStencil and axisYStencil in the GtPlotterScatterChart class to guarantee the scatter chart object is correctly associated with the stencil. This was necessary since I got an error when the composite object create method executed. This is the small fix:

axisYStencil
	^ axisYStencil scatterChart: self

This is a fragment class diagram with the resultant classes related to the existent examples classes that test them. All original examples pass.

image

Here are the snippets to recreate the chart from the book (page 28, figure 1-12). I did it in a Lepiter page with three snippets:

Generate the scatter plot:

pairs := OrderedCollection new.

500 timesRepeat: [ 
	pairs add: (50 atRandom - 25) -> (50 atRandom - 25) ].
	
f := [ :x | (-2 * x) - 3 ].

data := GtPlotterDataGroup new values: pairs.
data := data background: [ :each | (each key > (f value: each value))
	ifTrue: [Color red ] 
	ifFalse: [Color blue ]].
	
dataGroup := GtPlotterDataGroup new values: data.
	
scatterChart := GtPlotterScatterChart new
	with: data;
	valueX: #key;
	scaleX: GtPlotterLinearScale new;
	labelFormatX: [ :x | x asString ];
	titleX: 'x';
	ticksX: 10;
	valueY: #value;
	scaleY: GtPlotterLinearScale new;
	titleY: 'y';
	ticksY: 4.

Generate the Line chart:

pairs2 := (-15 to: 15 by:0.1)
			collect: [ :x | x -> (f value: x value) ].
			
data2 := GtPlotterDataGroup new values: pairs2.
data2 := data2 background: Color black.

lineChart2 := GtPlotterLineChart new
	with: data2;
	valueX: #key;
	scaleX: GtPlotterLinearScale new;
	labelFormatX: [ :x | x asString ];
	titleX: 'x';
	ticksX: 4;
	valueY: #value;
	scaleY: GtPlotterLinearScale new;
	titleY: 'y';
	ticksY: 4.

Generate the composite chart:

compositeChart := GtPlotterCompositeChart new.
compositeChart 
	addPlot: scatterChart;
	addPlot: lineChart2;
	yourself

This is the result chart:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant