/*
* LightningChartJS example that showcases visualization of large XY scatter chart.
*/
// Import LightningChartJS
const lcjs = require('@lightningchart/lcjs')
// Extract required parts from LightningChartJS.
const { lightningChart, PointShape, emptyLine, ColorCSS, SolidLine, SolidFill, Themes } = lcjs
// Create chart and series.
const chart = lightningChart()
.ChartXY({
legend: { visible: false },
// theme: Themes.darkGold
})
.setTitle('')
// Create point series for visualizing scatter points.
const pointSeries = chart.addPointSeries(
{ automaticColorIndex: 1 },
).setPointSize(1).setName('Scatter series').setPointStrokeStyle(emptyLine)
// Visualize confidence ellipse with polygon series.
// Note, routine for calculation of confidence ellipse coordinates from scatter data set is not currently included in LightningChart JS!
const polygonSeries = chart.addPolygonSeries().setCursorEnabled(false).setPointerEvents(false)
// Fetch example data from JSON asset.
fetch(
new URL(document.head.baseURI).origin + new URL(document.head.baseURI).pathname + 'examples/assets/0016/data-largeScatterChartXY.json',
)
.then((r) => r.json())
.then((data) => {
const { scatterPoints, confidenceEllipsePolygonCoords } = data
chart.setTitle(`Scatter chart (${(data.scatterPoints.length / 10 ** 6).toFixed(1)} million points) + confidence Ellipse`)
// Add data to series.
pointSeries.appendJSON(scatterPoints)
polygonSeries
.add(confidenceEllipsePolygonCoords)
.setFillStyle(new SolidFill({ color: ColorCSS('gray').setA(30) }))
.setStrokeStyle(
new SolidLine({
thickness: 2,
fillStyle: new SolidFill({ color: ColorCSS('white') }),
}),
)
})
// Example of separating data cursor from LCJS rendering. This can enable smooth cursor interactions even over extremely heavy series, such as massive point clouds
// Essentially the idea is to:
// - disable any functionality that results in chart re-rendering in normal auto cursor interactions (highlighting on hover basically)
// - set cursor mode to show pointed data point, instead of finding nearest data point
// - use custom cursor to display cursor in some other way that doesn't require chart to re-render
pointSeries.setHighlightOnHover(false)
chart.setCursorMode('show-pointed').setCustomCursor((event) => {
const { hit, mouseLocation } = event
if (hit) {
customResultTable.style.opacity = '1.0'
customResultTable.style.left = `${mouseLocation.clientX}px`
customResultTable.style.top = `${mouseLocation.clientY}px`
customResultTable.innerHTML = `${hit.series.getName()}<br/>X: ${hit.axisX.formatValue(hit.x)}<br/>Y: ${hit.axisY.formatValue(
hit.y,
)}`
} else {
customResultTable.style.opacity = '0.0'
}
})
const theme = chart.getTheme()
const customResultTable = document.createElement('div')
document.body.append(customResultTable)
customResultTable.style.position = 'absolute'
customResultTable.style.pointerEvents = 'none'
customResultTable.style.transition = 'opacity 0.5s'
customResultTable.style.backgroundColor = theme.isDark ? 'rgba(0,0,0,0.8)' : 'rgba(255,255,255,0.8)'
customResultTable.style.color = theme.isDark ? 'rgba(255,255,255)' : 'rgba(0,0,0)'
customResultTable.style.fontFamily = 'Segoe UI'
customResultTable.style.fontSize = '14px'
customResultTable.style.padding = '5px'
customResultTable.style.borderRadius = '3px'
customResultTable.style.boxSizing = 'border-box'
customResultTable.style.transform = 'translateY(-100%)'
This example showcases visualization of a Scatter chart with relatively large data set (1 million data points).
LightningChart JS has created a revolution with the possibilities of data visualization in web - previously Scatter chart visualization was realistically possible only to ranges of some hundred thousand data points and even then it required complicated developer effort to enable any zooming or panning interactions with satisfactory performance.
With LC JS a million points Scatter chart is loaded in less than a second and the result is unlike anything seen before - operations like zooming and panning are instantaneous.
Additionally, when compared to previous alternatives, the usage is ridiculously simple. With SVG/Canvas based drawing, users would have severe limitations with large data amounts as these technologies don't scale well.
Here's how simple the creation of a million points scatter chart is with LC JS:
const chart =lightningChart().ChartXY()const series = chart.addPointSeries().add(newArray(1000000).fill().map((_)=>({
x: Math.random(),
y: Math.random(),})),)
This takes about 100 milliseconds to load and will immediately be interactable with lightning fast reactions!
The maximum possible Scatter chart size scales well with used hardware, especially RAM (memory) and GPU (graphics card) - On average office PC, LC JS can handle more than hundred million data points.
This example also showcases a custom HTML based data cursor. The reason for this is that rendering a large scatter chart is extremely heavy. For performance reasons it is best to separate the data cursor rendering from the chart. This is very easy to do using the setCustomCursor API.
Additionally by selecting show-pointed cursor mode, you can get lightning fast auto cursor functionality no matter how massive your scatter data set is!