Skip to content

Commit

Permalink
Remove gap from bar thickness if width of bar is below threshold (#3450)
Browse files Browse the repository at this point in the history
This prevents bars from dissappearing at just the right
resolution and zoom level due to pixel sampling moire
  • Loading branch information
themadcreator authored Mar 23, 2018
1 parent 0fbce8e commit 8b38387
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions quicktests/overlaying/tests/basic/canvas_bar.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function makeData() {
"use strict";

// makes 10 datasets of 10000 points
return Array.apply(null, Array(1)).map((_, datasetIndex) => {
return Array.apply(null, Array(5*10000)).map((_, i) => {
return Array.apply(null, Array(1000)).map((_, i) => {
return {
// one data point per day, offset by one hour per dataset
x: new Date(i * 1000 * 3600 * 24 + datasetIndex * 1000 * 3600),
y: datasetIndex + Math.random()
y: datasetIndex + 10 + Math.random()
};
});
});
Expand All @@ -30,8 +29,9 @@ function run(div, data, Plottable) {
.renderer("canvas")
.deferredRendering(true)
.x((d) => d.x, xScale)
.barEnd((d) => new Date(1000 * 3600 * 24 + d.x.valueOf()))
.y((d) => d.y, yScale)
.attr("gap", () => 0)
.attr("gap", () => 1)
.attr("fill", (d,i,ds) => ds.metadata(), colorScale);

var table = new Plottable.Components.Table([
Expand Down
7 changes: 6 additions & 1 deletion src/plots/barPlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type BarAlignment = keyof typeof BarAlignment;

export class Bar<X, Y> extends XYPlot<X, Y> {
public static _BAR_THICKNESS_RATIO = 0.95;
public static _BAR_GAPLESS_THRESHOLD_PX = 3;
public static _SINGLE_BAR_DIMENSION_RATIO = 0.4;
private static _BAR_AREA_CLASS = "bar-area";
private static _BAR_END_KEY = "barEnd";
Expand Down Expand Up @@ -751,7 +752,11 @@ export class Bar<X, Y> extends XYPlot<X, Y> {
const thicknessF = attrToProjector[Bar._BAR_THICKNESS_KEY];
const gapF = attrToProjector["gap"];
const thicknessMinusGap = gapF == null ? thicknessF : (d: any, i: number, dataset: Dataset) => {
return thicknessF(d, i, dataset) - gapF(d, i, dataset);
const thick = thicknessF(d, i, dataset);
// only subtract gap if bars are at least 2 pixels wide, otherwise canvas
// interpolation can cause bars to become invisible due to subpixel
// sampling
return thick < Bar._BAR_GAPLESS_THRESHOLD_PX ? thick : thick - gapF(d, i, dataset);
};

// re-interpret "width" attr from representing "thickness" to actually meaning
Expand Down

1 comment on commit 8b38387

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

Remove gap from bar thickness if width of bar is below threshold (#3450)

Demo: quicktests | fiddle

Please sign in to comment.