BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
TWE

Occasional Contributor

Joined:

Oct 8, 2018

Bar chart variable by list

Hello,

I try to script a bar chart like this one.

Graph Builder(
	Size( 534, 418 ),
	Show Control Panel( 0 ),
	Variables(
		Y( :Name( "A" ) ),
		Y( :Name( "B" ), Position( 1 ) ),
		Y( :Name( "C" ), Position( 1 ) ),
	),
	Elements(
		Bar(
			Y( 1 ),
			Y( 2 ),
			Y( 3 ),
			Legend( 3 ),
			Bar Style( "Stacked" )
		)
	)
)

The challange is, that I have the Y variables in a list which will be changed from time to time (e.g. {A, B, D, Test, Bla}).

I would like to have the bar chart automatically adjusted dependening on the list "l" e.g:

l = {"A", "B", "D", "Test", "Bla"};

...the script should then be looks like this: 

Variables(
		Y( :Name( "A" ) ),
		Y( :Name( "B" ), Position( 1 ) ),
		Y( :Name( "D" ), Position( 1 ) ),
                Y( :Name( "Test" ), Position( 1 ) ),
                Y( :Name( "Bla" ), Position( 1 ) ),   	
),

Elements(
		Bar(
			Y( 1 ),
			Y( 2 ),
			Y( 3 ),
                        Y( 4 ),
                        Y( 5 ),
			Legend( 3 ),
			Bar Style( "Stacked" )
		)
	)
)
...

 

After an database query the list l will be changed and I need an automatical update of the bar chart.

I already tried to transform the list into a column and  loop over the number of list entries but it generates multiple instead of one bar chart

for (i=1, i <= Nitems(l), i++,
show (l[i]);
dt << Graph Builder(
	Size( 534, 418 ),
	Show Control Panel( 0 ),
	Variables(
		Y( Eval (l[i] )) 
	),
	Elements(
		Bar(
			Y( i ),
			Legend( 3 ),
			Bar Style( "Stacked" )
		)
	)
));

Regards,

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: Bar chart variable by list

If you stack the desired columns then it doesn't matter how many you have.  This code should get you started:

dt1 = New Table( "Untitled 27", Add Rows( 3 ),
	New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [6, 5, 4] ) ),
	New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [7, 8, 9] ) ),
	New Column( "C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [3, 2, 1] ) )
);

col_list = dt1 << get column names(string);

dt2 = dt1 << Stack(
	columns(col_list),
	Source Label Column( "Variable" ),
	Stacked Data Column( "Value" )
);

dt2 << Graph Builder(
	Size( 526, 415 ),
	Show Control Panel( 0 ),
	Variables( Y( :Value ), Overlay( :Variable ) ),
	Elements( Bar( Y, Legend( 8 ), Bar Style( "Stacked" ) ) ),
	SendToReport( Dispatch( {}, "X title", TextEditBox, {Set Wrap( 2 )} ) )
)
2 REPLIES 2
pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: Bar chart variable by list

If you stack the desired columns then it doesn't matter how many you have.  This code should get you started:

dt1 = New Table( "Untitled 27", Add Rows( 3 ),
	New Column( "A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [6, 5, 4] ) ),
	New Column( "B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [7, 8, 9] ) ),
	New Column( "C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [3, 2, 1] ) )
);

col_list = dt1 << get column names(string);

dt2 = dt1 << Stack(
	columns(col_list),
	Source Label Column( "Variable" ),
	Stacked Data Column( "Value" )
);

dt2 << Graph Builder(
	Size( 526, 415 ),
	Show Control Panel( 0 ),
	Variables( Y( :Value ), Overlay( :Variable ) ),
	Elements( Bar( Y, Legend( 8 ), Bar Style( "Stacked" ) ) ),
	SendToReport( Dispatch( {}, "X title", TextEditBox, {Set Wrap( 2 )} ) )
)
TWE

Occasional Contributor

Joined:

Oct 8, 2018

Re: Bar chart variable by list

Thank you, this works fine!