Skip to content

Commit 371a3f2

Browse files
authored
chore(ui-charts): sdk updates (#15)
1 parent 5655fc4 commit 371a3f2

72 files changed

Lines changed: 1297 additions & 222 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/ui-charts/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Property, View } from '@nativescript/core';
22

33
export class UIChartsViewBase extends View {
4-
options: any;
4+
options: Record<string, any>;
55
}
66

7-
export const optionsProperty = new Property<UIChartsViewBase, {}>({
7+
export const optionsProperty = new Property<UIChartsViewBase, Record<string, any>>({
88
name: 'options',
99
defaultValue: {},
1010
affectsLayout: true,

packages/ui-charts/index.android.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class UIChartsView extends UIChartsViewBase {
1313
super.onLoaded();
1414
this.customLayoutChangeListener = new android.view.View.OnLayoutChangeListener({
1515
onLayoutChange: (v) => {
16+
const nativeView = <com.highsoft.highcharts.core.HIChartView>this.nativeView;
1617
const w = this.nativeView.owner.get();
1718
if (w && this.nativeView.getOptions()) {
1819
const newWidth = w.getActualSize().width;
@@ -22,9 +23,9 @@ export class UIChartsView extends UIChartsViewBase {
2223
// condition detected where android chart won't resize above this height,
2324
// dont attempt resize to avoid chart being cut off at the bottom
2425
} else if (this.chartHeight !== newHeight) {
25-
if (this.nativeView.getOptions().getChart()) {
26-
this.nativeView.getOptions().getChart().setHeight(new java.lang.Long(newHeight));
27-
this.nativeView.getOptions().getChart().setWidth(new java.lang.Long(newWidth));
26+
if (nativeView.getOptions().getChart()) {
27+
nativeView.getOptions().getChart().setHeight(java.lang.Long.valueOf(newHeight));
28+
nativeView.getOptions().getChart().setWidth(java.lang.Long.valueOf(newWidth));
2829
}
2930
this.chartHeight = newHeight;
3031
this.chartWidth = newWidth;

packages/ui-charts/index.ios.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { langHandler } from './options-handlers/lang/lang-handler';
44
import { Utils, ViewBase } from '@nativescript/core';
55

66
export class UIChartsView extends UIChartsViewBase {
7-
public _chartInitialized: boolean = false;
7+
public _chartInitialized = false;
88
private _delegate: HighchartsViewDelegateImpl;
99

1010
public createNativeView() {
1111
// const chartView = new HIChartView({ frame: CGRectMake(0, 0, 200, 200) }) as any;
12-
const chartView = HIChartView.alloc().initWithFrame(CGRectMake(0, 0, 200, 200));
12+
const chartView = HIChartView.new();
1313
// always retain delegate on owner class to ensure it doesn't inadvertently get garbage collected
1414
this._delegate = HighchartsViewDelegateImpl.initWithOwner(new WeakRef(this));
1515
chartView.delegate = this._delegate;
@@ -51,13 +51,23 @@ export class UIChartsView extends UIChartsViewBase {
5151
}
5252
}
5353

54+
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
55+
const nativeView = this.nativeView;
56+
if (nativeView) {
57+
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
58+
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
59+
this.setMeasuredDimension(width, height);
60+
}
61+
}
62+
5463
public setOptions(opts: any) {
5564
this.options = opts;
5665
const hiOptions = optionsHandler(this.options);
57-
if (this.nativeView) {
58-
this.nativeView.options = hiOptions;
66+
const nativeView = this.nativeView as HIChartView;
67+
if (nativeView) {
68+
nativeView.options = hiOptions;
5969
this._chartInitialized = true;
60-
this.nativeView.reload();
70+
nativeView.reload();
6171
}
6272
}
6373

@@ -71,13 +81,14 @@ export class UIChartsView extends UIChartsViewBase {
7181
public updateOptions(opts) {
7282
this.options = opts;
7383
const hiOptions = optionsHandler(this.options);
74-
if (this.nativeView) {
75-
this.nativeView.updateRedrawOneToOneAnimation(hiOptions, 1, 1, new HIAnimationOptionsObject());
84+
const nativeView = this.nativeView as HIChartView;
85+
if (nativeView) {
86+
nativeView.updateRedrawOneToOneAnimation(hiOptions, 1, 1, new HIAnimationOptionsObject());
7687
}
7788
}
7889

7990
setExtremes(newMin, newMax, xAxisIndex = 0) {
80-
const nativeview = this.nativeView;
91+
const nativeview = this.nativeView as HIChartView;
8192
if (nativeview) {
8293
const opts = nativeview.options;
8394
if (opts) {

packages/ui-charts/options-handlers/annotations/annotations-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export function annotationsHandler(annotationsOptions) {
1616
visible: 'number',
1717
zIndex: 'number',
1818
};
19-
if (annotationsOptions instanceof Array) {
20-
var annotationsArray = [];
21-
for (var i = 0; i < annotationsOptions.length; i++) {
19+
if (Array.isArray(annotationsOptions)) {
20+
const annotationsArray = [];
21+
for (let i = 0; i < annotationsOptions.length; i++) {
2222
annotationsArray.push(optionsBuilder(annotationsSchema, annotationsOptions[i], annotations));
2323
}
2424
return convertJSArrayToNative(annotationsArray);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { optionsBuilder } from '../helpers/helpers';
2+
import { isAndroid } from '@nativescript/core';
3+
4+
export function conditionHandler(conditionOptions) {
5+
const condition = isAndroid ? new com.highsoft.highcharts.common.hichartsclasses.HICondition() : new HICondition();
6+
7+
const conditionSchema = {
8+
callback: 'HIFunction',
9+
maxHeight: 'number',
10+
maxWidth: 'number',
11+
minHeight: 'number',
12+
minWidth: 'number',
13+
};
14+
15+
return optionsBuilder(conditionSchema, conditionOptions, condition);
16+
}

packages/ui-charts/options-handlers/function/function-handler.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,59 @@ import { toStringArray } from '../helpers/helpers';
2626

2727
// With event?
2828
export function functionHandler(functionOptions) {
29+
let func = functionOptions;
30+
31+
if (typeof func === 'string') {
32+
try {
33+
func = eval(`function func() { (${functionOptions})() }`);
34+
// eslint-disable-next-line no-empty
35+
} catch (error) {
36+
console.log('Error in functionHandler eval: ', error);
37+
}
38+
}
39+
2940
if (isAndroid) {
3041
return new com.highsoft.highcharts.core.HIFunction(
3142
new java.lang.Runnable({
32-
run: functionOptions,
43+
run() {
44+
func?.();
45+
},
3346
})
3447
);
3548
}
3649

37-
return new HIFunction({ closure: (event) => functionOptions(event) });
50+
return new HIFunction({
51+
closure: (event) => {
52+
func?.(event);
53+
},
54+
});
3855
}
3956

4057
export function dataPointFunctionHandler(functionOptions) {
58+
let func = functionOptions;
59+
60+
if (typeof func === 'string') {
61+
try {
62+
func = eval(`function func() { (${functionOptions})() }`);
63+
// eslint-disable-next-line no-empty
64+
} catch (error) {
65+
console.log('Error in functionHandler eval: ', error);
66+
}
67+
}
68+
4169
if (isAndroid) {
4270
return new com.highsoft.highcharts.core.HIFunction(
4371
new java.lang.Runnable({
44-
run: functionOptions,
72+
run() {
73+
func?.();
74+
},
4575
})
4676
);
4777
}
4878

49-
return new HIFunction({ closure: (event) => functionOptions(event) });
79+
return new HIFunction({
80+
closure: (event) => {
81+
func?.(event);
82+
},
83+
});
5084
}

packages/ui-charts/options-handlers/helpers/_helpers.common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ import { wordcloudHandler } from '../series/wordcloud/wordcloud-handler';
105105
import { xrangeHandler } from '../series/xrange/xrange-handler';
106106
import { selectHandler } from '../select/select-handler';
107107
import { inactiveHandler } from '../inactive/inactive-handler';
108+
import { responsiveHandler } from '../responsive/responsive-handler';
109+
import { rulesHandler } from '../rules/rules-handler';
110+
import { conditionHandler } from '../condition/conditionHandler';
108111

109112
const seriesHandlers = {
110113
HIArea: (options) => areaHandler(options),
@@ -170,6 +173,7 @@ export const typesMap = {
170173
HIButtonOptions: (options) => buttonOptionsHandler(options),
171174
// 'HICaption': (options) => captionHandler(options),
172175
HIChart: (options) => chartHandler(options),
176+
HICondition: (options) => conditionHandler(options),
173177
HICredits: (options) => creditsHandler(options),
174178
HICSSObject: (options) => styleHandler(options),
175179
HIDateTimeLabelFormats: (options) => dateTimeLabelFormatsHandler(options),
@@ -204,8 +208,9 @@ export const typesMap = {
204208
HIPlotOptions: (options) => plotOptionsHandler(options),
205209
HIPlotBands: (options) => plotBandsHandler(options),
206210
HIPlotLines: (options) => plotLinesHandler(options),
207-
// 'HIResponsive': (options) => responsiveHandler(options),
211+
HIResponsive: (options) => responsiveHandler(options),
208212
HIResetZoomButton: (options) => resetZoomButtonHandler(options),
213+
HIRules: (options) => rulesHandler(options),
209214
HIScrollablePlotArea: (options) => scrollablePlotAreaHandler(options),
210215
HISeries: (options) => seriesHandler(options),
211216
HIShapes: (options) => shapesHandler(options),

packages/ui-charts/options-handlers/helpers/helpers.android.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Color } from '@nativescript/core';
22
import { typesMap as _typesMap } from './_helpers.common';
3+
import { dataSerialize, numberHasDecimals } from '@nativescript/core/utils';
34

45
const typesMap = Object.assign({}, _typesMap, {
56
number: (options) => fromJSToNativePrimitive(options),
67
boolean: (options) => fromJSToNativePrimitive(options),
78
Array: (options) => convertJSArrayToNative(options),
89
LinkedList: (options) => toLinkedList(options),
910
HIColor: (options) => toHIColor(options),
11+
object: (options) => dataSerialize(options),
1012
});
1113

1214
export function convertJSArrayToNative(jsArray: Array<any>): java.util.ArrayList<any> {
@@ -19,25 +21,37 @@ export function convertJSArrayToNative(jsArray: Array<any>): java.util.ArrayList
1921
}
2022

2123
export function fromJSToNativePrimitive(value: any): any {
24+
if (value === null) return null;
25+
if (value === undefined) return undefined;
2226
if (typeof value === 'boolean' || value === 'false' || value === 'true') return new java.lang.Boolean(value);
23-
if (typeof value === 'string') return value;
27+
if (typeof value === 'string') return new java.lang.String(value);
2428

25-
if (Number.isInteger(value)) {
26-
return new java.lang.Double(value);
27-
}
29+
if (typeof value === 'number' || Number.isInteger(value) || value instanceof Number) {
30+
if (numberHasDecimals(value)) {
31+
return new java.lang.Double(value);
32+
}
33+
if (Number.isInteger(value)) {
34+
return new java.lang.Long(value);
35+
}
2836

29-
if (!isNaN(Number(value)) && value !== null) {
30-
return new java.lang.Double(value.toString());
37+
if (!isNaN(Number(value)) && value !== null) {
38+
if (value.toString().includes('.')) {
39+
return new java.lang.Double(value.toString());
40+
}
41+
return new java.lang.Long(value.toString());
42+
}
3143
}
3244

3345
return value;
3446
}
3547

3648
export function toArrayList(arr, isNumber = false) {
37-
const arrayList = new java.util.ArrayList<any>();
38-
arr.forEach((item) => {
49+
const size = arr?.length ?? 0;
50+
const arrayList = new java.util.ArrayList<any>(size);
51+
for (let i = 0; i < size; i++) {
52+
const item = arr[i];
3953
arrayList.add(item);
40-
});
54+
}
4155
return arrayList;
4256
}
4357

@@ -79,7 +93,7 @@ export function colorToString(color: any) {
7993
}
8094

8195
export function toHIColor(color) {
82-
if (color instanceof Array) {
96+
if (Array.isArray(color)) {
8397
const colorArray = [];
8498
for (let i = 0; i < color.length; i++) {
8599
const c = color[i];

packages/ui-charts/options-handlers/helpers/helpers.ios.ts

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Color } from '@nativescript/core';
22
import { typesMap as _typesMap } from './_helpers.common';
3+
import { dataSerialize } from '@nativescript/core/utils';
34

45
const typesMap = Object.assign({}, _typesMap, {
56
number: (options) => fromJSToNativePrimitive(options),
67
boolean: (options) => fromJSToNativePrimitive(options),
78
string: (options) => fromJSToNativePrimitive(options),
89
Array: (options) => convertJSArrayToNative(options),
910
HIColor: (options) => toHIColor(options),
11+
object: (options) => dataSerialize(options),
1012
});
1113

1214
export function convertJSArrayToNative(array) {
@@ -43,10 +45,10 @@ export function colorToString(color: any) {
4345
}
4446

4547
export function toHIColor(color) {
46-
if (color instanceof Array) {
48+
if (Array.isArray(color)) {
4749
const colorArray = [];
4850
for (let i = 0; i < color.length; i++) {
49-
const c = color[i];
51+
let c = color[i];
5052

5153
if (c.radialGradient && c.stops) {
5254
const stops = c.stops.map((stop, index) => [index, colorToString(stop)]);
@@ -67,38 +69,70 @@ export function toHIColor(color) {
6769
})
6870
);
6971
} else {
72+
if (typeof c === 'string' && c.indexOf('#') === 0) {
73+
if (c.length === 4) {
74+
c = `#${c[1]}${c[1]}${c[2]}${c[2]}${c[3]}${c[3]}`;
75+
}
76+
}
77+
7078
const _c = new Color(c);
71-
colorArray.push(new HIColor(_c.ios) as any);
79+
colorArray.push(HIColor.alloc().initWithUIColor(_c.ios));
7280
}
7381
}
7482

7583
return convertJSArrayToNative(colorArray);
7684
} else {
7785
if (color.radialGradient && color.stops) {
78-
const stops = color.stops.map((stop, index) => [index, colorToString(stop)]);
86+
const stops = NSMutableArray.alloc().initWithCapacity(color.stops.length);
87+
88+
for (const stop of color.stops) {
89+
const item = NSMutableArray.alloc().initWithCapacity(2);
90+
item.addObject(NSNumber.numberWithDouble(stop[0]));
91+
item.addObject(colorToString(stop));
92+
stops.addObject(item);
93+
}
94+
7995
const g = color.radialGradient;
80-
return new HIColor({
81-
radialGradient: NSDictionary.dictionaryWithObjectsForKeys([g.cx, g.cy, g.r], ['cx', 'cy', 'r']),
82-
stops: stops,
83-
});
96+
const radialGradient = NSMutableDictionary.alloc().initWithCapacity(3);
97+
radialGradient.setObjectForKey(g.cx, 'cx');
98+
radialGradient.setObjectForKey(g.cy, 'cy');
99+
radialGradient.setObjectForKey(g.r, 'r');
100+
return HIColor.alloc().initWithRadialGradientStops(radialGradient, stops);
84101
} else if (color.linearGradient && color.stops) {
85-
const stops = color.stops.map((stop, index) => [index, colorToString(stop)]);
86-
const g = color.linearGradient;
87-
return new HIColor({
88-
linearGradient: NSDictionary.dictionaryWithObjectsForKeys([g.x1, g.y1, g.x2, g.y2], ['x1', 'y1', 'x2', 'y2']),
89-
stops: stops,
102+
const stops = NSMutableArray.alloc().initWithCapacity(color.stops.length);
103+
104+
color.stops.forEach((stop, index) => {
105+
const item = NSMutableArray.alloc().initWithCapacity(2);
106+
item.addObject(NSNumber.numberWithInt(index));
107+
item.addObject(colorToString(stop));
108+
stops.addObject(item);
90109
});
110+
const g = color.linearGradient;
111+
112+
const linearGradient = NSMutableDictionary.alloc().initWithCapacity(4);
113+
114+
linearGradient.setObjectForKey(g.x1, 'x1');
115+
linearGradient.setObjectForKey(g.y1, 'y1');
116+
linearGradient.setObjectForKey(g.x2, 'x2');
117+
linearGradient.setObjectForKey(g.y2, 'y2');
118+
119+
return HIColor.alloc().initWithLinearGradientStops(linearGradient, stops);
91120
} else {
121+
if (typeof color === 'string' && color.indexOf('#') === 0) {
122+
if (color.length === 4) {
123+
color = `#${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${color[3]}`;
124+
}
125+
return HIColor.alloc().initWithHexValue(color);
126+
}
92127
const c = new Color(color);
93-
return new HIColor(c.ios) as any;
128+
return HIColor.alloc().initWithUIColor(c.ios);
94129
}
95130
}
96131
}
97132

98133
export function optionsBuilder(schema, options, containerObject) {
99134
const schemaKeys = Object.keys(schema);
100135
const optionsKeys = Object.keys(options);
101-
102136
for (const schemaKey of schemaKeys) {
103137
if ((<any>optionsKeys).includes(schemaKey)) {
104138
if (typeof typesMap[schema[schemaKey]] === 'function') {

0 commit comments

Comments
 (0)