leeoniya / uPlot

📈 A small, fast chart for time series, lines, areas, ohlc & bars
MIT License
8.84k stars 385 forks source link

Highlight intervals (weekends, etc..) #119

Open CrashLaker opened 4 years ago

CrashLaker commented 4 years ago

Hi all,

Is there a way to highlight intervals along the X axis? e.g. weekends, holidays, etc. Like in: http://dygraphs.com/gallery/#g/highlighted-weekends

leeoniya commented 4 years ago

nothing out of the box, but it should be easy enough to author a plugin that does this. you have full access to all timestamps obviously, and you can use uPlot.tzDate() to get a timezone-adjusted date objects which you can use to isolate weekend ranges, then simply paint on u.ctx in the drawAxes hook if want it atop the grid (but below the series), or drawClear hook if you need it under the grid.

take a look at how https://github.com/leeoniya/uPlot/blob/master/demos/draw-hooks.html does everything.

would be a good plugin to author for the demos here ;)

leeoniya commented 4 years ago

copying from https://github.com/leeoniya/uPlot/pull/126#issuecomment-594074971:

yes, you have to use uPlot.tzDate. the real trick is not to iterate every datapoint in the scale range in search of weekends, because that would mean creating 60,000 Date instances when you have 30,000 datapoints. you have to find these weekends efficiently, like the axis ticks find breaks in months, days, hours without walking the data.