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

fix: fixed axes update #209

Merged
merged 1 commit into from
Jan 29, 2024
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
4 changes: 4 additions & 0 deletions demo/examples/dynamic-updates.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ <h1>Dynamic updates</h1>
yagr7.setAxes({
y: {
precision: 2,
font: 'normal 20px serif',
},
x: {
font: 'normal 10px serif',
}
});
};

Expand Down
27 changes: 25 additions & 2 deletions src/YagrCore/utils/axes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as defaults from '../defaults';
import type Yagr from '../../';
import {YagrConfig, AxisOptions} from '../types';

import {asFn, getUnitSuffix, isNil, toFixed} from './common';
import {asFn, getUnitSuffix, isNil, px, toFixed} from './common';
import {Axis as TypedAxis} from './types';

const YAGR_AXIS_TO_UPLOT_AXIS = {
Expand Down Expand Up @@ -184,6 +184,14 @@ export function getRedrawOptionsForAxesUpdate(axes: YagrConfig['axes']) {
return options;
}


function pxRatioFont(font: string) {
let fontSize, fontSizeCss;
// eslint-disable-next-line no-return-assign
font = font.replace(/(\d+)px/, (_, p1) => px((fontSize = Math.round((fontSizeCss = Number(p1)) * window.devicePixelRatio))));
return [font, fontSize, fontSizeCss];
}

export function updateAxis(yagr: Yagr, uAxis: Axis, axisConfig: AxisOptions) {
const upd = getAxis(
{
Expand All @@ -192,11 +200,26 @@ export function updateAxis(yagr: Yagr, uAxis: Axis, axisConfig: AxisOptions) {
},
yagr,
);
upd.font = axisConfig.font || upd.font;

upd.ticks = {...uAxis.ticks, ...upd.ticks};
upd.grid = {...uAxis.grid, ...upd.grid};
upd.border = {...uAxis.border, ...upd.border};
upd.splits = upd.splits || uAxis.splits;

/**
* uPlot implicitly converts theese properties and mutate axis.font and axis.labelFont
* but doesn't cover it with typings.
* @see https://github.com/leeoniya/uPlot/blob/378faf6fab9b84d86fd25a5b4425dc44d486b64d/src/uPlot.js#L275
*/
if (axisConfig.font && axisConfig.font !== uAxis.font?.[0]) {
//@ts-ignore
upd.font = pxRatioFont(axisConfig.font);
}
if (axisConfig.labelFont && axisConfig.labelFont !== uAxis.labelFont?.[0]) {
//@ts-ignore
upd.labelFont = pxRatioFont(axisConfig.labelFont);
}

Object.assign(uAxis, upd);

yagr.plugins.plotLines?.update(axisConfig.plotLines, axisConfig.scale);
Expand Down
Loading