Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted vis_type_tagcloud to Typescript #63592 fix #64747

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9d24ed5
Added missing d3-cloud type to devDependencies
arjunvkurup Apr 27, 2020
d3c488c
Converted to TypeScript issue #63592 fixed
arjunvkurup Apr 27, 2020
dd3e136
Converted label.js to TypeScript issue #63592 fixed
arjunvkurup Apr 27, 2020
d493f37
Converted tag_cloud.js to TypeScript issue #63592 fixed
arjunvkurup Apr 27, 2020
b422ebc
Added types supporting TagCloud; issue #63592 fixed
arjunvkurup Apr 27, 2020
3fee65e
Converted tag_cloud_visualization to TypeScript issue #63592 fixed
arjunvkurup Apr 27, 2020
e4d40eb
Removed feedback_message.js for conversion: issue #63592 fixed
arjunvkurup Apr 27, 2020
0261434
Added missing d3-cloud type to devDependencies: issue #63592 fix
arjunvkurup Apr 27, 2020
6dd6581
Removed label.js for conversion: issue #63592 fix
arjunvkurup Apr 27, 2020
dfdce0e
Removed tag_cloud.js for conversion: issue #63592 fix
arjunvkurup Apr 27, 2020
ae36e53
Removed tag_cloud_visualization.js for conversion: issue #63592 fix
arjunvkurup Apr 27, 2020
357a548
Updated types.ts for conversion: issue #63592 fix
arjunvkurup Apr 27, 2020
2e7a296
Added type for D3Scaling Function issue #63592 fix
arjunvkurup Apr 29, 2020
37c4b3e
Fixed errors while TypeScript conversion issue #63592 fix
arjunvkurup Apr 29, 2020
c16389f
Added type for D3Scaling Function issue #63592 fix
arjunvkurup Apr 29, 2020
07b5309
removed any types form tag_cloud
arjunvkurup May 4, 2020
bab2ce9
updated types in tag_cloud_visualization issue #63592 fix
arjunvkurup May 4, 2020
0342efd
updated tag_cloud with strictly typed issue: 65839 fix
arjunvkurup Jun 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
"@types/classnames": "^2.2.9",
"@types/color": "^3.0.0",
"@types/d3": "^3.5.43",
"@types/d3-cloud": "^1.2.3",
"@types/dedent": "^0.7.0",
"@types/deep-freeze-strict": "^1.1.0",
"@types/delete-empty": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import React, { Component, Fragment } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiIconTip } from '@elastic/eui';
import { FeedbackMessageComponentState } from '../types';

