8
$\begingroup$

I have multiple series and I am plotting using Mma's ListLinePlot function. But I am trying to implement a feature to my plot so that I can view legend and corresponding values of the series at a particular point of the plotted lines as I move my cursor over the lines.

Here is an example: Web Browser Market Share Trends

We can use the following example codes form Wolfram Documentation to be consistent.

ListLinePlot[Table[Accumulate[RandomReal[{-1, 1}, 500]], {3}], 
 Filling -> Axis, PlotLegends -> {"First", "Second", "Third"}]

To make it more clearer, I have uploaded the following two screen prints form http://www.w3counter.com/trends. enter image description here

enter image description here

Thank you all for making this site so useful to solve practical problems.

$\endgroup$
2
  • 1
    $\begingroup$ related: 9447, 5478 $\endgroup$
    – Kuba
    Nov 14, 2014 at 8:05
  • $\begingroup$ @ Kuba, thanks for your post especially 9447, somewhat related. I will try. But, this question is still open. $\endgroup$
    – ramesh
    Nov 14, 2014 at 17:06

1 Answer 1

9
$\begingroup$

Using code from the mentioned 9447, with some patches

data = Transpose@{{"First", "Second", "Third"}, Table[Accumulate[RandomReal[{-1, 1}, 500]], {3}]};

hover[data_] := 
 Module[{mouse}, 
  mouse = MousePosition[{"Graphics", Graphics}, {99999, 99999}];
  {Text[Style[Framed@Column[
       (Row[{#[[1]], ": ", #[[2, Min[Length@#[[2]], Max[1, Round[mouse[[1]]]]]]]}]) & /@ data],
     Background -> LightYellow, FontSize -> 16],
    mouse, {-1.1, 2}],

   PointSize[Large],
   Point[{Min[Length@#[[2]], 
        Max[1, Round[mouse[[1]]]]], #[[2, 
         Min[Length@#[[2]], Max[1, Round[mouse[[1]]]]]]]}] & /@ data
   }
  ]

ListLinePlot[#[[All, 2]], Epilog -> Dynamic@hover[#], PlotLegends -> #[[All, 1]]]& @ data

Plot display

There is some styling to do, but this could be useful as a starting point! I tried to be as verbose as possible... ugly but easier to poke around!

Lazy saturday update

A slightly prettier version that hop's on the Plot's styles and legends. Also better mirrors your example. And uses Clip!

addHover[plot_Legended] :=
 DynamicModule[{mouse, legend, data, newPlot},
  legend = 
   plot /. Legended[a_, Placed[b_, __]] :> List @@ b /. 
      HoldPattern[h : Except[LegendMarkers] -> _] :> Sequence[] /. 
     HoldPattern[Rule[_, b_]] -> List@b // Flatten[#, {{2}, {1}}] &;
  data = Cases[First@plot, Line[x__] -> x, Infinity];
  newPlot = plot;
  newPlot[[1, 2]] = Append[plot[[1, 2]], Epilog ->
     {Text[Style[Framed[Grid[{
           {Dynamic@Clip[Round@MousePosition["Graphics", {99999, 99999}][[1]], {1,Infinity}]},
           Sequence @@ MapThread[
             {Row@{Style[Dynamic@#2[[2]], #2[[1]]], ": ",                     
                 Text[Dynamic@#1[[Clip[Round@MousePosition["Graphics", {99999, 99999}][[1]], {1,Length@#1}]]][[2]]]}} &
             , {data, legend}]},
          Alignment -> Left,
          Spacings -> 0],
         FrameStyle -> LightBlue, RoundingRadius -> 10],
        Background -> Opacity[0.8, White], FontSize -> 12], 
       Dynamic@MousePosition["Graphics", {99999, 99999}], {-1.5, 0}],

      MapThread[
       {#2[[1]] /. a_ /; ColorQ@a :> Darker@a,
         PointSize[0.012],
         Point[Dynamic@#1[[Clip[Round@MousePosition["Graphics", {99999, 99999}][[1]], {1, Length@#1}]]]]} &,
       {data, legend}]
      }];
  newPlot
  ]

Now data remains unaltered!

data2 = Table[Accumulate[RandomReal[{-1, 1}, 500]], {3}]
ListLinePlot[data2, PlotLegends -> {"First", "Second", "Third"} , PlotStyle -> {Orange, Dashed, Thick}] // addHover

Nicer Plot

It will probably break if you try it with fancier plots, use DataRange or stare long enough.

There is still some important aspects missing, such as mirroring PlotMarkers, but I couldn't manage to avoid distortion when Showing or Epiloging anything that is not a Point.

$\endgroup$
2
  • 1
    $\begingroup$ Instead of Min/Max you can use Clip. +1. $\endgroup$
    – Kuba
    Nov 14, 2014 at 19:36
  • $\begingroup$ @ Aisamu, thank you so much for your time and answer. That's what I was looking for. Thank you again $\endgroup$
    – ramesh
    Nov 14, 2014 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.