I am quite new to design software and especially graphs. So I am working on a full-stack app with a back-end built on FastAPI (python) et front-end on React.

I need to create 4 graphs on a single page on my React app. Each graph is a time-series graph (simple) but with 3 millions data points (every 3 seconds since a couple of years) with 2 others data points (weather and humidity). They are currently on 4 PSQL tables but probably migration to timescaleDB on the long-run.

Note that the user can only watch graph (zoom) but this is read-only.

I never worked with graph before and such large amount of data, and I am wondering:

How should I built my endpoints? Should I send 3 millions data-points on each fetch call?

I was thinking about something but I have no idea if it's a good practice or it would make things worst:

Each graph must be displayed completely by default (full year, so 30 millions data points because I have data every 3 seconds). So when there is no zoom on a specific period, I could just display less data point (use intervals) and have an API that like only fetch 1 point per day so instead of fetching 30 millions pointsby default, I would have 365 points. But in this case, if the user zoom, I guess things will get a bit more complex.

  • 1
    Your requirements are, put bluntly, silly. There are (at most) of the order of 4k pixels horizontally on a monitor, so 30 million points means you're plotting 10000 points per pixel which isn't helpful. Find a way to summarise your data. Commented Sep 14, 2023 at 10:28
  • @PhilipKendall: I think you misunderstood the requirements, there is nothing "silly" in having a graph based an 3 million points input data and display it in different levels of detail, depending on the zoom level.
    – Doc Brown
    Commented Sep 14, 2023 at 10:40
  • ... it would bei silly trying to display the 3 million points literally "all at once", but I am sure that's not what the OP meant here.
    – Doc Brown
    Commented Sep 14, 2023 at 11:01
  • @DocBrown, this is exactly what I meant "graph based an 3 million points input data and display it in different levels of detail, depending on the zoom level." Because if i understand correctly, it won't be a good option to directly load the 30 millions points on the first load? Commented Sep 14, 2023 at 13:13
  • For future reference it's better to use the term 'chart' for what you are doing here because 'graph' is a common term used in software that means something very different.
    – JimmyJames
    Commented Sep 14, 2023 at 17:50

2 Answers 2


I think you might be making this a more difficult than it is. You have M data points to show on a chart. You can only actually display N data points at a time. You simply need to divide your point into N intervals of size M/N and calculate single value for each interval.

Let's assume you want to display 365 data points in the default 1-year view. You need to find a way to translate 24x60x(60/3) points into one point. The most obvious way to do that is to calculate the arithmetic mean of the values for each day. There are other options such as the median value. There are really no limits on how you can solve for this. You simply need a function that takes a set of values and returns a single one.

When you zoom, the only thing that changes is the range of the data and perhaps how many intervals you display. A lot of that depends on your requirements for the chart's appearance.

As far as an API goes, you essentially need to know what the range is and how many intervals you need to slice it into. There are a few ways you can take that as parameters to your API:

  • An interval size, start point (or end point), and number of intervals
  • The start and end of the range and the number of intervals
  • The start and end of the range and the size of the intervals

I would avoid anything where you make the caller provide parameters that can be derived from other parameters. For example, don't ask for a range, interval size, and a number of intervals. It's pointless to make the caller calculate all of these and what are you going to do if the parameters are inconsistent?

I would probably start with something like this:

async def get_humidity(start: datetime, end: datetime, intervals: int):

You might find that a different way to pass things in makes more sense depending on your charting library.

One thing you might want to consider is returning a range or some non-scalar value for each aggregated data point. For example, instead of just returning the mean value, you could return a min and max tuple for interval and plot them as two lines on the graph.

  • That's perfect, this is a great solution! thanks Commented Sep 15, 2023 at 9:36
  • 1
    In addition as this is already a great answer: The data returned can be calculated on the spot but can also be pre-calculated in your backend and just stored in the database. That makes your logic for getting the results easier and more performant if you run into that point. Just simple cache tables like: humidity_per_month, humidity_per_week etc. As the data is old it is likely stale and could maybe be calculated once and shown many times. Interesting to read a bit more about data warehouses as well, likely too far for now but the concepts are interesting. Commented Sep 16, 2023 at 7:27

The usual approach for allowing different zoom levels for 2D graphics is to use a tree-like data structure (for example a quadtree), or for a one-dimensional time-series just a binary tree, where you store simplified versions of sub-intervals of the graph at the different nodes.

The tree level should correspond to the degree of simplification, so at the root level, you may reduce the whole graph with 30 million points to, lets say, not more than 1000 points (roughly, you can try other numbers, if you like). For the leaf nodes, you will need around 30000 of them each one containing a subintervals of ~1000 points. Here, the data is stored unsimplified. If you use a binary tree, the tree needs 16 levels, each one with an increasing level of detail of the graph. The simplification can be done, for example, the way you already suggested by yourself, by picking every 2nd point, every 4th, every 8th, and so on. Another approach is to use some curve fitting algorithm, which will be more effort, but presumably create nicer looking results.

Now, when zooming in or shifting the view, you will typically pick one tree level of which you display the data. Use the level where the screen window covers only a few, maybe 2 nodes / intervals of the whole time series. That is the data you need to fetch, which means you will never fetch more than 1000 points in one call.

The whole tree should be prepared and stored on the server. That works best when you graph does not change any more, otherwise you will have to update your precomputed tree from time to time. The api has to be designed accordingly, where the client tells the server the time interval it wants to display as well as the horizontal screen resolution, then the server can pick the most appropriate tree level and return the data which fits best.

  • I'm a little confused. From what I can tell from the question, this a one-dimensional time series. I don't see how a quad-tree helps.
    – JimmyJames
    Commented Sep 14, 2023 at 18:09
  • @JimmyJames: read my answer again, I wrote "for a time-series just a binary tree".
    – Doc Brown
    Commented Sep 14, 2023 at 20:02
  • But why do you need a binary tree? What's wrong with an array (or array of arrays)?
    – JimmyJames
    Commented Sep 14, 2023 at 20:05
  • @JimmyJames: an array of arrays leads just to a k-nary tree, which is fine, but k=2 is actually the most simple approach I can think of. Note my initial description is intentionally general, aiming at general 2D-zooming, that's why I mentioned a quadtree. For a time series, that approach can be even made simpler, of course.
    – Doc Brown
    Commented Sep 14, 2023 at 20:08
  • When I've built these kinds of visualizations, the charting tools basically need an array of values. I'm not seeing how a binary tree helps me digest an e.g., 100,000 value series, slurp 1000 values at a time into some function and output 100 values.
    – JimmyJames
    Commented Sep 14, 2023 at 20:14

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.