export class FeedbackMessage extends Component {
constructor() {
super();
export class FeedbackMessage extends Component<{}, FeedbackMessageComponentState> {
constructor(props: {}) {
super(props);
this.state = { shouldShowTruncate: false, shouldShowIncomplete: false };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/

import React, { Component } from 'react';
import { LabelComponentState } from '../types';

export class Label extends Component {
constructor() {
super();
export class Label extends Component<{}, LabelComponentState> {
constructor(props: {}) {
super(props);
this.state = { label: '', shouldShowLabel: true };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,105 @@
import d3 from 'd3';
import d3TagCloud from 'd3-cloud';
import { EventEmitter } from 'events';
import { D3ScalingFunction } from '../types';

const ORIENTATIONS = {
const ORIENTATIONS: any = {
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
single: () => 0,
'right angled': tag => {
'right angled': (tag: any) => {
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
return hashWithinRange(tag.text, 2) * 90;
},
multiple: tag => {
return hashWithinRange(tag.text, 12) * 15 - 90; //fan out 12 * 15 degrees over top-right and bottom-right quadrant (=-90 deg offset)
multiple: (tag: any) => {
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
return hashWithinRange(tag.text, 12) * 15 - 90; // fan out 12 * 15 degrees over top-right and bottom-right quadrant (=-90 deg offset)
},
};
const D3_SCALING_FUNCTIONS = {
const D3_SCALING_FUNCTIONS: D3ScalingFunction = {
linear: () => d3.scale.linear(),
log: () => d3.scale.log(),
'square root': () => d3.scale.sqrt(),
};

export class TagCloud extends EventEmitter {
constructor(domNode, colorScale) {
_element: HTMLElement;
_d3SvgContainer: any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be typed strictly

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have issues with setting type on _d3SvgContainer

_svgGroup: any;
_size: [number, number];

_fontFamily: string;
_fontStyle: string;
_fontWeight: string;
_spiral: string;
_timeInterval: number;
_padding: number;

_orientation: string;
_minFontSize: number;
_maxFontSize: number;
_textScale: string;
_optionsAsString: string | null;

_words: any;
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved

_colorScale: string;
_setTimeoutId: any;
_pendingJob: any;
_layoutIsUpdating: boolean | null;
_allInViewBox: boolean;
_DOMisUpdating: boolean;

_cloudWidth: number;
_cloudHeight: number;

_completedJob: any;
tag: any;

STATUS = { COMPLETE: 0, INCOMPLETE: 1 };

constructor(domNode: HTMLElement, colorScale: string) {
super();

//DOM
// DOM
this._element = domNode;
this._d3SvgContainer = d3.select(this._element).append('svg');
this._svgGroup = this._d3SvgContainer.append('g');
this._size = [1, 1];
this.resize();

//SETTING (non-configurable)
// SETTING (non-configurable)
this._fontFamily = 'Open Sans, sans-serif';
this._fontStyle = 'normal';
this._fontWeight = 'normal';
this._spiral = 'archimedean'; //layout shape
this._timeInterval = 1000; //time allowed for layout algorithm
this._spiral = 'archimedean'; // layout shape
this._timeInterval = 1000; // time allowed for layout algorithm
this._padding = 5;

//OPTIONS
// OPTIONS
this._orientation = 'single';
this._minFontSize = 10;
this._maxFontSize = 36;
this._textScale = 'linear';
this._optionsAsString = null;

//DATA
// DATA
this._words = null;

//UTIL
// UTIL
this._colorScale = colorScale;
this._setTimeoutId = null;
this._pendingJob = null;
this._layoutIsUpdating = null;
this._allInViewBox = false;
this._DOMisUpdating = false;

this._cloudWidth = 0;
this._cloudHeight = 0;

this._completedJob = null;

this.STATUS.COMPLETE = 0;
this.STATUS.INCOMPLETE = 0;
}

setOptions(options) {
setOptions(options: any) {
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
if (JSON.stringify(options) === this._optionsAsString) {
return;
}
Expand Down Expand Up @@ -105,7 +149,7 @@ export class TagCloud extends EventEmitter {
}
}

setData(data) {
setData(data: string) {
this._words = data;
this._invalidate(false);
}
Expand All @@ -116,7 +160,7 @@ export class TagCloud extends EventEmitter {
}

getStatus() {
return this._allInViewBox ? TagCloud.STATUS.COMPLETE : TagCloud.STATUS.INCOMPLETE;
return this._allInViewBox ? this.STATUS.COMPLETE : this.STATUS.INCOMPLETE;
}

_updateContainerSize() {
Expand All @@ -140,7 +184,7 @@ export class TagCloud extends EventEmitter {
}

this._completedJob = null;
const job = await this._pickPendingJob();
const job: any = await this._pickPendingJob();
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
if (job.words.length) {
if (job.refreshLayout) {
await this._updateLayout(job);
Expand All @@ -159,7 +203,7 @@ export class TagCloud extends EventEmitter {
}

if (this._pendingJob) {
this._processPendingJob(); //pick up next job
this._processPendingJob(); // pick up next job
} else {
this._completedJob = job;
this.emit('renderComplete');
Expand All @@ -185,7 +229,7 @@ export class TagCloud extends EventEmitter {
this._DOMisUpdating = false;
}

async _updateDOM(job) {
async _updateDOM(job: any) {
arjunvkurup marked this conversation as resolved.
Show resolved Hide resolved
const canSkipDomUpdate = this._pendingJob || this._setTimeoutId;
if (canSkipDomUpdate) {
this._DOMisUpdating = false;
Expand Down Expand Up @@ -216,13 +260,13 @@ export class TagCloud extends EventEmitter {

const self = this;
enteringTags.on({
click: function(event) {
click(event: MouseEvent) {
self.emit('select', event);
},
mouseover: function() {
mouseover() {
d3.select(this).style('cursor', 'pointer');
},
mouseout: function() {
mouseout() {
d3.select(this).style('cursor', 'default');
},
});
Expand Down Expand Up @@ -288,7 +332,7 @@ export class TagCloud extends EventEmitter {
return {
refreshLayout: false,
size: this._size.slice(),
words: this._completedJob.words.map(tag => {
words: this._completedJob.words.map((tag: any) => {
return {
x: tag.x,
y: tag.y,
Expand All @@ -302,7 +346,7 @@ export class TagCloud extends EventEmitter {
};
}

_invalidate(keepLayout) {
_invalidate(keepLayout: any) {
if (!this._words) {
return;
}
Expand All @@ -314,7 +358,7 @@ export class TagCloud extends EventEmitter {
this._processPendingJob();
}

async _updateLayout(job) {
async _updateLayout(job: any) {
if (job.size[0] <= 0 || job.size[1] <= 0) {
// If either width or height isn't above 0 we don't relayout anything,
// since the d3-cloud will be stuck in an infinite loop otherwise.
Expand All @@ -329,7 +373,7 @@ export class TagCloud extends EventEmitter {
tagCloudLayoutGenerator.font(this._fontFamily);
tagCloudLayoutGenerator.fontStyle(this._fontStyle);
tagCloudLayoutGenerator.fontWeight(this._fontWeight);
tagCloudLayoutGenerator.fontSize(tag => mapSizeToFontSize(tag.value));
tagCloudLayoutGenerator.fontSize((tag: any) => mapSizeToFontSize(tag.value));
tagCloudLayoutGenerator.random(seed);
tagCloudLayoutGenerator.spiral(this._spiral);
tagCloudLayoutGenerator.words(job.words);
Expand All @@ -350,10 +394,10 @@ export class TagCloud extends EventEmitter {
* Returns debug info. For debugging only.
* @return {*}
*/
getDebugInfo() {
const debug = {};
getDebugInfo(): any {
const debug: any = {};
debug.positions = this._completedJob
? this._completedJob.words.map(tag => {
? this._completedJob.words.map((tag: any) => {
return {
displayText: tag.displayText,
rawText: tag.rawText || tag.text,
Expand All @@ -370,43 +414,41 @@ export class TagCloud extends EventEmitter {
return debug;
}

getFill(tag) {
getFill(tag: any): string {
return this._colorScale(tag.text);
}
}

TagCloud.STATUS = { COMPLETE: 0, INCOMPLETE: 1 };

function seed() {
return 0.5; //constant seed (not random) to ensure constant layouts for identical data
function seed(): number {
return 0.5; // constant seed (not random) to ensure constant layouts for identical data
}

function getText(word) {
function getText(word: any): string {
return word.rawText;
}

function getDisplayText(word) {
function getDisplayText(word: any): string {
return word.displayText;
}

function positionWord(xTranslate, yTranslate, word) {
function positionWord(xTranslate: any, yTranslate: any, word: any): string {
if (isNaN(word.x) || isNaN(word.y) || isNaN(word.rotate)) {
//move off-screen
// move off-screen
return `translate(${xTranslate * 3}, ${yTranslate * 3})rotate(0)`;
}

return `translate(${word.x + xTranslate}, ${word.y + yTranslate})rotate(${word.rotate})`;
}

function getValue(tag) {
function getValue(tag: any) {
return tag.value;
}

function getSizeInPixels(tag) {
function getSizeInPixels(tag: any): string {
return `${tag.size}px`;
}

function hashWithinRange(str, max) {
function hashWithinRange(str: string, max: any): number {
str = JSON.stringify(str);
let hash = 0;
for (const ch of str) {
Expand Down
Loading