|
1 | 1 | // Internal Imports
|
| 2 | +import { AxisPositions, Events, ScaleTypes } from "../interfaces"; |
2 | 3 | import { Service } from "./service";
|
3 |
| -import { Events } from "../interfaces"; |
4 | 4 | import { Tools } from "../tools";
|
5 | 5 |
|
6 | 6 | // D3 imports
|
7 | 7 | import { extent } from "d3-array";
|
8 | 8 |
|
9 | 9 | export class Zoom extends Service {
|
| 10 | + isZoomBarEnabled() { |
| 11 | + // CartesianScales service is only available in axis charts |
| 12 | + if (!this.services.cartesianScales) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + |
| 16 | + // @todo - need to update this if zoom bar in other position (bottom, left, right) is supported |
| 17 | + // check configuration |
| 18 | + if ( |
| 19 | + !Tools.getProperty( |
| 20 | + this.model.getOptions(), |
| 21 | + "zoomBar", |
| 22 | + "top", |
| 23 | + "enabled" |
| 24 | + ) |
| 25 | + ) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + // @todo - Zoom Bar only supports main axis at BOTTOM axis and time scale for now |
| 30 | + this.services.cartesianScales.findDomainAndRangeAxes(); // need to do this before getMainXAxisPosition() |
| 31 | + const mainXAxisPosition = this.services.cartesianScales.getMainXAxisPosition(); |
| 32 | + const mainXScaleType = Tools.getProperty( |
| 33 | + this.model.getOptions(), |
| 34 | + "axes", |
| 35 | + mainXAxisPosition, |
| 36 | + "scaleType" |
| 37 | + ); |
| 38 | + |
| 39 | + return ( |
| 40 | + mainXAxisPosition === AxisPositions.BOTTOM && |
| 41 | + mainXScaleType === ScaleTypes.TIME |
| 42 | + ); |
| 43 | + } |
| 44 | + |
10 | 45 | // get display data for zoom bar
|
11 | 46 | // basically it's sum of value grouped by time
|
12 | 47 | getZoomBarData() {
|
@@ -85,4 +120,36 @@ export class Zoom extends Service {
|
85 | 120 | "zoomRatio"
|
86 | 121 | );
|
87 | 122 | }
|
| 123 | + |
| 124 | + // filter out data not inside zoom domain |
| 125 | + // to get better range value for axis label |
| 126 | + filterDataForRangeAxis(displayData: object[], configs?: any) { |
| 127 | + const zoomDomain = this.model.get("zoomDomain"); |
| 128 | + const mergedConfigs = Object.assign( |
| 129 | + { stacked: false }, // default configs |
| 130 | + configs |
| 131 | + ); |
| 132 | + const shouldUpdateRangeAxis = Tools.getProperty( |
| 133 | + this.model.getOptions(), |
| 134 | + "zoomBar", |
| 135 | + "updateRangeAxis" |
| 136 | + ); |
| 137 | + if (this.isZoomBarEnabled() && shouldUpdateRangeAxis && zoomDomain) { |
| 138 | + const domainIdentifier = mergedConfigs.stacked |
| 139 | + ? "sharedStackKey" |
| 140 | + : this.services.cartesianScales.getDomainIdentifier(); |
| 141 | + const filteredData = displayData.filter( |
| 142 | + (datum) => |
| 143 | + new Date(datum[domainIdentifier]) >= zoomDomain[0] && |
| 144 | + new Date(datum[domainIdentifier]) <= zoomDomain[1] |
| 145 | + ); |
| 146 | + // if no data in zoom domain, use all data to get full range value |
| 147 | + // so only return filteredData if length > 0 |
| 148 | + if (filteredData.length > 0) { |
| 149 | + return filteredData; |
| 150 | + } |
| 151 | + } |
| 152 | + // return original data by default |
| 153 | + return displayData; |
| 154 | + } |
88 | 155 | }
|
0 commit comments