diff --git a/Examples/Geometry/CubeAxes/index.js b/Examples/Geometry/CubeAxes/index.js index b960be8d8a7..6ad048b87ba 100644 --- a/Examples/Geometry/CubeAxes/index.js +++ b/Examples/Geometry/CubeAxes/index.js @@ -3,6 +3,9 @@ import '@kitware/vtk.js/favicon'; // Load the rendering pieces we want to use (for both WebGL and WebGPU) import '@kitware/vtk.js/Rendering/Profiles/Geometry'; +import * as d3scale from 'd3-scale'; +import * as d3array from 'd3-array'; + import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; import vtkCubeAxesActor from '@kitware/vtk.js/Rendering/Core/CubeAxesActor'; import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource'; @@ -40,6 +43,18 @@ renderer.addActor(actor); const cubeAxes = vtkCubeAxesActor.newInstance(); cubeAxes.setCamera(renderer.getActiveCamera()); cubeAxes.setDataBounds(actor.getBounds()); +// Replace ticks from axis 0 +function myGenerateTicks(defaultGenerateTicks) { + return (dataBounds) => { + const res = defaultGenerateTicks(dataBounds); + const scale = d3scale.scaleLinear().domain([dataBounds[0], dataBounds[1]]); + res.ticks[0] = d3array.range(dataBounds[0], dataBounds[1], 0.1); + const format = scale.tickFormat(res.ticks[0].length); + res.tickStrings[0] = res.ticks[0].map(format); + return res; + }; +} +cubeAxes.setGenerateTicks(myGenerateTicks(cubeAxes.getGenerateTicks())); renderer.addActor(cubeAxes); renderer.resetCamera(); diff --git a/Sources/Rendering/Core/CubeAxesActor/index.js b/Sources/Rendering/Core/CubeAxesActor/index.js index eceea2aaa0c..4d638597fc3 100644 --- a/Sources/Rendering/Core/CubeAxesActor/index.js +++ b/Sources/Rendering/Core/CubeAxesActor/index.js @@ -81,6 +81,22 @@ function applyTextStyle(ctx, style) { ctx.font = `${style.fontStyle} ${style.fontSize}px ${style.fontFamily}`; } +function defaultGenerateTicks(publicApi, model) { + return (dataBounds) => { + const ticks = []; + const tickStrings = []; + for (let i = 0; i < 3; i++) { + const scale = d3 + .scaleLinear() + .domain([dataBounds[i * 2], dataBounds[i * 2 + 1]]); + ticks[i] = scale.ticks(5); + const format = scale.tickFormat(5); + tickStrings[i] = ticks[i].map(format); + } + return { ticks, tickStrings }; + }; +} + // many properties of this actor depend on the API specific view The main // dependency being the resolution as that drives what font sizes to use. // Bacause of this we need to do some of the calculations in a API specific @@ -643,27 +659,23 @@ function vtkCubeAxesActor(publicAPI, model) { } // compute tick marks for axes - const ticks = []; - const tickStrings = []; - for (let i = 0; i < 3; i++) { - const scale = d3 - .scaleLinear() - .domain([model.dataBounds[i * 2], model.dataBounds[i * 2 + 1]]); - ticks[i] = scale.ticks(5); - const format = scale.tickFormat(5); - tickStrings[i] = ticks[i].map(format); - } + const t = model.generateTicks(model.dataBounds); // update gridlines / edge lines - publicAPI.updatePolyData(facesToDraw, edgesToDraw, ticks); + publicAPI.updatePolyData(facesToDraw, edgesToDraw, t.ticks); // compute label world coords and text - publicAPI.updateTextData(facesToDraw, edgesToDraw, ticks, tickStrings); + publicAPI.updateTextData( + facesToDraw, + edgesToDraw, + t.ticks, + t.tickStrings + ); // rebuild the texture only when force or changed bounds, face // visibility changes do to change the atlas if (boundsChanged || model.forceUpdate) { - publicAPI.updateTextureAtlas(tickStrings); + publicAPI.updateTextureAtlas(t.tickStrings); } } @@ -802,7 +814,7 @@ function vtkCubeAxesActor(publicAPI, model) { // Object factory // ---------------------------------------------------------------------------- -function defaultValues(initialValues) { +function defaultValues(publicAPI, model, initialValues) { return { boundsScaleFactor: 1.3, camera: null, @@ -811,30 +823,35 @@ function defaultValues(initialValues) { gridLines: true, axisLabels: null, axisTitlePixelOffset: 35.0, + tickLabelPixelOffset: 12.0, + generateTicks: defaultGenerateTicks(publicAPI, model), + ...initialValues, axisTextStyle: { fontColor: 'white', fontStyle: 'normal', fontSize: 18, fontFamily: 'serif', + ...initialValues?.axisTextStyle, }, - tickLabelPixelOffset: 12.0, tickTextStyle: { fontColor: 'white', fontStyle: 'normal', fontSize: 14, fontFamily: 'serif', + ...initialValues?.tickTextStyle, }, - ...initialValues, }; } // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { - Object.assign(model, defaultValues(initialValues)); - // Inheritance - vtkActor.extend(publicAPI, model, initialValues); + vtkActor.extend( + publicAPI, + model, + defaultValues(publicAPI, model, initialValues) + ); // internal variables model.lastFacesToDraw = [false, false, false, false, false, false]; @@ -870,6 +887,7 @@ export function extend(publicAPI, model, initialValues = {}) { 'faceVisibilityAngle', 'gridLines', 'tickLabelPixelOffset', + 'generateTicks', ]); macro.setGetArray(publicAPI, model, ['dataBounds'], 6);