Skip to content

Commit

Permalink
Improve code quality;
Browse files Browse the repository at this point in the history
  • Loading branch information
6nv committed Apr 17, 2024
1 parent ffd1a80 commit 6f57664
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/igen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import { Psychart } from 'psychart';

for (let gradient of Psychart.getGradientNames()) {
for (const gradient of Psychart.getGradientNames()) {
fs.writeFileSync(
'src/img/' + gradient.toLowerCase() + '.svg',
Psychart.generateGradientIcon(gradient));
Expand Down
8 changes: 4 additions & 4 deletions src/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { Psychart } from 'psychart';
export const PsyPanel: React.FC<PanelProps<PsyOptions>> = ({ options, data, width, height }) => {
const isDarkTheme = useTheme2().isDark;
try {
const layout = { padding: { x: 40, y: 20 }, size: { x: width, y: height } } as Layout,
const layout: Layout = { padding: { x: 40, y: 20 }, size: { x: width, y: height } },
style = Psychart.getDefaultStyleOptions(isDarkTheme),
psychart = new Psychart(layout, options, style),
psychart: Psychart = new Psychart(layout, options, style),
formatted = format(data.series),
fieldList = getFieldList(formatted),
startTime = data.timeRange.from.unix() * 1e3,
endTime = data.timeRange.to.unix() * 1e3;
for (let t in formatted) {
for (let f in options.series || {}) {
for (const t in formatted) {
for (const f in options.series || {}) {
if (options.series[f].legend && fieldList.includes(options.series[f].dryBulb) && fieldList.includes(options.series[f].other)) {
psychart.plot({ db: formatted[t][options.series[f].dryBulb], other: formatted[t][options.series[f].other], measurement: options.series[f].measurement }, +f, +t, startTime, endTime);
}
Expand Down
22 changes: 11 additions & 11 deletions src/psychart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,19 @@ export class Psychart {
/**
* Return an array of all allowed gradient names.
*/
static getGradientNames(): PaletteName[] {
public static getGradientNames(): PaletteName[] {
return Object.keys(Palette).filter(name => name !== this.regionGradient) as PaletteName[];
}
/**
* Return an array of region names and their corresponding tooltips.
*/
static getRegionNamesAndTips(): Array<[RegionName, string]> {
public static getRegionNamesAndTips(): Array<[RegionName, string]> {
return Object.entries(this.regions).map(([name, region]) => [name as RegionName, region.tooltip]);
}
/**
* Return some suggested style options based on if the current display is dark or light theme.
*/
static getDefaultStyleOptions(isDarkTheme: boolean): StyleOptions {
public static getDefaultStyleOptions(isDarkTheme: boolean): StyleOptions {
return {
darkTheme: isDarkTheme,
fontColor: isDarkTheme ? new Color(208, 208, 208) : new Color(32, 32, 32),
Expand All @@ -232,7 +232,7 @@ export class Psychart {
* Generate an SVG element to use as this gradient's icon.
* Returns the outer HTML string to be saved in a file.
*/
static generateGradientIcon(gradient: PaletteName): string {
public static generateGradientIcon(gradient: PaletteName): string {
const maxColorIndex: number = Palette[gradient].colors.length - 1;
return '<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="grad" x1="0" y1="0" x2="1" y2="0">' +
Palette[gradient].colors.map((color, i) => '<stop style="stop-color:' + color.toString() + '" offset="' + SMath.normalize(i, 0, maxColorIndex) + '" />').join('') +
Expand All @@ -241,7 +241,7 @@ export class Psychart {
/**
* Returns the path to the gradient icon.
*/
static getGradientIcon(gradient: PaletteName): string {
public static getGradientIcon(gradient: PaletteName): string {
return require('img/' + gradient.toLowerCase() + '.svg');
}
/**
Expand Down Expand Up @@ -525,7 +525,7 @@ export class Psychart {
/**
* Plot one psychrometric state onto the psychrometric chart.
*/
plot(state: Datum, id = 0, time: number = Date.now(), startTime: number = this.startTime, endTime: number = this.endTime): void {
public plot(state: Datum, id = 0, time: number = Date.now(), startTime: number = this.startTime, endTime: number = this.endTime): void {
// Skip series that are missing a measurement point.
if (typeof state.db !== 'number' || typeof state.other !== 'number') {
return;
Expand Down Expand Up @@ -583,7 +583,7 @@ export class Psychart {
/**
* Draw a shaded region on Psychart.
*/
drawRegion(states: Datum[], color: Color, tooltip?: string): void {
public drawRegion(states: Datum[], color: Color, tooltip?: string): void {
// Add the first state to the data set
const data: PsyState[] = [new PsyState(states[0])];
for (let i = 1; i < states.length; i++) {
Expand Down Expand Up @@ -615,28 +615,28 @@ export class Psychart {
/**
* Clear all plotted data from Psychart.
*/
clearData(): void {
public clearData(): void {
this.lastState = {};
this.clearChildren(this.g.points);
this.clearChildren(this.g.trends);
}
/**
* Clear all rendered regions from Psychart.
*/
clearRegions(): void {
public clearRegions(): void {
this.clearChildren(this.g.regions);
}
/**
* Return the SVG element to append on the parent.
*/
getElement(): SVGElement {
public getElement(): SVGElement {
return this.base;
}
}

/**
* Represents where the origin is in relation to the text.
*/
enum TextAnchor {
const enum TextAnchor {
NW, N, NE, E, SE, S, SW, W, C
}
20 changes: 10 additions & 10 deletions src/psystate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ export class PsyState {
/**
* Dry Bulb
*/
readonly db: number;
public readonly db: number;
/**
* Relative Humidity
*/
readonly rh: number = 0;
public readonly rh: number = 0;
/**
* Wet Bulb
*/
readonly wb: number = 0;
public readonly wb: number = 0;
/**
* Dew Point
*/
readonly dp: number = 0;
public readonly dp: number = 0;
/**
* Humidity Ratio
*/
readonly hr: number = 0;
public readonly hr: number = 0;
/**
* Vapor Pressure
*/
readonly vp: number = 0;
public readonly vp: number = 0;
/**
* Moist Air Enthalpy
*/
readonly h: number = 0;
public readonly h: number = 0;
/**
* Moist Air Volume
*/
readonly v: number = 0;
public readonly v: number = 0;
/**
* Standard Atmospheric Air Pressure
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ export class PsyState {
/**
* Compute a first-time initialization of psychrolib.
*/
static initialize(layout: Layout, config: PsyOptions): void {
public static initialize(layout: Layout, config: PsyOptions): void {
PsyState.size = layout.size;
PsyState.padding = layout.padding;
PsyState.flipXY = config.flipXY;
Expand Down Expand Up @@ -126,7 +126,7 @@ export class PsyState {
/**
* Convert this psychrometric state to an X-Y coordinate on a psychrometric chart.
*/
toXY(): Point {
public toXY(): Point {
if (PsyState.flipXY) {
return {
x: SMath.clamp(SMath.translate(this.hr, 0, PsyState.hrMax, PsyState.padding.x, PsyState.size.x - PsyState.padding.x), PsyState.padding.x, PsyState.size.x - PsyState.padding.x),
Expand Down
60 changes: 30 additions & 30 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export interface Layout {
/**
* The outer size of Psychart, in pixels.
*/
size: Point;
readonly size: Point;
/**
* The padding in pixels.
*/
padding: Point;
readonly padding: Point;
}

export interface Datum {
Expand All @@ -41,125 +41,125 @@ export interface Datum {
/**
* The type of measurements that were taken.
*/
measurement: Measurement;
readonly measurement: Measurement;
}

export interface Region {
/**
* The text to display on mouse hover
*/
tooltip: string;
readonly tooltip: string;
/**
* The data that represents the boundary of this region
*/
data: Datum[];
readonly data: Datum[];
}

export interface StyleOptions {
/**
* Determines whether or not the user is using a dark theme.
*/
darkTheme: boolean;
readonly darkTheme: boolean;
/**
* The font color.
*/
fontColor: Color;
readonly fontColor: Color;
/**
* The axis color.
*/
lineColor: Color;
readonly lineColor: Color;
/**
* The font size, in pixels.
*/
fontSize: number;
readonly fontSize: number;
/**
* The chart resolution, in units.
*/
resolution: number;
readonly resolution: number;
/**
* The major axis interval.
*/
major: number;
readonly major: number;
/**
* The default time span (ms) between the first and last plotted point.
*/
timeSpan: number;
readonly timeSpan: number;
}

export interface PsyOptions {
/**
* Represents the unit system, in either US (IP) or metric (SI)
*/
unitSystem: 'IP' | 'SI';
readonly unitSystem: 'IP' | 'SI';
/**
* The altitude of measurements taken.
*/
altitude: number;
readonly altitude: number;
/**
* The minimum value on the dry bulb axis.
*/
dbMin: number;
readonly dbMin: number;
/**
* The maximum value on the dry bulb axis.
*/
dbMax: number;
readonly dbMax: number;
/**
* The maximum value on the dew point axis.
*/
dpMax: number;
readonly dpMax: number;
/**
* Determine whether to render a Mollier diagram.
*/
flipXY: boolean;
readonly flipXY: boolean;
/**
* Render pre-defined shaded regions.
*/
regions: RegionName[];
readonly regions: RegionName[];
/**
* The number of data series to render.
*/
count: number;
readonly count: number;
/**
* The data series information.
*/
series: DataSeries;
readonly series: DataSeries;
}

export interface DataOptions {
/**
* Add a label to this data series.
*/
legend: string;
readonly legend: string;
/**
* The type of measurements that were taken.
*/
measurement: Measurement;
readonly measurement: Measurement;
/**
* The name of the dry bulb series.
*/
dryBulb: string;
readonly dryBulb: string;
/**
* The name of the wet bulb, dew point, or relative humidity series, depending on `measurement`.
*/
other: string;
readonly other: string;
/**
* The relative humidity measurement type, in percent [0-100] or float [0.0-1.0]
*/
relHumType: 'percent' | 'float';
readonly relHumType: 'percent' | 'float';
/**
* The point radius, in pixels.
*/
pointRadius: number;
readonly pointRadius: number;
/**
* Determines whether or not to connect points with a line.
*/
line: boolean;
readonly line: boolean;
/**
* Determines the color gradient for time series plots.
*/
gradient: PaletteName;
readonly gradient: PaletteName;
/**
* Defines whether or not to show advanced state variables.
*/
advanced: boolean;
readonly advanced: boolean;
}

0 comments on commit 6f57664

Please sign in to comment.