Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic Control of Graphics

31 views
Skip to first unread message

PeterSF

unread,
Nov 18, 2009, 7:11:58 AM11/18/09
to
I am new to the dynamic features of the Mathematica fron end. Having spent a
considerable amount of time, I am still strufggling with the following
problem: how to control dynamically individual elements of a combined graphics?
Specifically, we are given
Show[ Plot[ Sin[x], {x,0,Pi} ], Graphics[ { textColor, Text
["Test"] } ],
Graphics[ { lineWidth,
Line[{ {0,0}, {0,2}] } ] ]
The objective is to change:
* the color of text (the 2nd element) if and only if a mouse is
over that text;
* the thickness of the line (the 3rd element) if and only if a
mouse is
over that line.
I can see how to handle these simultaneously:

DynamicModule[ { lineWidth = Thin, textColor = Blue},
Dynamic @ EventHandler[ Show[ Plot[Sin[x], {x, 0, \[Pi]} ],
Graphics[{ textColor, Text["XXX", {1, 1}]}],
Graphics[{ lineWidth, Line[{{0, 0}, {2, 1}}]}]],
"MouseClicked" :> (textColor = Red, lineWidth )]]

The question is, how can one control the wdth and the color
individually?

More broadly, I need to perform the following tasks: upon mouseover to
change the appearance of a single graphics element (line, text, etc.);
then, upon mouse click, to store in a variable which elements was
selected (clicked over); and, upon pressing of the Delete key, to
remove that element from the combined graphics.

I would be most greatful for any and all of the help you could give
me.
Peter.


dr DanW

unread,
Nov 20, 2009, 6:40:21 AM11/20/09
to
Here is a function I wrote that creates a legend based on the tooltips
on the functions being plotted. Each line swatch in the legend is a
button that lets me dim (lower the opacity) or darken (opacity -> 1)
the associated curve. I tried very hard to get this to work on the
curves themselves (treating each curve as a button) but could never
get it to work. However, I find this function is extremely useful,
and I get many surprised gasps from the audience when I use it in a
presentation. Not quite what you were looking for, but maybe you can
use the technique.

Code follows. Enjoy.
--------------------

Attributes[DynamicLegend] = {HoldFirst};

DynamicLegend[(plotfnc_)[f_, args___, (opts___)?OptionQ]] :=
Module[{tags, ps, op, dim, makeLegend}, tags = Flatten[{f}][[All,
2]];
ps = MapIndexed[Directive[ColorData[1, #2[[1]]], Thick,
Opacity[op[#1]]] & , tags]; op[_] = 1; dim = 0.3;
makeLegend[s_, t_] := {Toggler[Dynamic[op[t]],
{1 -> Graphics[Flatten[{s, Line[{{0, 0}, {1, 0}}]}],
ImageSize -> 72/3, AspectRatio -> 0.1], Dynamic[dim] ->
Graphics[Flatten[{s, Line[{{0, 0}, {1, 0}}]}],
ImageSize -> 72/3, AspectRatio -> 0.1]}], Text[t]};
Column[{Dynamic[Row[{plotfnc[f, args, PlotStyle -> ps,
ImageSize -> Medium, opts], Grid[MapThread[makeLegend,
{ps, tags}], Alignment -> {Left, Right}]}]],
ButtonBar[{"Dim All" :> (Clear[op]; op[_] = Dynamic[dim]),
"Darken All" :> (Clear[op]; op[_] = 1)}],
Slider[Dynamic[dim], {0, 1}]}]];

DynamicLegend[
Plot[{
Tooltip[x^2, TraditionalForm[x^2]],
Tooltip[x^3, TraditionalForm[x^3]],
Tooltip[x^4, TraditionalForm[x^4]]}, {x, -3, 3}]]

dh

unread,
Nov 20, 2009, 6:41:48 AM11/20/09
to

PeterSF wrote:

> Specifically, we are given

> ["Test"] } ],

> Graphics[ { lineWidth,

> Line[{ {0,0}, {0,2}] } ] ]

> over that text;

> mouse is

> over that line.

>

>

> individually?

>

>

> me.

> Peter.

>

>

Hi Peter,

an event handler handles the whole region of the corresponding graphics

object. It can not distinguish between parts of it.

Daniel

Albert Retey

unread,
Nov 21, 2009, 3:38:03 AM11/21/09
to
PeterSF schrieb:

I think the following is doing what you described, except that I am
using the "d" key instead of the delete key. Unfortunatly there seems to
be no documented way to get ahold of the Delete key pressed events, or
am I missing something? I am using a combination of Mouseover, Dynamic
and Eventhandler:

DynamicModule[{
selected = None, highlighted = None,


lineWidth = Thin, textColor = Blue,

curve, text, line
},
curve = Plot[Sin[x], {x, 0, \[Pi]}] /. Line[points_] :> Mouseover[
Dynamic[highlighted = None; Line[points]],
Dynamic[highlighted = Hold[curve]; Line[points]]
];
text = Graphics[Mouseover[
Dynamic[{highlighted = None; Blue, Text["XXX", {1, 1}]}],
Dynamic[{highlighted = Hold[text]; Red, Text["XXX", {1, 1}]}]
]];
line = Graphics[Mouseover[
Dynamic[{highlighted = None; Thin, Line[{{0, 0}, {2, 1}}]}],
Dynamic[{highlighted = Hold[line]; Thick, Line[{{0, 0}, {2, 1}}]}]
]];
EventHandler[
Column[{
Dynamic[Show[curve, text, line]],
Dynamic[highlighted],
Dynamic[selected]
}],
{
"MouseClicked" :> (selected = highlighted),
{"KeyDown", "d"} :> (
Print["deleting: " <> ToString[selected]];
Set @@ Append[selected, Graphics[]]
)
},
PassEventsDown -> True
]]

hth,

albert

0 new messages