Subscribe Bookmark RSS Feed

if/else statements to change background colours depending on y-variable

mt

New Contributor

Joined:

Jun 7, 2017

Hi, I've got some different graphs, where the background concists of different gradients according to the y variable. I currently have three different graph builder's - one for each y-variable. Since the background gradients are the only things that vary, I wanted to make the code simpler by only having one graph builder, but using if/else statements to determine the correct background to apply: 

 

		Dispatch(
			{},
			"Graph Builder",
			FrameBox,
			{
			if(
				//graph 1
				yAxis == "1",
				Background Color( 1 ), 
				Add Image(Set Blob(green,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( graphMax ), Bottom( if(graphMin>0.51,graphMin,0.51) ) ),
					SetSize( {95401, 918} )
				),
				Add Image(Set Blob(red,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( if(graphMax<0.49,graphMax,0.49) ), Bottom( graphMin ) ),
					SetSize( {95401, 918} )
				),
				//graph 2
				yAxis == "2",
				Add Image(Set Blob(green,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( if(graphMax<0.75,graphMax,0.75) ), Bottom( if(graphMin>0.55,graphMin,0.55) ) ),
					SetSize( {95401, 918} )
				),
				Add Image(Set Blob(redCogTop,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( graphMax ), Bottom( if(graphMin>0.75,graphMin,0.75 ) )),
					SetSize( {95401, 918} )
				),
				Add Image(Set Blob(red,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( if(graphMax<0.55,graphMax,0.55) ), Bottom( graphMin ) ),
					SetSize( {95401, 918} )
				),
				//all other graphs
				Add Image(Set Blob(green,"png"),
					Bounds( Left( -100 ), Right( 100 ), Top( graphMax ), Bottom( graphMin ) ),
					SetSize( {95401, 918} )
				)
			),
			}
			Grid Line Order( 2 ), Reference Line Order( 3 ),
			DispatchSeg( BarSeg( 2 ), {Line Color( "Gray" )} )
				
			}
		),

Here's the original code from the first graph: 

Dispatch(
			{},
			"Graph Builder",
			FrameBox,
			{
			Background Color( 1 ), 
			Add Image(Set Blob(green,"png"),
				Bounds( Left( -100 ), Right( 100 ), Top( graphMax ), Bottom( if(graphMin>0.51,graphMin,0.51) ) ),
				SetSize( {95401, 918} )
			),
			Add Image(Set Blob(red,"png"),
				Bounds( Left( -100 ), Right( 100 ), Top( if(graphMax<0.49,graphMax,0.49) ), Bottom( graphMin ) ),
				SetSize( {95401, 918} )
			),
			Grid Line Order( 2 ), Reference Line Order( 3 ),
			DispatchSeg( BarSeg( 2 ), {Line Color( "Gray" )} )}
		),

 

 

Of course there are a lot of commas in the first code snippet which confuse the if(), so it doesn't work. Do you have any suggestions on how this issue could be solved?

 

Thanks heaps, 

Mikael

2 REPLIES
pmroz

Super User

Joined:

Jun 23, 2011

What I've typically done is build the graph builder statement dynamically as a text string.  Then execute it using eval(parse(txt)).

mt

New Contributor

Joined:

Jun 7, 2017

Thanks! I can't get it to work though - even using the eval(parse()) function on small parts of the code doesn't work for me. I will try to upload the code soon, perhaps that shed light on what is going on.