I have an app which parses data files on the clients browsers and displays a scatter chart. This is an example of a scatter chart with 10,000 data points:
This is an example of a scatter chart with 1.4 million data points:
My algorithm is crude and simple. I get the events in a correct format (eg if the canvas is 200px x 200px, and the the minimum and maximum values of the data are 0 and 262144, then the values (100000, 200000) would be plotted at (76, 47) Note: the 47 is 200-153 since with HTML Canvas its plotted from the top on the y axis, not bottom). Here's the Javascript:
getFormattedEvents(enrichedEvents).forEach(
(formattedEvent) => {
context.fillStyle = formattedEvent.color;
context.fillRect(formattedEvent[0], formattedEvent[1], 1, 1);
if (
formattedEvent[0] > width ||
formattedEvent[0] < 2 ||
formattedEvent[1] > height ||
formattedEvent[1] < 2
) {
pointsOutside.push([formattedEvent[0], formattedEvent[1]]);
pointsOutsideCanvasCount++;
}
}
);
You can see I also keep a count of any points that dont fall on the canvas (eg values outside of 0 to 262144 wont fall on the Canvas).
I want to do 2 things: Use 3 colors to show where the highest concentration of points is e.g. something like this:
And I also want to make the algorithm more efficient. Currently, if there are 1 million data points, I loop through 1 million times and plot on the canvas. So one pixel may get drawn on many many times.
Initially, I had an algorithm which checked the color of a point, and if it was already black (meaning already a point drawn there), I would make the color green. If it was green, I would make the color yellow. And if yellow, make the color orange (highest concentration). This worked fine when al the datasets were 10,000. But now some users have huge datasets of over 1 million - and now the scatter plot for these large datasets are all orange.
What's the best approach here?
I have created a Codepen: https://codepen.io/mark-kelly-the-looper/pen/VwQyVaE