-
Notifications
You must be signed in to change notification settings - Fork 2.2k
PlotLayer add axes titles #671
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
Changes from all commits
fc8785e
456ca4f
9eea028
a2c6b01
74530c2
cef1132
121091d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,3 +95,9 @@ Amount that grids should setback from the bounding box. Relative to the size of | |
- Default: `[0, 0, 0, 255]` | ||
|
||
Color to draw the grids with, in `[r, g, b, a]`. | ||
|
||
##### `axesTitles` (Array, optional) | ||
|
||
- Default: `['x', 'z', 'y']` | ||
|
||
Strings to draw next to each axis as their titles, note that the second element is the axis that points upwards. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated README |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,11 @@ import labelFragment from './label-fragment.glsl'; | |
const FONT_SIZE = 48; | ||
|
||
const defaultProps = { | ||
fontSize: 12, | ||
fontSize: 16, | ||
ticksCount: 6, | ||
axesOffset: 0, | ||
axesColor: [0, 0, 0, 255] | ||
axesColor: [0, 0, 0, 255], | ||
axesTitles: ['x', 'z', 'y'] | ||
}; | ||
|
||
/* Utils */ | ||
|
@@ -53,7 +54,8 @@ export default class AxesLayer extends Layer { | |
|
||
attributeManager.addInstanced({ | ||
instancePositions: {size: 2, update: this.calculateInstancePositions, noAlloc: true}, | ||
instanceNormals: {size: 3, update: this.calculateInstanceNormals, noAlloc: true} | ||
instanceNormals: {size: 3, update: this.calculateInstanceNormals, noAlloc: true}, | ||
instanceIsTitle: {size: 1, update: this.calculateInstanceIsTitle, noAlloc: true} | ||
}); | ||
|
||
this.setState({ | ||
|
@@ -69,13 +71,13 @@ export default class AxesLayer extends Layer { | |
const {attributeManager} = this.state; | ||
|
||
if (changeFlags.dataChanged || oldProps.ticksCount !== props.ticksCount) { | ||
const {data, ticksCount} = props; | ||
const {data, ticksCount, axesTitles} = props; | ||
|
||
const ticks = this.calculateTicks(data, ticksCount); | ||
|
||
this.setState({ | ||
ticks, | ||
labelTexture: this.renderLabelTexture(ticks), | ||
labelTexture: this.renderLabelTexture(ticks, axesTitles), | ||
center: data.map(b => (b[0] + b[1]) / 2), | ||
dim: data.map(b => b[1] - b[0]) | ||
}); | ||
|
@@ -254,23 +256,42 @@ export default class AxesLayer extends Layer { | |
attribute.value = new Float32Array(flatten(normals)); | ||
} | ||
|
||
calculateInstanceIsTitle(attribute) { | ||
const {ticks} = this.state; | ||
|
||
const isTitle = ticks.map(axisTicks => { | ||
const ticksCount = axisTicks.length - 1; | ||
return axisTicks.map((t, i) => i < ticksCount ? 0 : 1); | ||
}); | ||
|
||
attribute.value = new Float32Array(flatten(isTitle)); | ||
} | ||
|
||
// updates the instancePositions and instanceNormals attributes | ||
calculateTicks(bounds, ticksCount) { | ||
const xTicks = getTicks(bounds[0], ticksCount); | ||
const yTicks = getTicks(bounds[1], ticksCount); | ||
const zTicks = getTicks(bounds[2], ticksCount); | ||
|
||
return [xTicks, yTicks, zTicks]; | ||
return [ | ||
[...xTicks, (bounds[0][0] + bounds[0][1]) / 2], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this PR handles the case that different axis has different ranges? You once mentioned that you'd like to scale each axis differently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet. The solution to axes scaling was implemented by @Pessimistress probably on some other branch. |
||
[...yTicks, (bounds[1][0] + bounds[1][1]) / 2], | ||
[...zTicks, (bounds[2][0] + bounds[2][1]) / 2]]; | ||
} | ||
|
||
renderLabelTexture(ticks) { | ||
renderLabelTexture(ticks, axesTitles) { | ||
|
||
if (this.state.labels) { | ||
this.state.labels.labelTexture.delete(); | ||
} | ||
|
||
const axesLabels = ticks.map((axisTicks, axisId) => { | ||
const ticksCount = axisTicks.length - 1; | ||
return axisTicks.map((t, i) => i < ticksCount ? t : axesTitles[axisId]); | ||
}); | ||
|
||
// attach a 2d texture of all the label texts | ||
const textureInfo = textMatrixToTexture(this.context.gl, ticks, FONT_SIZE); | ||
const textureInfo = textMatrixToTexture(this.context.gl, axesLabels, FONT_SIZE); | ||
if (textureInfo) { | ||
// success | ||
const {columnWidths, texture} = textureInfo; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used in the PR? Or was it a missing dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question... is d3-scale used for scaling each axis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a missing dependency before the PR was added. Yes it is used to scale the axes.