3
$\begingroup$

Hi Dynamic Noob here :)

I have written the following piece of code:

now:=CurrentDate["Minute"];
DynamicModule[{
    intervals={{DateObject[{2022,4,18,5,5},"Minute"],DateObject[{2022,4,18,20,6},"Minute"]},DateObject[{2022,4,19,5,4},"Minute"]},
    timeMarkers=KeyValueMap[Labeled[#2,#1]&,<|"One"->DateObject[{2022,4,18,6,31},"Minute"],"Two"->DateObject[{2022,4,18,13,18},"Minute"],"Three"->DateObject[{2022,4,18,17,4},"Minute"],"Four"->DateObject[{2022,4,18,21,32},"Minute"],"Five"->DateObject[{2022,4,19,1,17},"Minute"]|>],
    plot
},
    EchoLabel["Timeline:"]@Dynamic[
        FinishDynamic[];
        plot=TimelinePlot[{DateInterval/@intervals,timeMarkers,{Labeled[now,"You are here!"]}},Filling->Below];
        Style[plot,DynamicUpdating->True],
        TrackedSymbols->{},
        UpdateInterval->60
    ];
    "some other code with more dynamics"
]

But for some reason after updating the first two items in the TimelinePlot disappear. How do I fix this? and can someone give me an explanation of the problem as well.

Output before update:

enter image description here

Output after update:

enter image description here

$\endgroup$
2
  • $\begingroup$ What is the code that triggers an update? $\endgroup$
    – Jason B.
    Apr 18, 2022 at 17:41
  • $\begingroup$ @JasonB. There is no trigger, see the option TrackedSymbols->{}. The update happens periodically with UpdateInterval->60, i.e. every one minute which is exactly the granularity being used for the DateObjects in the code. $\endgroup$
    – user13892
    Apr 18, 2022 at 18:04

1 Answer 1

0
$\begingroup$

I don't think you need DynamicModule for this. I may be missing something from your context, but I think all you need to do is generate the appropriate Dynamic expression. Ultimately, this is the expression that does the job (I've added a bit so we can see changes and I lowered the update interval so we don't need to wait so long):

Dynamic[
  TimelinePlot[
    {DateInterval/@intervals,timeMarkers,{Labeled[now,"You are here! "<>DateString[now]]}},Filling->Below],
  UpdateInterval->1,TrackedSymbols->{now}]

Now, if you don't want the dependencies to be global, you can use a With:

With[
  {intervals={{DateObject[{2022,4,18,5,5},"Minute"],DateObject[{2022,4,18,20,6},"Minute"]},DateObject[{2022,4,19,5,4},"Minute"]},
  timeMarkers=KeyValueMap[Labeled[#2,#1]&,<|"One"->DateObject[{2022,4,18,6,31},"Minute"],"Two"->DateObject[{2022,4,18,13,18},"Minute"],"Three"->DateObject[{2022,4,18,17,4},"Minute"],"Four"->DateObject[{2022,4,18,21,32},"Minute"],"Five"->DateObject[{2022,4,19,1,17},"Minute"]|>]},
  Dynamic[
    TimelinePlot[
      {DateInterval/@intervals,timeMarkers,{Labeled[now,"You are here! "<>DateString[now]]}},Filling->Below],
    UpdateInterval->1,TrackedSymbols->{now}]]
$\endgroup$

Your Answer

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

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