|
| 1 | +import { AutoscaleInfo, DeepPartial, HistogramData, SeriesDataItemTypeMap, SingleValueData, Time, WhitespaceData, createChart } from 'lightweight-charts'; |
| 2 | +import { generateLineData } from '../../../sample-data'; |
| 3 | +import { PrettyHistogramSeries } from '../pretty-histogram-series'; |
| 4 | +import { PrettyHistogramSeriesOptions } from '../options'; |
| 5 | +import { PrettyHistogramData } from '../data'; |
| 6 | + |
| 7 | +const chart = ((window as unknown as any).chart = createChart('chart', { |
| 8 | + autoSize: true, |
| 9 | +})); |
| 10 | + |
| 11 | +const customSeriesView = new PrettyHistogramSeries(); |
| 12 | + |
| 13 | +let autoscaleToZero = true; |
| 14 | + |
| 15 | +const options: DeepPartial<PrettyHistogramSeriesOptions> = { |
| 16 | + autoscaleInfoProvider: (baseImplementation: () => AutoscaleInfo | null) => { |
| 17 | + const baseRes = baseImplementation(); |
| 18 | + if (!autoscaleToZero) { |
| 19 | + return baseRes; |
| 20 | + } |
| 21 | + if (!baseRes?.priceRange) { |
| 22 | + return { priceRange: { minValue: 0, maxValue: 0 } }; |
| 23 | + } |
| 24 | + const minValue = Math.min(baseRes.priceRange.minValue, 0); |
| 25 | + const maxValue = Math.max(baseRes.priceRange.maxValue, 0); |
| 26 | + return { ...baseRes, priceRange: { minValue, maxValue } }; |
| 27 | + }, |
| 28 | + radius: 6, |
| 29 | + widthPercent: 50, |
| 30 | +}; |
| 31 | + |
| 32 | +const myCustomSeries = chart.addCustomSeries(customSeriesView, options); |
| 33 | + |
| 34 | +const data: PrettyHistogramData<Time>[] = generateLineData(6); |
| 35 | +data.forEach((item: PrettyHistogramData<Time>, i: number) => { |
| 36 | + (item as HistogramData<Time>).color = (i % 2) ? '#6438D6' : undefined; |
| 37 | +}); |
| 38 | + |
| 39 | +myCustomSeries.setData(data); |
| 40 | + |
| 41 | +chart.timeScale().fitContent(); |
| 42 | + |
| 43 | +data.forEach((item: (SeriesDataItemTypeMap<Time>['Custom'] & SingleValueData<Time>), i: number) => { |
| 44 | + const element = document.getElementById(`bar_${i + 1}`) as HTMLInputElement; |
| 45 | + element.value = item.value.toFixed(2); |
| 46 | + element.onchange = () => { |
| 47 | + const newValue = parseFloat(element.value); |
| 48 | + if (!isNaN(newValue)) { |
| 49 | + item.value = newValue; |
| 50 | + myCustomSeries.setData(data); |
| 51 | + } |
| 52 | + }; |
| 53 | +}); |
| 54 | + |
| 55 | +const radiusElement = document.getElementById('radius') as HTMLInputElement; |
| 56 | +radiusElement.value = options.radius!.toString(); |
| 57 | +radiusElement.onchange = () => { |
| 58 | + const newRadius = parseFloat(radiusElement.value); |
| 59 | + if (newRadius >= 0) { |
| 60 | + options.radius = newRadius; |
| 61 | + myCustomSeries.applyOptions(options); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +const widthElement = document.getElementById('width') as HTMLInputElement; |
| 66 | +widthElement.value = options.widthPercent!.toString(); |
| 67 | +widthElement.onchange = () => { |
| 68 | + const newWidth = parseFloat(widthElement.value); |
| 69 | + if (newWidth >= 0 && newWidth <= 100) { |
| 70 | + options.widthPercent = newWidth; |
| 71 | + myCustomSeries.applyOptions(options); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +const zeroAutoscaleElement = document.getElementById('autoscaleToZero') as HTMLInputElement; |
| 76 | +zeroAutoscaleElement.checked = autoscaleToZero; |
| 77 | +zeroAutoscaleElement.onchange = () => { |
| 78 | + autoscaleToZero = zeroAutoscaleElement.checked; |
| 79 | + myCustomSeries.applyOptions(options); |
| 80 | +}; |
0 commit comments