Skip to content

Commit

Permalink
fix #304 loop twice for updating series
Browse files Browse the repository at this point in the history
  • Loading branch information
Tucsky committed May 12, 2023
1 parent 51d2fc1 commit c2a2d2d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/components/chart/IndicatorDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
<div class="d-flex">
<dropdown-button
:value="priceFormat"
:options="['price', 'volume']"
:options="['price', 'volume', 'percent']"
class="mr8 -outline form-control -arrow"
@input="setPriceFormat($event, precision)"
v-tippy
Expand Down
61 changes: 37 additions & 24 deletions src/components/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ export interface LoadedIndicator {
script: string
model: IndicatorTranspilationResult
adapter: IndicatorRealtimeAdapter
silentAdapter: IndicatorRealtimeAdapter
apis: IndicatorApi[]
}

export interface IndicatorTranspilationResult {
output: string
silentOutput: string
variables: IndicatorVariable[]
functions: IndicatorFunction[]
plots: IndicatorPlot[]
Expand Down Expand Up @@ -163,6 +161,7 @@ export interface Renderer {
interface RendererIndicatorData {
canRender: boolean
series: {
rendered?: boolean
time: number
value?: number
open?: number
Expand Down Expand Up @@ -684,7 +683,6 @@ export default class Chart {
script: indicatorSettings.script,
model: null,
adapter: null,
silentAdapter: null,
apis: []
}

Expand Down Expand Up @@ -846,9 +844,6 @@ export default class Chart {
// create function ready to calculate (& render) everything for this indicator
try {
indicator.adapter = this.serieBuilder.getAdapter(indicator.model.output)
indicator.silentAdapter = this.serieBuilder.getAdapter(
indicator.model.silentOutput
)
} catch (error) {
this.unbindIndicator(indicator, renderer)

Expand Down Expand Up @@ -1910,6 +1905,8 @@ export default class Chart {
updateBar(renderer: Renderer) {
this.preventPan()

const toUpdate = []

for (let i = 0; i < this.loadedIndicators.length; i++) {
if (this.loadedIndicators[i].options.visible === false) {
continue
Expand All @@ -1918,26 +1915,42 @@ export default class Chart {
const indicator = this.loadedIndicators[i]
const serieData = renderer.indicators[indicator.id]

this.loadedIndicators[i].adapter(
renderer,
serieData.functions,
serieData.variables,
indicator.apis,
indicator.options,
seriesUtils
)

if (serieData.canRender) {
this.loadedIndicators[i].adapter(
renderer,
serieData.functions,
serieData.variables,
indicator.apis,
indicator.options,
seriesUtils
)
} else {
this.loadedIndicators[i].silentAdapter(
renderer,
serieData.functions,
serieData.variables,
indicator.apis,
indicator.options,
seriesUtils
)
for (let j = 0; j < indicator.apis.length; j++) {
if (serieData.series[j] && !serieData.series[j].rendered) {
if (
(indicator.model.plots[j].type === 'line' &&
!serieData.series[j].value) ||
(indicator.model.plots[j].type === 'histogram' &&
!serieData.series[j].value) ||
((indicator.model.plots[j].type === 'cloudarea' ||
indicator.model.plots[j].type === 'brokenarea') &&
serieData.series[j].lowerValue === null) ||
(indicator.model.plots[j].type === 'histogram' &&
!serieData.series[j].value)
) {
continue
}

toUpdate.push([indicator.apis[j], serieData.series[j]])
}
}
}
}

for (let i = 0; i < toUpdate.length; i++) {
toUpdate[i][0].update(toUpdate[i][1])
toUpdate[i][1].rendered = true
}
}

/**
Expand Down Expand Up @@ -1968,7 +1981,7 @@ export default class Chart {
serieData.series = []

try {
indicator.silentAdapter(
indicator.adapter(
renderer,
serieData.functions,
serieData.variables,
Expand Down
54 changes: 1 addition & 53 deletions src/components/chart/serieBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
uniqueName
} from '@/utils/helpers'
import { plotTypesMap } from './options'
const SERIE_UPDATE_REGEX = /series\[[a-zA-Z0-9+\-$ ]+\]\.update\(/
const VARIABLES_VAR_NAME = 'vars'
const FUNCTIONS_VAR_NAME = 'fns'
const SERIE_TYPES = {
Expand Down Expand Up @@ -75,7 +74,6 @@ export default class SerieBuilder {

return {
output: result.output,
silentOutput: result.silentOutput,
functions: result.functions,
variables: result.variables,
references: result.references,
Expand Down Expand Up @@ -205,34 +203,6 @@ export default class SerieBuilder {
}
}

getSilentOutput(originalOutput) {
let silentOutput = originalOutput

let instructionMatch = null
let iterations = 0

do {
if ((instructionMatch = SERIE_UPDATE_REGEX.exec(silentOutput))) {
const openingParenthesisIndex =
instructionMatch.index + instructionMatch[0].length - 1
const closingParenthesisIndex = findClosingBracketMatchIndex(
silentOutput,
openingParenthesisIndex
)
silentOutput = silentOutput.replace(
instructionMatch[0] +
silentOutput.slice(
openingParenthesisIndex + 1,
closingParenthesisIndex + 1
),
1
)
}
} while (instructionMatch && ++iterations < 1000)

return silentOutput
}

/**
* parse variable, functions referenced sources and external indicators used in it
* @param input
Expand Down Expand Up @@ -264,11 +234,8 @@ export default class SerieBuilder {

output = this.formatOutput(output)

const silentOutput = this.getSilentOutput(output)

return {
output,
silentOutput,
functions,
variables,
plots,
Expand Down Expand Up @@ -491,26 +458,7 @@ export default class SerieBuilder {
}
}

// this line will paint the point
const serieUpdate = `series[${plots.length}].update(renderer.indicators['${this.indicatorId}'].series[${plots.length}])`

// put everything together
let finalInstruction = seriePoint + ','

if (serieType === 'histogram') {
// only update point if there is a value to avoid long histogram * base line * at zero
finalInstruction += pointVariable + '.value&&'
} else if (serieType === 'line') {
// prevent null
finalInstruction += pointVariable + '.value !== null&&'
} else if (serieType === 'cloudarea' || serieType === 'brokenarea') {
// prevent null
finalInstruction += pointVariable + '.lowerValue !== null&&'
}

finalInstruction += serieUpdate

output = output.replace(rawFunctionInstruction, finalInstruction)
output = output.replace(rawFunctionInstruction, seriePoint)

let id: string

Expand Down
4 changes: 2 additions & 2 deletions src/components/framework/picker/ColorPickerDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>
<div class="color-picker-dialog-controls">
<button
class="btn -text"
class="btn -text -small"
@click="switchFormat"
type="button"
title="Rotate"
Expand All @@ -70,7 +70,7 @@
@input="setColorFromProp($event)"
></editable>
<button
class="btn -text"
class="btn -text -small"
@click="submitColor"
type="button"
title="Save color"
Expand Down

0 comments on commit c2a2d2d

Please sign in to comment.