const lcjsTrader = require('@lightningchart/lcjs-trader')
const lcjs = require('@lightningchart/lcjs')
const { ColorRGBA } = lcjs
lcjsTrader.trader(TRADER_LICENSE).then(async (trader) => {
// Create a trading chart. Optionally, various chart settings can be provided.
const tradingChart = trader.tradingChart({ loadFromStorage: false })
tradingChart.showSearchbar(false)
await fetch(`${document.head.baseURI}examples/assets/0005/DemoData2.csv`).then((res) => res.text()).then((text) => {
tradingChart.loadCsvString(text, 'DemoData2.csv')
})
tradingChart.indicators().addVolume()
// Extract the current dataset.
const dataset = tradingChart.getData()
// Create heatmap based on each month.
let currentMonth = new Date(dataset[0][0]).getMonth()
let highs = []
let lows = []
let prevX = 0
for (let i = 0; i < dataset[0].length; i++) {
if (new Date(dataset[0][i]).getMonth() != currentMonth) {
highs = dataset[2].slice(prevX, i)
lows = dataset[3].slice(prevX, i)
createHeatmap(highs, lows, prevX, i)
currentMonth = new Date(dataset[0][i]).getMonth()
prevX = i
}
}
highs = dataset[2].slice(prevX)
lows = dataset[3].slice(prevX)
createHeatmap(highs, lows, prevX, dataset[0].length)
function createHeatmap(highValues, lowValues, startX, endX) {
// Create heatmap values based on desired logic.
const max = Math.max(...highValues)
const min = Math.floor(Math.min(...lowValues))
const step = 2
const stepCount = Math.ceil((max - min) / step)
const nodeValues = []
for (let i = 0; i < stepCount; i++) {
nodeValues[i] = 0
}
for (let i = 0; i < highValues.length; i++) {
for (let j = 0; j < stepCount; j++) {
if (min + (j + 1) * step >= lowValues[i] && min + j * step <= highValues[i]) {
nodeValues[j]++
}
}
}
const heatmapValues = [nodeValues, nodeValues]
const highestNode = Math.max(...nodeValues)
// Add the heatmap series.
const heatmap = tradingChart.addHeatmap(startX, min, endX, max, heatmapValues)
// Modify heatmap color steps.
heatmap.setPalette([
{ value: 0, color: ColorRGBA(0, 0, 0, 0) },
{ value: highestNode * 0.25, color: ColorRGBA(0, 50, 255, 30) },
{ value: highestNode * 0.5, color: ColorRGBA(0, 255, 50, 30) },
{ value: highestNode * 0.75, color: ColorRGBA(255, 255, 50, 30) },
{ value: highestNode, color: ColorRGBA(255, 50, 50, 30) }
])
}
})
Heatmap in Technical Analysis Chart - Editor
Example showing heatmap usage in Technical Analysis Chart. Heatmap allows visualizing a two-dimensional dataset as a rectangular colored grid. Colors and their respective values can be modified. Note that heatmaps can only be added in code.