Skip to content

Commit

Permalink
fill are with respect to zero line fix tradingview#187 / tradingview#151
Browse files Browse the repository at this point in the history
  • Loading branch information
karanjitsingh committed Aug 26, 2019
1 parent dd7fb36 commit 74cb84d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
25 changes: 16 additions & 9 deletions src/model/price-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class PriceScale {
return this._logicalToCoordinate(price, baseValue, keepItFloat);
}

public pointsArrayToCoordinates<T extends PricedValue>(points: T[], baseValue: number, visibleRange?: SeriesItemsIndexesRange): void {
public pointsArrayToCoordinates<T extends PricedValue>(points: T[], baseValue: number, visibleRange?: SeriesItemsIndexesRange): Coordinate {
this._makeSureItIsValid();
const bh = this._bottomMarginPx();
const range = ensureNotNull(this.priceRange());
Expand All @@ -364,6 +364,18 @@ export class PriceScale {
const toIndex = (visibleRange === undefined) ? points.length : visibleRange.to;

const transformFn = this._getCoordinateTransformer();

const getPoint = (price: BarPrice): Coordinate => {
let logical = price;
if (transformFn !== null) {
logical = transformFn(price, baseValue) as BarPrice;
}

const invCoordinate = bh + hmm * (logical - min);
const coordinate = isInverted ? invCoordinate : this._height - 1 - invCoordinate;
return Math.round(coordinate) as Coordinate;
};

for (let i = fromIndex; i < toIndex; i++) {
const point = points[i];
const price = point.price;
Expand All @@ -372,15 +384,10 @@ export class PriceScale {
continue;
}

let logical = price;
if (transformFn !== null) {
logical = transformFn(point.price, baseValue) as BarPrice;
}

const invCoordinate = bh + hmm * (logical - min);
const coordinate = isInverted ? invCoordinate : this._height - 1 - invCoordinate;
point.y = Math.round(coordinate) as Coordinate;
point.y = getPoint(price);
}

return getPoint(0 as BarPrice);
}

public barPricesToCoordinates<T extends BarPrices & BarCoordinates>(pricesList: T[], baseValue: number, visibleRange?: SeriesItemsIndexesRange): void {
Expand Down
11 changes: 7 additions & 4 deletions src/renderers/area-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface PaneRendererAreaData {
topColor: string;
bottomColor: string;
bottom: Coordinate;
zeroLine: Coordinate;

visibleRange: SeriesItemsIndexesRange | null;
}
Expand All @@ -32,6 +33,8 @@ export class PaneRendererArea implements IPaneRenderer {
return;
}

const baseline = Math.min(this._data.bottom, this._data.zeroLine);

ctx.save();

ctx.lineCap = 'square';
Expand All @@ -43,16 +46,16 @@ export class PaneRendererArea implements IPaneRenderer {
ctx.lineWidth = 1;

ctx.beginPath();
ctx.moveTo(this._data.items[this._data.visibleRange.from].x, this._data.bottom);
ctx.moveTo(this._data.items[this._data.visibleRange.from].x, baseline);
ctx.lineTo(this._data.items[this._data.visibleRange.from].x, this._data.items[this._data.visibleRange.from].y);

walkLine(ctx, this._data.items, this._data.lineType, this._data.visibleRange);

ctx.lineTo(this._data.items[this._data.visibleRange.to - 1].x, this._data.bottom);
ctx.lineTo(this._data.items[this._data.visibleRange.from].x, this._data.bottom);
ctx.lineTo(this._data.items[this._data.visibleRange.to - 1].x, baseline);
ctx.lineTo(this._data.items[this._data.visibleRange.from].x, baseline);
ctx.closePath();

const gradient = ctx.createLinearGradient(0, 0, 0, this._data.bottom);
const gradient = ctx.createLinearGradient(0, 0, 0, Math.min(baseline, this._data.zeroLine));
gradient.addColorStop(0, this._data.topColor);
gradient.addColorStop(1, this._data.bottomColor);

Expand Down
1 change: 1 addition & 0 deletions src/views/pane/area-pane-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class SeriesAreaPaneView extends LinePaneViewBase<'Area', LineItem> {
topColor: areaStyleProperties.topColor,
bottomColor: areaStyleProperties.bottomColor,
bottom: height as Coordinate,
zeroLine: this._itemsZeroLine,
visibleRange: this._itemsVisibleRange,
};

Expand Down
4 changes: 3 additions & 1 deletion src/views/pane/line-pane-view-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { TimeScale } from '../../model/time-scale';
import { SeriesPaneViewBase } from './series-pane-view-base';

export abstract class LinePaneViewBase<TSeriesType extends 'Line' | 'Area', ItemType extends PricedValue & TimedValue> extends SeriesPaneViewBase<TSeriesType, ItemType> {
protected _itemsZeroLine: Coordinate = NaN as Coordinate;

protected constructor(series: Series<TSeriesType>, model: ChartModel) {
super(series, model, true);
}

protected _convertToCoordinates(priceScale: PriceScale, timeScale: TimeScale, firstValue: number): void {
timeScale.indexesToCoordinates(this._items, undefinedIfNull(this._itemsVisibleRange));
priceScale.pointsArrayToCoordinates(this._items, firstValue, undefinedIfNull(this._itemsVisibleRange));
this._itemsZeroLine = priceScale.pointsArrayToCoordinates(this._items, firstValue, undefinedIfNull(this._itemsVisibleRange));
}

protected abstract _createRawItem(time: TimePointIndex, price: BarPrice, colorer: SeriesBarColorer): ItemType;
Expand Down

0 comments on commit 74cb84d

Please sign in to comment.