5
5
const lcjs = require ( '@arction/lcjs' )
6
6
7
7
// Extract required parts from LightningChartJS.
8
- const {
9
- lightningChart,
10
- PalettedFill,
11
- LUT ,
12
- emptyFill,
13
- emptyLine,
14
- AxisScrollStrategies,
15
- AxisTickStrategies,
16
- ColorHSV,
17
- synchronizeAxisIntervals,
18
- regularColorSteps,
19
- Themes,
20
- } = lcjs
8
+ const { lightningChart, PalettedFill, LUT , emptyLine, AxisScrollStrategies, AxisTickStrategies, regularColorSteps, Themes } = lcjs
21
9
22
10
const AudioContext = window . AudioContext || window . webkitAudioContext
23
11
// Create a new audio context,
@@ -182,7 +170,6 @@ const remapDataToTwoDimensionalMatrix = (data, strideSize, tickCount) => {
182
170
183
171
/**
184
172
* Create a chart for a channel
185
- * @param {lcjs.Dashboard } dashboard Dashboard to create the chart in
186
173
* @param {number } channelIndex Current channel index
187
174
* @param {number } rows Data row count
188
175
* @param {number } columns Data column count
@@ -191,17 +178,7 @@ const remapDataToTwoDimensionalMatrix = (data, strideSize, tickCount) => {
191
178
* @param {number } minDecibels dB amount that matches value 0 in data (Uint8).
192
179
* @param {number } maxDecibels dB amount that matches value 255 in data (Uint8).
193
180
*/
194
- const createChannel = ( dashboard , channelIndex , rows , columns , maxFreq , duration , minDecibels , maxDecibels ) => {
195
- // Create a new chart in a specified row
196
- const chart = dashboard
197
- . createChartXY ( {
198
- columnIndex : 0 ,
199
- columnSpan : 1 ,
200
- rowIndex : channelIndex ,
201
- rowSpan : 1 ,
202
- } )
203
- // Hide the chart title
204
- . setTitleFillStyle ( emptyFill )
181
+ const createChannel = ( chart , channelIndex , rows , columns , maxFreq , duration , minDecibels , maxDecibels ) => {
205
182
const theme = chart . getTheme ( )
206
183
207
184
// Define function that maps Uint8 [0, 255] to Decibels.
@@ -218,9 +195,11 @@ const createChannel = (dashboard, channelIndex, rows, columns, maxFreq, duration
218
195
// Use half of the fft data range
219
196
y : Math . ceil ( maxFreq / 2 ) ,
220
197
}
198
+ const yAxis = chart . addAxisY ( { iStack : 1 - channelIndex } ) . setMargins ( channelIndex < 1 ? 15 : 0 , channelIndex > 0 ? 15 : 0 )
221
199
// Create the series
222
200
const series = chart
223
201
. addHeatmapGridSeries ( {
202
+ yAxis,
224
203
// Data columns, defines horizontal resolution
225
204
columns : columns ,
226
205
// Use half of the fft data range
@@ -254,23 +233,14 @@ const createChannel = (dashboard, channelIndex, rows, columns, maxFreq, duration
254
233
)
255
234
256
235
// Set default X axis settings
257
- series . axisX
258
- . setInterval ( { start : start . x , end : end . x , stopAxisAfter : false } )
259
- . setTickStrategy ( AxisTickStrategies . Empty )
260
- . setTitleMargin ( 0 )
261
- . setScrollStrategy ( undefined )
262
- . setMouseInteractions ( false )
263
- // Set default chart settings
264
- chart . setPadding ( { left : 0 , top : 8 , right : 8 , bottom : 1 } ) . setMouseInteractions ( false )
265
- // Set default X axis settings
266
- series . axisY
236
+ yAxis
267
237
. setInterval ( { start : start . y , end : end . y , stopAxisAfter : false } )
268
238
. setTitle ( `Channel ${ channelIndex + 1 } (Hz)` )
269
239
. setScrollStrategy ( AxisScrollStrategies . fitting )
270
240
271
241
return {
272
- chart,
273
242
series,
243
+ yAxis,
274
244
}
275
245
}
276
246
@@ -279,25 +249,22 @@ const createChannel = (dashboard, channelIndex, rows, columns, maxFreq, duration
279
249
* @param {WaveFormData } data Data set to render
280
250
*/
281
251
const renderSpectrogram = async ( data ) => {
282
- // Create a dashboard with enough rows for the number of channels in data set
283
- // NOTE: Using `Dashboard` is no longer recommended for new applications. Find latest recommendations here: https://lightningchart.com/js-charts/docs/basic-topics/grouping-charts/
284
- const dashboard = lc
285
- . Dashboard ( {
252
+ const chart = lc
253
+ . ChartXY ( {
286
254
theme : Themes [ new URLSearchParams ( window . location . search ) . get ( 'theme' ) || 'darkGold' ] || undefined ,
287
- numberOfColumns : 1 ,
288
- numberOfRows : data . channels . length ,
289
255
} )
290
- // Hide the dashboard splitter
291
- . setSplitterStyle ( emptyLine )
292
-
293
- // Collection of created charts
294
- const charts = [ ]
295
-
256
+ . setTitle ( 'Spectrogram chart 2 channels' )
257
+ chart
258
+ . getDefaultAxisX ( )
259
+ . setTickStrategy ( AxisTickStrategies . Numeric )
260
+ . setScrollStrategy ( AxisScrollStrategies . fitting )
261
+ . setTitle ( `Duration (s)` )
262
+ chart . getDefaultAxisY ( ) . dispose ( )
296
263
// Create channels and set data for each channel
297
264
for ( let i = 0 ; i < data . channels . length ; i += 1 ) {
298
265
// Create a chart for the channel
299
266
const ch = createChannel (
300
- dashboard ,
267
+ chart ,
301
268
i ,
302
269
data . stride ,
303
270
data . tickCount ,
@@ -317,32 +284,17 @@ const renderSpectrogram = async (data) => {
317
284
iColumn : 0 ,
318
285
values : remappedData ,
319
286
} )
320
- // Add the created chart and series to collection
321
- charts . push ( ch )
322
287
}
323
288
324
- // Style to bottom most chart axis to use it as the common axis for each chart
325
- charts [ charts . length - 1 ] . series . axisX
326
- . setTickStrategy ( AxisTickStrategies . Numeric )
327
- . setScrollStrategy ( AxisScrollStrategies . fitting )
328
- . setTitle ( `Duration (s)` )
329
-
330
289
// Add LegendBox.
331
- const legend = dashboard
290
+ const legend = chart
332
291
. addLegendBox ( )
292
+ . add ( chart )
333
293
// Dispose example UI elements automatically if they take too much space. This is to avoid bad UI on mobile / etc. devices.
334
294
. setAutoDispose ( {
335
295
type : 'max-width' ,
336
296
maxWidth : 0.3 ,
337
297
} )
338
- . setPosition ( { x : 100 , y : 50 } )
339
- . setOrigin ( { x : 1 , y : 0 } )
340
- charts . forEach ( ( c ) => legend . add ( c . chart ) )
341
- // Link chart X axis scales
342
- const syncedAxes = charts . map ( ( chart ) => chart . series . axisX )
343
- synchronizeAxisIntervals ( ...syncedAxes )
344
-
345
- return dashboard
346
298
}
347
299
348
300
; ( async ( ) => {
@@ -360,7 +312,7 @@ const renderSpectrogram = async (data) => {
360
312
// Process the loaded wave form to prepare it for being added to the chart
361
313
const processed = await processWaveForm ( waveform )
362
314
// Create a dashboard from the processed waveform data
363
- const dashboard = renderSpectrogram ( processed )
315
+ renderSpectrogram ( processed )
364
316
}
365
317
// Check if audio context was started
366
318
if ( audioCtx . state === 'suspended' ) {
0 commit comments