MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Zoomable ListPlot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91864] Zoomable ListPlot
  • From: "peter.steneteg at gmail.com" <peter.steneteg at gmail.com>
  • Date: Thu, 11 Sep 2008 06:12:23 -0400 (EDT)

Hi

I made a small function to produce a zoomable listplot, hold alt down
and draw a rect to zoom, shift click to return. I'm not very familiar
with the eventhandlers, maybe someone else can do this much better,
without redrawing the plot as many times? I would also like to be able
to turn data series on and off by clicking the legend, but I have no
idea on how to do that...

/Peter

PListPlot[fun_,
  opts : OptionsPattern[{Options[ListPlot], Legend -> {""},
     Frame -> True, ColorScheme -> 1}]] :=
 DynamicModule[{p1, p2, pr = Automatic, pt = {{0, 0}, {0, 0}},
   pl = {}, mylegend, is, style},
  mylegend[data_] := Framed[Grid[
     MapIndexed[{Item[
         Graphics[{ColorData[OptionValue[ColorScheme], First@#2],
           Thick, Line[{{0, 0}, {1, 0}}]}, AspectRatio -> .1,
          ImageSize -> 30], Alignment -> {Center, Center}],
        Item[Style[#1, FontSize -> 16, FontFamily -> "Helvetica"],
         Alignment -> {Left, Bottom}]} &, data]],
    Background -> RGBColor[1, 1, 0.99999]];
  is[x_] := {x, x/GoldenRatio};
  EventHandler[
   Dynamic[
    ListPlot[fun, PlotRange -> pr,
     Epilog -> {pl,
       Inset[mylegend[OptionValue[Legend]],
        ImageScaled[{1 - 0.02, 1 - 0.02}], {Right, Top}]},
     Evaluate@ FilterRules[{opts}, Options[ListPlot]],
     MaxPlotPoints -> ControlActive[1000, Infinity]],
    SynchronousUpdating -> Automatic],
   "MouseDown" :> (If[CurrentValue["AltKey"],
      p1 = MousePosition["Graphics"]];
     			If[CurrentValue["ShiftKey"], pr = Automatic];
     ),
   "MouseUp" :> (If[CurrentValue["AltKey"],
      p2 = MousePosition["Graphics"];
      pt[[1, 1]] = p1[[1]];
      pt[[1, 2]] = p2[[1]];
      pt[[2, 1]] = p1[[2]];
      pt[[2, 2]] = p2[[2]];
      pr = pt;
      pl = {};
      ]
     ),
   "MouseDragged" :> (If[CurrentValue["AltKey"],
       p2 = MousePosition["Graphics"];
       pt[[1, 1]] = p1[[1]];
       pt[[1, 2]] = p2[[1]];
       pt[[2, 1]] = p1[[2]];
       pt[[2, 2]] = p2[[2]];
       pl = {Line[{p1, {p2[[1]], p1[[2]]}, p2, {p1[[1]], p2[[2]]},
           p1}]};
       ];
     )
   ,
   PassEventsDown -> True
   ]
  ]


  • Prev by Date: Re: How I can fit data with a parametric equation?
  • Next by Date: Alternating sums of large numbers
  • Previous by thread: Problems with Frame in ListPlots with Legends
  • Next by thread: Re: Zoomable ListPlot