DateListPlot of multiple time series with data values and legend appearing as the cursor moves over ploted lines

4

4

This is very similar to my previous question. I want to use "DateListPlot" to visualize multiple time series (n) and see data values and legend as I move the cursor over plotted lines. To begin with, I have the following codes (preferable n > 2):

Stock1 = FinancialData["NVDA", "Close", {{2017, 1, 1}, Now}];
Stock2 = FinancialData["QQQ", "Close", {{2017, 1, 1}, Now}];
DateListPlot[{Stock1, Stock2},PlotLegends -> {"NVDA Price", "QQQ 
 Price"},PlotStyle -> {Red, Green}]

It produces the graph but does not show the data points as I move my mouse along the plotted lines. Any help would be greatly appreciated. Thank you in advance.

ramesh

Posted 2017-07-27T12:11:30.783

Reputation: 2 167

1

How about TradingChart: TradingChart[{"GE", {{2017, 1, 1}, Now}}]?

– kglr – 2017-07-27T13:23:44.243

Any improvement on my answer is most welcome. – ramesh – 2017-09-04T16:57:35.673

Answers

1

You may use a Dynamic Epilog to create a plot that displays the values via MousePosition.

plot tracker

I use Nearest to speed up finding the values in the series.

nearStock1 = Nearest[First@# -> # & /@ MapAt[AbsoluteTime, {All, 1}]@Stock1];
nearStock2 = Nearest[First@# -> # & /@ MapAt[AbsoluteTime, {All, 1}]@Stock2];

The plot's Epilog has a bit of formatting in it but it is nothing out of the ordinary.

DateListPlot[{Stock1, Stock2},
 PlotLegends -> {"NVDA Price", "QQQ Price"},
 PlotStyle -> {Red, Green},
 Epilog ->
  Dynamic@Module[{s1, s2},
    With[{pos = MousePosition[{"Graphics", DateListPlot}, {0, 0}]},
     s1 = First@nearStock1@First@pos;
     s2 = First@nearStock2@First@pos;
     {
      Thin, LightGray, InfiniteLine[pos, {0, 1}],
      PointSize[Large],
      Darker@Red, Point[s1],
      Darker@Green, Point[s2],
      Inset[
       Grid[
        MapAt[NumberForm[#, {5, 2}] &, {All, 2}]@
         MapAt[DateString[#, "ISODate"] &, {All, 1}]@{s1, s2},
        ItemStyle -> {{Automatic}, Darker /@ {Red, Green}},
        Alignment -> {{Automatic, Decimal}, {Automatic}}
        ],
       Scaled[{.02, .98}],
       Scaled[{0, 1}]
       ]
      }
     ]]
  ]

Hope this helps.

Edmund

Posted 2017-07-27T12:11:30.783

Reputation: 35 657

@ Edmund, thank you for your time and answer. I really appreciate it. – ramesh – 2017-11-06T03:47:12.160