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

refactor: cleanup wordcloud config, types and theming #1358

Merged
merged 4 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
'@typescript-eslint/no-unsafe-return': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/restrict-plus-operands': 0, // rule is broken
'@typescript-eslint/no-unsafe-call': 1,
'@typescript-eslint/no-unsafe-call': 0, // seems to have issues with default import types
'@typescript-eslint/unbound-method': 1,
'@typescript-eslint/no-redeclare': 'off', // we use to declare enum type and object with the same name
'@typescript-eslint/no-shadow': 'off', // we use shadow mostly within the canvas renderer function when we need a new context
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions integration/tests/wordcloud_stories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { TEMPLATES } from '../../storybook/stories/wordcloud/1_wordcloud.story';
import { common } from '../page_objects';

describe('Scales stories', () => {
it.each(TEMPLATES.filter((t) => t !== 'edit'))('should render %s wordcloud template', async (template) => {
await common.expectChartAtUrlToMatchScreenshot(
`http://localhost:9001/?path=/story/wordcloud-alpha--simple-wordcloud&knob-template=${template}`,
);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@types/color": "^3.0.1",
"@types/core-js": "^2.5.2",
"@types/d3-array": "^1.2.6",
"@types/d3-cloud": "^1.2.5",
"@types/d3-collection": "^1.0.8",
"@types/d3-color": "^1.2.2",
"@types/d3-interpolate": "^1.3.1",
Expand Down
44 changes: 7 additions & 37 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2301,49 +2301,21 @@ export type WeightFn = $Values<typeof WeightFn>;
export const Wordcloud: React_2.FunctionComponent<SpecRequiredProps_10 & SpecOptionalProps_10>;

// @public (undocumented)
export interface WordcloudConfigs {
// (undocumented)
count: number;
// (undocumented)
endAngle: number;
// (undocumented)
exponent: number;
// (undocumented)
fontFamily: string;
// (undocumented)
fontStyle: FontStyle;
// (undocumented)
fontWeight: number;
// (undocumented)
height: number;
// (undocumented)
maxFontSize: number;
// (undocumented)
minFontSize: number;
// (undocumented)
padding: number;
// (undocumented)
spiral: string;
// (undocumented)
startAngle: number;
export type WordCloudElementEvent = [WordModel, SeriesIdentifier];

// @alpha (undocumented)
export interface WordcloudSpec extends Spec, WordcloudViewModel {
// (undocumented)
weightFn: WeightFn;
chartType: typeof ChartType.Wordcloud;
// (undocumented)
width: number;
specType: typeof SpecType.Series;
}

// @public (undocumented)
export type WordCloudElementEvent = [WordModel, SeriesIdentifier];

// @alpha (undocumented)
export interface WordcloudSpec extends Spec {
export interface WordcloudViewModel {
// (undocumented)
angleCount: number;
// (undocumented)
chartType: typeof ChartType.Wordcloud;
// (undocumented)
config: RecursivePartial<WordcloudConfigs>;
// (undocumented)
data: WordModel[];
// (undocumented)
endAngle: number;
Expand All @@ -2364,8 +2336,6 @@ export interface WordcloudSpec extends Spec {
// (undocumented)
padding: number;
// (undocumented)
specType: typeof SpecType.Series;
// (undocumented)
spiral: string;
// (undocumented)
startAngle: number;
Expand Down
42 changes: 0 additions & 42 deletions packages/charts/src/chart_types/wordcloud/layout/config/config.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
* Side Public License, v 1.
*/

import { Word as D3Word } from 'd3-cloud';
import { $Values as Values } from 'utility-types';

import { Pixels, PointObject, Rectangle } from '../../../../common/geometry';
import { Pixels, PointObject } from '../../../../common/geometry';
import { FontStyle } from '../../../../common/text_utils';
import { Color } from '../../../../utils/common';
import { config } from '../config/config';
import { Config } from './config_types';

/** @public */
export interface WordModel {
Expand All @@ -32,49 +31,26 @@ export const WeightFn = Object.freeze({
/** @public */
export type WeightFn = Values<typeof WeightFn>;

/** @internal */
export interface Word extends Rectangle {
/**
* Word properties extends `D3Word` except for explicitly defined values.
*
* `D3Word` values are added datum via d3TagCloud, but may be undefined
* @internal
*/
export interface Word extends D3Word {
datum: WordModel;
text: string;
color: string;
font: string;
fontFamily: string;
fontWeight: number;
hasText: boolean;
height: number;
padding: number;
rotate: number;
size: number;
style: string;
text: string;
weight: number;
x: number;
xoff: number;
y: number;
yoff: number;
datum: WordModel;
}

/** @public */
export interface Configs {
count: number;
endAngle: number;
exponent: number;
fontFamily: string;
fontStyle: FontStyle;
fontWeight: number;
height: number;
maxFontSize: number;
minFontSize: number;
padding: number;
spiral: string;
startAngle: number;
weightFn: WeightFn;
width: number;
size: number;
}

/** @public */
export type OutOfRoomCallback = (wordCount: number, renderedWordCount: number, renderedWords: string[]) => void;

/** @internal */
/** @public */
export interface WordcloudViewModel {
startAngle: number;
endAngle: number;
Expand All @@ -90,7 +66,6 @@ export interface WordcloudViewModel {
data: WordModel[];
weightFn: WeightFn;
outOfRoomCallback: OutOfRoomCallback;
// specType: string;
}

/** @internal */
Expand All @@ -105,7 +80,6 @@ export type PickFunction = (x: Pixels, y: Pixels) => Array<WordcloudViewModel>;

/** @internal */
export type ShapeViewModel = {
config: Config;
wordcloudViewModel: WordcloudViewModel;
chartCenter: PointObject;
pickQuads: PickFunction;
Expand Down Expand Up @@ -141,10 +115,9 @@ export const nullWordcloudViewModel: WordcloudViewModel = {
};

/** @internal */
export const nullShapeViewModel = (specifiedConfig?: Config, chartCenter?: PointObject): ShapeViewModel => ({
config: specifiedConfig || config,
export const nullShapeViewModel = (): ShapeViewModel => ({
wordcloudViewModel: nullWordcloudViewModel,
chartCenter: chartCenter || { x: 0, y: 0 },
chartCenter: { x: 0, y: 0 },
pickQuads: () => [],
specId: 'empty',
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
* Side Public License, v 1.
*/

import { Dimensions } from '../../../../utils/dimensions';
import { Theme } from '../../../../utils/themes/theme';
import { WordcloudSpec } from '../../specs';
import { Config } from '../types/config_types';
import { WordcloudViewModel, PickFunction, ShapeViewModel } from '../types/viewmodel_types';

/** @internal */
export function shapeViewModel(spec: WordcloudSpec, config: Config): ShapeViewModel {
const { width, height, margin } = config;

const innerWidth = width * (1 - Math.min(1, margin.left + margin.right));
const innerHeight = height * (1 - Math.min(1, margin.top + margin.bottom));
export function shapeViewModel(spec: WordcloudSpec, theme: Theme, chartDimensions: Dimensions): ShapeViewModel {
const { width, height } = chartDimensions;
const { chartMargins: margin } = theme;
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;

const chartCenter = {
x: width * margin.left + innerWidth / 2,
Expand Down Expand Up @@ -64,7 +65,6 @@ export function shapeViewModel(spec: WordcloudSpec, config: Config): ShapeViewMo

// combined viewModel
return {
config,
chartCenter,
wordcloudViewModel,
pickQuads,
Expand Down
Loading