Skip to content

Commit 2809aae

Browse files
over 1mm -> scientific notation
1 parent 52f7100 commit 2809aae

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

src/plotxy.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { HiPlotPluginData } from "./plugin";
1414
import React from "react";
1515
import { ResizableH } from "./lib/resizable";
1616
import _ from "underscore";
17-
import { Datapoint } from "./types";
17+
import { Datapoint, ParamType } from "./types";
1818
import { foCreateAxisLabel, foDynamicSizeFitContent } from "./lib/svghelpers";
1919
import { ContextMenu } from "./contextmenu";
2020

@@ -187,6 +187,36 @@ export class PlotXY extends React.Component<PlotXYProps, PlotXYState> {
187187
scale.range(range);
188188
return scale;
189189
}
190+
function getTickFormatter(param: string, scale: any): ((d: any) => string) | null {
191+
const paramDef = me.props.params_def[param];
192+
if (!paramDef) {
193+
return null;
194+
}
195+
if (paramDef.ticks_format) {
196+
return d3.format(paramDef.ticks_format);
197+
}
198+
const isNumericLike =
199+
paramDef.type === ParamType.NUMERIC ||
200+
paramDef.type === ParamType.NUMERICLOG ||
201+
paramDef.type === ParamType.NUMERICPERCENTILE ||
202+
paramDef.type === ParamType.TIMESTAMP;
203+
if (!isNumericLike || typeof scale.domain !== "function") {
204+
return null;
205+
}
206+
const domain = scale.domain();
207+
if (!Array.isArray(domain)) {
208+
return null;
209+
}
210+
const maxAbsDomain = d3.max(
211+
domain
212+
.map((v: any) => Math.abs(Number(v)))
213+
.filter((v: number) => Number.isFinite(v)),
214+
);
215+
if (maxAbsDomain === undefined || maxAbsDomain < 1e6) {
216+
return null;
217+
}
218+
return d3.format(".3~e");
219+
}
190220
function redraw_axis() {
191221
me.svg.selectAll(".axis_render").remove();
192222
me.svg.selectAll(".brush").remove();
@@ -212,6 +242,8 @@ export class PlotXY extends React.Component<PlotXYProps, PlotXYState> {
212242
me.state.height - margin.bottom - insideGraphMargin,
213243
margin.top + insideGraphMargin,
214244
]);
245+
const xTickFormatter = getTickFormatter(me.state.axis_x, x_scale);
246+
const yTickFormatter = getTickFormatter(me.state.axis_y, y_scale);
215247
zoom_brush = d3
216248
.brush()
217249
.extent([
@@ -227,6 +259,7 @@ export class PlotXY extends React.Component<PlotXYProps, PlotXYState> {
227259
d3
228260
.axisLeft(y_scale)
229261
.ticks(1 + me.state.height / 40)
262+
.tickFormat(yTickFormatter as any)
230263
.tickSizeInner(margin.left + margin.right - me.state.width),
231264
)
232265
.call((g) => g.select(".domain").remove())
@@ -259,6 +292,7 @@ export class PlotXY extends React.Component<PlotXYProps, PlotXYState> {
259292
d3
260293
.axisBottom(x_scale)
261294
.ticks(1 + me.state.width / 80)
295+
.tickFormat(xTickFormatter as any)
262296
.tickSizeInner(margin.bottom + margin.top - me.state.height),
263297
)
264298
.call((g) =>

0 commit comments

Comments
 (0)