Skip to content

Commit 14f4621

Browse files
demvladhaslinghuis
andauthored
Added curves legend at multi-curves spectrum (#786)
* added legend at multi-spectrum chart * The buttons are placed in one row * Changed buttons height * resolved issue of wrong legend drawing code placement * Code style improvement Co-authored-by: Mark Haslinghuis <[email protected]> --------- Co-authored-by: Mark Haslinghuis <[email protected]>
1 parent d98cacf commit 14f4621

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ <h4>Workspace</h4>
474474
</div>
475475

476476
<div id="spectrumComparison" data-toggle="tooltip" title="Spectrum comparison">
477-
<button id="btn-spectrum-export" type="button" class="spectrum-comparasion-button" title="Export spectrum to CSV">Export</button>
478-
<button type="button" class="spectrum-comparasion-button" onclick="document.getElementById('btn-spectrum-import').click()" title="Import spectrum from CSV">Import</button>
477+
<button id="btn-spectrum-export" type="button" title="Export spectrum to CSV">Export</button>
478+
<button type="button" onclick="document.getElementById('btn-spectrum-import').click()" title="Import spectrum from CSV">Import</button>
479479
<input type="file" id="btn-spectrum-import" accept=".csv" style="display:none" multiple/>
480-
<button type="button" id="btn-spectrum-clear" class="spectrum-comparasion-button" title="Clear imported spectrums">Clear</button>
480+
<button type="button" id="btn-spectrum-clear" title="Clear imported spectrums">Clear</button>
481481
</div>
482482

483483
<div id="analyserResize" class="btn-nobg view-analyser-fullscreen" data-toggle="tooltip" title="Zoom Analyser Window">

src/css/main.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ html.has-analyser-fullscreen.has-analyser
647647
border: gray;
648648
border-style: solid;
649649
border-width: 1px;
650+
display: flex;
650651
}
651652

652653
.analyser #spectrumComparison select {
@@ -655,9 +656,11 @@ html.has-analyser-fullscreen.has-analyser
655656
color: black;
656657
}
657658

658-
.spectrum-comparasion-button {
659-
width: 100%;
659+
#spectrumComparison button {
660+
width: auto;
661+
height: auto;
660662
border-radius: 3px;
663+
float: left;
661664
}
662665

663666
.analyser input#analyserZoomX {

src/graph_spectrum.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,12 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
292292

293293
this.importSpectrumFromCSV = function(files) {
294294
const maxImportCount = 5;
295+
let importsLeft = maxImportCount - GraphSpectrumPlot.getImportedSpectrumCount();
296+
295297
for (const file of files) {
296-
if (GraphSpectrumPlot.getImportedSpectrumCount() == maxImportCount) {
298+
if (importsLeft-- == 0) {
297299
break;
298300
}
299-
300301
const reader = new FileReader();
301302
reader.onload = function (e) {
302303
try {
@@ -316,7 +317,7 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
316317
};
317318
});
318319

319-
GraphSpectrumPlot.addImportedSpectrumData(spectrumData);
320+
GraphSpectrumPlot.addImportedSpectrumData(spectrumData, file.name);
320321
} catch (e) {
321322
alert('Spectrum data import error: ' + e.message);
322323
return;
@@ -330,7 +331,7 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
330331
} catch (e) {
331332
console.log(`Failed to create analyser... error: ${e}`);
332333
}
333-
334+
334335
this.clearImportedSpectrums = function() {
335336
GraphSpectrumPlot.clearImportedSpectrums();
336337
};

src/graph_spectrum_plot.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ GraphSpectrumPlot.getImportedSpectrumCount = function () {
9494
return this._importedSpectrumsData.length;
9595
};
9696

97-
GraphSpectrumPlot.addImportedSpectrumData = function (curvesData) {
98-
this._importedSpectrumsData.push(curvesData);
97+
GraphSpectrumPlot.addImportedSpectrumData = function (curvesData, name) {
98+
const curve = {
99+
points: curvesData,
100+
name: name,
101+
};
102+
this._importedSpectrumsData.push(curve);
99103
this._invalidateCache();
100104
this._invalidateDataCache();
101105
GraphSpectrumPlot.draw();
@@ -171,8 +175,6 @@ GraphSpectrumPlot._drawGraph = function (canvasCtx) {
171175
};
172176

173177
GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
174-
canvasCtx.lineWidth = 1;
175-
176178
const HEIGHT = canvasCtx.canvas.height - MARGIN;
177179
const WIDTH = canvasCtx.canvas.width;
178180
const LEFT = canvasCtx.canvas.left;
@@ -181,6 +183,7 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
181183
const PLOTTED_BUFFER_LENGTH = this._fftData.fftLength / this._zoomX;
182184
const PLOTTED_BLACKBOX_RATE = this._fftData.blackBoxRate / this._zoomX;
183185

186+
canvasCtx.save();
184187
canvasCtx.translate(LEFT, TOP);
185188

186189
this._drawGradientBackground(canvasCtx, WIDTH, HEIGHT);
@@ -225,12 +228,14 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
225228
"Chocolate",
226229
];
227230

228-
for (let spectrumNum = 0; spectrumNum < this._importedSpectrumsData.length; spectrumNum++) {
229-
const curvesPonts = this._importedSpectrumsData[spectrumNum];
231+
const spectrumCount = this._importedSpectrumsData.length;
232+
for (let spectrumNum = 0; spectrumNum < spectrumCount; spectrumNum++) {
233+
const curvesPonts = this._importedSpectrumsData[spectrumNum].points;
230234
const pointsCount = curvesPonts.length;
231235
const scaleX = 2 * WIDTH / PLOTTED_BLACKBOX_RATE * this._zoomX;
232236

233237
canvasCtx.beginPath();
238+
canvasCtx.lineWidth = 1;
234239
canvasCtx.strokeStyle = curvesColors[spectrumNum];
235240
canvasCtx.moveTo(0, HEIGHT);
236241
const filterPointsCount = 50;
@@ -259,6 +264,31 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
259264
canvasCtx.stroke();
260265
}
261266

267+
//Legend draw
268+
if (this._isFullScreen) {
269+
const legendPosX = 0.84 * WIDTH,
270+
legendPosY = 0.6 * HEIGHT,
271+
rowHeight = 16,
272+
padding = 4,
273+
legendWidth = 0.13 * WIDTH + padding,
274+
legendHeight = spectrumCount * rowHeight + 3 * padding;
275+
276+
const legendArea = new Path2D();
277+
legendArea.rect(legendPosX, legendPosY, legendWidth, legendHeight);
278+
canvasCtx.clip(legendArea);
279+
canvasCtx.strokeStyle = "gray";
280+
canvasCtx.strokeRect(legendPosX, legendPosY, legendWidth, legendHeight);
281+
canvasCtx.font = `${this._drawingParams.fontSizeFrameLabelFullscreen}pt ${DEFAULT_FONT_FACE}`;
282+
canvasCtx.textAlign = "left";
283+
for (let row = 0; row < spectrumCount; row++) {
284+
const curvesName = this._importedSpectrumsData[row].name.split('.')[0];
285+
const Y = legendPosY + padding + rowHeight * (row + 1);
286+
canvasCtx.strokeStyle = curvesColors[row];
287+
canvasCtx.strokeText(curvesName, legendPosX + padding, Y);
288+
}
289+
}
290+
canvasCtx.restore();
291+
262292
this._drawAxisLabel(
263293
canvasCtx,
264294
this._fftData.fieldName,

0 commit comments

Comments
 (0)