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(core): Flipped y-axis stacked bar chart #1042

Merged
merged 2 commits into from
Feb 22, 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
24 changes: 16 additions & 8 deletions src/core/mark/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function drawBar(track: any, tile: Tile, model: GoslingTrackModel) {
const pivotedData = group(data, d => d[genomicChannel.field as string]);
const xKeys = [...pivotedData.keys()];

const isFlippedY = (IsChannelDeep(spec.y) && spec.y.flip) || spec.flipY;
// TODO: users may want to align rows by values
xKeys.forEach(k => {
let prevYEnd = 0;
Expand All @@ -89,8 +90,12 @@ export function drawBar(track: any, tile: Tile, model: GoslingTrackModel) {
const alphaTransition = model.markVisibility(d, { width: barWidth, zoomLevel });
const actualOpacity = Math.min(alphaTransition, opacity);

if (actualOpacity === 0 || barWidth <= 0 || y <= 0) {
// do not draw invisible marks
if (
actualOpacity === 0 ||
barWidth <= 0 ||
(isFlippedY && y - rowHeight >= 0) ||
(!isFlippedY && y <= 0)
Comment on lines +96 to +97
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some extra conditions to keep incorrect draws from occurring

) {
return;
}

Expand All @@ -103,9 +108,13 @@ export function drawBar(track: any, tile: Tile, model: GoslingTrackModel) {

let polygonForMouseEvents: number[] = [];

const barHeight = isFlippedY ? rowHeight - y : y;

if (circular) {
const farR = trackOuterRadius - ((rowHeight - prevYEnd) / trackHeight) * trackRingSize;
const nearR = trackOuterRadius - ((rowHeight - y - prevYEnd) / trackHeight) * trackRingSize;
const ys = isFlippedY ? prevYEnd : rowHeight - prevYEnd;
const farR = trackOuterRadius - (ys / trackHeight) * trackRingSize;
const ye = isFlippedY ? barHeight + prevYEnd : rowHeight - y - prevYEnd;
const nearR = trackOuterRadius - (ye / trackHeight) * trackRingSize;

const sPos = cartesianToPolar(xs, trackWidth, nearR, cx, cy, startAngle, endAngle);
const startRad = valueToRadian(xs, trackWidth, startAngle, endAngle);
Expand All @@ -119,16 +128,15 @@ export function drawBar(track: any, tile: Tile, model: GoslingTrackModel) {
g.closePath();
} else {
g.beginFill(colorToHex(color), color === 'none' ? 0 : actualOpacity);
g.drawRect(xs, rowHeight - y - prevYEnd, barWidth, y);
const ys = rowHeight - y - prevYEnd;
const ys = isFlippedY ? prevYEnd : rowHeight - y - prevYEnd;
g.drawRect(xs, ys, barWidth, barHeight);
const ye = ys + y;
polygonForMouseEvents = [xs, ys, xs, ye, xe, ye, xe, ys];
}

/* Mouse Events */
model.getMouseEventModel().addPolygonBasedEvent(d, polygonForMouseEvents);

prevYEnd += y;
prevYEnd += barHeight;
});
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tracks/gosling-track/gosling-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ const factory: PluginTrackFactory<Tile, GoslingTrackOptions> = (HGC, context, op

// Replace width and height information with the actual values for responsive encoding
const [trackWidth, trackHeight] = this.dimensions; // actual size of a track
const axisSize = IsXAxis(resolvedSpec) ? HIGLASS_AXIS_SIZE : 0; // Why the axis size must be added here?
const axisSize = IsXAxis(resolvedSpec) && this.options.spec.layout === 'linear' ? HIGLASS_AXIS_SIZE : 0; // Why the axis size must be added here?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circular tracks were receiving the incorrect track height because height was being added here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch.

const [w, h] = [trackWidth, trackHeight + axisSize];
const circularFactor = Math.min(w, h) / Math.min(resolvedSpec.width!, resolvedSpec.height!);
if (resolvedSpec.innerRadius) {
Expand Down
Loading