cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
UEJ
UEJ
Level II

How to dynamically change color column in graph builder

Hi,

 

upon creation of an Graph Builder object one can define variables as follows: Variables(X(:x), Y(:y), Color(:color))

 

What I would like to do is to change the color column dynamically by selecting specific columns from a list. How can this be achieved?

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to dynamically change color column in graph builder

Here is very quick version of the script

Names Default To Here(1); 

If(Is Empty(Current Report()),
	Throw("No open reports found");
);

cur_report = Current Report() << XPath("//OutlineBox[@helpKey='Graph Builder']");
If(N Items(cur_report) < 1,
	Throw("No graph builder open");
,
	cur_report = cur_report[1] << Get Scriptable Object;
	dt = cur_report << Get Data Table;
);

// Find current Color role
find_role_column = Function({gb, role = "Color"}, {Default Local},
	vars = gb << Get Variables();
	col = "";
	role_idx = 0;
	For Each({var, idx}, vars,
		role = var["Role"];
		If(role == "Color",
			col = var[1] << get name;
			role_idx = idx;
			break();
		);
	);
	return(Eval List({col, role_idx}));
);

replace_role_column = function({gb, new_col, role = "Color"}, {Default Local},
	{colname, role_idx} = find_role_column(gb);
	gb << inval;
	If(role_idx > 0,
		gb << Remove Variable(role_idx);
	);
	Eval(EvalExpr(
		gb << Add Variable({Expr(NameExpr(AsColumn(new_col))), Role(Expr(role))});
	));
	wait(0);
	gb << update window;
	return(1);
);

nw = new window("demo",
	clb = Col List Box(Datatable(dt), width(250), maxSelected(1),
		<< Set Function(function({this},
			replace_role_column(cur_report, this << get selected, "Color");
		));
	)
);
clb << Set Items(dt << Get Column Names("String"));

Creating better one will take some time and I might have enough time during weekend to create one (it is fairly similar to my Column Quick Swapper - easily change multiple Y and X-axis columns in Graph Builder )

-Jarmo

View solution in original post

7 REPLIES 7
jthi
Super User

Re: How to dynamically change color column in graph builder

You can use column switcher for this

jthi_0-1715934151953.png

jthi_1-1715934160141.png

jthi_2-1715934169049.png

jthi_3-1715934177331.png

 

-Jarmo
UEJ
UEJ
Level II

Re: How to dynamically change color column in graph builder

Hi Jarmo, this is true, but obviously I have to be more precise: The column switcher would need to affect only the color column, not let's say the x or y column.

jthi
Super User

Re: How to dynamically change color column in graph builder

Isn't that exactly what column switcher does? Only "issue" with column switcher is that you cannot have same column in multiple roles at the same time.

-Jarmo
UEJ
UEJ
Level II

Re: How to dynamically change color column in graph builder

'Only "issue" with column switcher is that you cannot have same column in multiple roles at the same time.': this is EXACTLY what I need, unfortunately.

jthi
Super User

Re: How to dynamically change color column in graph builder

I did create wish list item for that Add new switcher called Role Switcher . Currently I think this will require scripting solution, if it just limited to graph builder AND color role it is bit more easy to create (I will most likely create add-in for this when I have time).

-Jarmo
jthi
Super User

Re: How to dynamically change color column in graph builder

Here is very quick version of the script

Names Default To Here(1); 

If(Is Empty(Current Report()),
	Throw("No open reports found");
);

cur_report = Current Report() << XPath("//OutlineBox[@helpKey='Graph Builder']");
If(N Items(cur_report) < 1,
	Throw("No graph builder open");
,
	cur_report = cur_report[1] << Get Scriptable Object;
	dt = cur_report << Get Data Table;
);

// Find current Color role
find_role_column = Function({gb, role = "Color"}, {Default Local},
	vars = gb << Get Variables();
	col = "";
	role_idx = 0;
	For Each({var, idx}, vars,
		role = var["Role"];
		If(role == "Color",
			col = var[1] << get name;
			role_idx = idx;
			break();
		);
	);
	return(Eval List({col, role_idx}));
);

replace_role_column = function({gb, new_col, role = "Color"}, {Default Local},
	{colname, role_idx} = find_role_column(gb);
	gb << inval;
	If(role_idx > 0,
		gb << Remove Variable(role_idx);
	);
	Eval(EvalExpr(
		gb << Add Variable({Expr(NameExpr(AsColumn(new_col))), Role(Expr(role))});
	));
	wait(0);
	gb << update window;
	return(1);
);

nw = new window("demo",
	clb = Col List Box(Datatable(dt), width(250), maxSelected(1),
		<< Set Function(function({this},
			replace_role_column(cur_report, this << get selected, "Color");
		));
	)
);
clb << Set Items(dt << Get Column Names("String"));

Creating better one will take some time and I might have enough time during weekend to create one (it is fairly similar to my Column Quick Swapper - easily change multiple Y and X-axis columns in Graph Builder )

-Jarmo
UEJ
UEJ
Level II

Re: How to dynamically change color column in graph builder

Interesting. I think I got the bits and pieces from your script, but I will need some time to work on this next week.