Subscribe Bookmark RSS Feed

scripting a bar chart in graph builder

hlazar

Occasional Contributor

Joined:

Jan 13, 2017

Hello. I am scripting a bar chart in graph builder because using a standard chart does not eliminate empty bars where as the graph builder does. I am allowing for the use of continuous modeling types as an X factor. Let me explain how I am approaching this.

 

The user selects some columns by which to plot a bar chart. I create the chart using graph builder and the first column selected. Then if there are more than one columns selected, I go in and <<add variables to the graph builder. This works exactly as I want it except when one of the selected columns is continuous. Then the graph builder replaces the entire X with the continuous variable and says there are more variables there but it is only plotting versus the one. When I create the chart manually like this by changing the continuous variable to ordinal, it works the way I want it. So I change each continuous column to oridinal but the graph still does not add the variable but replaces it. 

 

Does anyone have any ideas how I can make this work the way I want it? Here is a simplified version of my script:

myWin = Column Dialog( FactorCol = Col List( "Multivariate Factor(s)" ) );

myFactor = myWin["FactorCol"];

myPlot = Graph Builder(
	Show Control Panel( 0 ),
	Show Legend( 0 ),
	Variables( X( Eval( myFactor[1] ) ), Y( yCol ), Color( Eval( myColor ) ) ),
	Elements( Bar( X, Y, Summary Statistic( "Std Dev" ), Label( "Label by Value" ) ) ),
	SendToReport(
		Dispatch(
			{},
			ScaleBox,
			{Label Row( 1, {Automatic Tick Marks( 0 ), Show Major Ticks( 1 ), Lower Frame( 1 ), Tick Mark Style( "Long Divider" )} )}
		)
	)
);

//If there is more than one factor then need to add variables to the graph
If( N Items( myFactor ) > 1,
	myRep = Report( myPlot )[Graph Builder Box( 1 )];
	For( j = 2, j <= N Items( myFactor ), j++,
		xcol = Column( dtmm, myFactor[j] );
		If( xcol << Get Modeling Type == "Continuous", 
//set a column property so I can change it back later
			xcol << Set Property( "change", 1 );
			xcol << Modeling Type( "Ordinal" );
		);
		myRep << Add Variable( {xcol, Role( "X" ), Position( 1 )} );
	);
//now I have to adjust all the x-axis for the way I want it
	For( j = 1, j <= N Items( myFactor ), j++,
		Report( myPlot )[Axisbox( 1 )] << {Label Row(
			j,
			{Automatic Tick Marks( 0 ), Show Major Ticks( 1 ), Lower Frame( 1 ), Tick Mark Style( "Long Divider" )}
		)}
	);
);




 

1 ACCEPTED SOLUTION

Accepted Solutions
hlazar

Occasional Contributor

Joined:

Jan 13, 2017

Solution

Actually, I figured out how to do this by checking the variables of the graph builder box step by step. If the column is continuous, I change it to ordinal and when adding a variable, you have to add an inner position term. Here is the pertinant script bit.

 

//graph builder can not add factors unless they are ordinal or nominal
//so if it is continuous, change to ordinal, and set a column property to change back later
			If( xcol << Get Modeling Type == "Continuous",
				xcol << Set Property( "change", {1} );
				xcol << Modeling Type( "Ordinal" );
				myRep << Add Variable( {xcol, Role( "X" ), Position( 1 ), Inner Position[i]} ),
			,
				myRep << Add Variable( {xcol, Role( "X" ), Position( 1 )} )
			);

This does exactly what I want it to do.

H

3 REPLIES
stephen_pearson

Community Trekker

Joined:

Oct 6, 2014

The graph builder behaves 'smartly' based on the column data types and type of graph selected. When I have done this myself, I have found it easier to build the graph builder code up as a text string, and then parsing the string to run the code. This can be a little tricky as you need to replace the " character using \!".

I did this by building the plot I wanted manually and looking at the code. I used a loop to build up the text string bit by bit for each column.
hlazar

Occasional Contributor

Joined:

Jan 13, 2017

Thank you. Yes, I know creating a string script is an option but I am always reluctant to do that. But it may be necessary in this case. I am disappointed that is the case.

 

best

H

hlazar

Occasional Contributor

Joined:

Jan 13, 2017

Solution

Actually, I figured out how to do this by checking the variables of the graph builder box step by step. If the column is continuous, I change it to ordinal and when adding a variable, you have to add an inner position term. Here is the pertinant script bit.

 

//graph builder can not add factors unless they are ordinal or nominal
//so if it is continuous, change to ordinal, and set a column property to change back later
			If( xcol << Get Modeling Type == "Continuous",
				xcol << Set Property( "change", {1} );
				xcol << Modeling Type( "Ordinal" );
				myRep << Add Variable( {xcol, Role( "X" ), Position( 1 ), Inner Position[i]} ),
			,
				myRep << Add Variable( {xcol, Role( "X" ), Position( 1 )} )
			);

This does exactly what I want it to do.

H