7
$\begingroup$

After creating an association of time series as follows:

stocks = {"AAPL", "FB"};
prices = FinancialData[stocks, {{2019, 11, 1}, {2019, 11, 15}}];
stockPrices = AssociationThread[stocks, prices];

I plot with:

DateListPlot[Tooltip[stockPrices]]

Unfortunately I cannot figure out how to format the dates in the tooltip in a more conventional form, e.g. mm/dd/yy.

enter image description here

Also, how can one create tooltips with just the value, excluding the date. The documentation for Tooltip is pretty thin.

$\endgroup$
1
  • 1
    $\begingroup$ DateListPlot[Tooltip[stockPrices]] /. Tooltip[pt_, {date_, price_}] :> Tooltip[pt, {DateString[ DateList@date, {"Month", "/", "Day", "/", "YearShort"}], price}] $\endgroup$
    – Bob Hanlon
    Nov 17, 2019 at 19:18

2 Answers 2

7
$\begingroup$

Here's one way:

stocks = {"AAPL", "FB"};
prices = FinancialData[stocks, {{2019, 11, 1}, {2019, 11, 15}}];
stockPrices = AssociationThread[stocks, prices];
tooltip[expr : {date_, value_}] := Tooltip[expr, DateString[date]]

DateListPlot[
 (tooltip /@ Normal[#]) & /@ stockPrices
 ]

Tooltip demo

$\endgroup$
1
  • $\begingroup$ that works and it's very easy to extend to create tooltips with just the value. $\endgroup$
    – Lee
    Nov 17, 2019 at 18:30
2
$\begingroup$

An alternative procedure is to recognize that DateListPlot[] uses absolute time as the abscissa, and then construct a replacement rule using DateString[]. Using the same data as in C.E.'s answer:

DateListPlot[Tooltip[stockPrices]] /.
Tooltip[obj_, {at_, val_}] :> Tooltip[obj, DateString[at]]

Use the second argument of DateString[] if you want to modify the format used in the tooltips.

$\endgroup$
1
  • $\begingroup$ Yes, that knowledge is helpful. The combination of ReplaceAll and RuleDelayed looks quite powerful. $\endgroup$
    – Lee
    Nov 18, 2019 at 11:04

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.