Commit 537afd47 authored by Vasily Belolapotkov's avatar Vasily Belolapotkov

optimize yViewRange calculation

parent e3841e82
......@@ -5,7 +5,6 @@ import {
IInspectorValue,
IInspectorValues,
IRange,
Timestamp,
} from './types';
import { adjustRange, getAdjustedYRange } from '../utils/state';
......@@ -97,14 +96,8 @@ export function makeChartState(chartData: IChartData): IChartState {
}
function getYViewRange(): IRange {
const { xScale, xOffset } = state;
const xViewStart = xTotalRange.start + xOffset * xTotalRange.distance;
const xViewEnd = xViewStart + xScale * xTotalRange.distance;
// TODO: performance can be optimized
const xIndexStart = findXStartIndex(xViewStart);
const xIndexEnd = Math.max(findXEndIndex(xViewEnd), xIndexStart);
const xIndexStart = findXViewStartIndex();
const xIndexEnd = findXViewEndIndex(xIndexStart);
return getYTotalRange(xIndexStart, xIndexEnd);
}
......@@ -114,19 +107,49 @@ export function makeChartState(chartData: IChartData): IChartState {
updateYViewRange();
}
function findXStartIndex(startTimestamp: Timestamp): number {
const index = xAxis.findIndex(timestamp => timestamp >= startTimestamp);
return Math.max(index, 0);
function findXViewStartIndex(): number {
const { xOffset, xTotalRange, xAxis } = state;
const preciseStart = xTotalRange.start + xOffset * xTotalRange.distance;
const maxIndex = xAxis.length - 1;
let index = Math.floor(maxIndex * xOffset);
let value = xAxis[index];
if (value < preciseStart) {
while (index < maxIndex && value < preciseStart) {
index = index + 1;
value = xAxis[index];
}
} else {
while (index > 0 && value >= preciseStart) {
index = index - 1;
value = xAxis[index];
}
}
return index;
}
function findXEndIndex(endTimestamp: Timestamp): number {
for (let i = xAxis.length - 1; i >= 0; i = i - 1) {
if (xAxis[i] <= endTimestamp) {
return i;
function findXViewEndIndex(startIndex: number): number {
const { xScale, xOffset, xTotalRange, xAxis } = state;
const preciseEnd =
xTotalRange.start + (xOffset + xScale) * xTotalRange.distance;
const maxIndex = xAxis.length - 1;
let index = Math.ceil(maxIndex * (xOffset + xScale));
let value = xAxis[index];
if (value < preciseEnd) {
while (index < maxIndex && value < preciseEnd) {
index = index + 1;
value = xAxis[index];
}
} else {
while (index > startIndex && value >= preciseEnd) {
index = index - 1;
value = xAxis[index];
}
}
return 0;
return index;
}
function toggleDataSetVisibility(dataSet: IDataSet): void {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment