-
Notifications
You must be signed in to change notification settings - Fork 186
difference mark and shift transform #1896
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
ecf9138
difference mark
mbostock 64e3f62
fix filtering; opacity options
mbostock 4ac221f
remove unused import
mbostock 6b56c80
withTip; don’t duplicate channels
mbostock 1880bff
difference as a composite mark
Fil a93cc56
difference tip
Fil c6c0be5
reuse channels
Fil a347333
more composite marks
mbostock b5ccc92
apply clip as render transform
mbostock 43275f8
consolidate code
mbostock dea1cf3
aria labels
mbostock cc32a9b
organize imports
mbostock 0326a33
fix differenceY1 test
mbostock b893638
update tests
mbostock 376c079
better defaults
mbostock f1cd4ed
handle ChannelValueSpec
mbostock 3544a98
update test
mbostock 0775c7e
memoTuple
mbostock 79ceefe
checkpoint docs
mbostock 9e8a7ff
fix differenceY1 test
mbostock 6952832
tip fixes
mbostock b18c780
**positiveOpacity**, **negativeOpacity** default to **fillOpacity**; …
Fil 56cbe36
positiveFill
Fil 6aa5488
another test
mbostock 23dd51f
positiveFillOpacity & fix test
Fil fea7c77
swap [xy][12]; default y1 = 0
mbostock 790b449
Merge branch 'main' into mbostock/difference
mbostock 0b2eea1
shift option
mbostock 6585322
another difference example
mbostock e170c8c
z
Fil 24531b9
simpler marks (no need for two differences)
Fil db54a68
inferScaleOrder
mbostock a6a88da
Merge branch 'mbostock/difference' into fil/difference
Fil 354d22f
simpler chart
Fil a6ecc1c
enhanced group extent; findX sketch
mbostock 136ffc2
Merge branch 'main' into mbostock/difference
mbostock 9fa447a
shift transform
mbostock 52cc5b1
shift domain hint
mbostock d10ce7e
promote stroke to z
mbostock 94a48ee
simpler channel domain hint
mbostock d4b0685
more difference docs
mbostock 67b3e3b
more difference docs
mbostock 54bf3fe
more documentation
mbostock b7a95ad
Merge branch 'mbostock/difference' into fil/difference
Fil 6452734
call next twice (once for the path, once for the clipPath)
Fil 30db89c
support clip: frame
Fil 60b48f9
document differenceY
Fil 0c58cf0
test ordinal difference
Fil f1cee3d
adopt Plot.find
Fil 36dd5ea
Merge branch 'main' into mbostock/difference
mbostock c7afcb8
Merge branch 'fil/difference' into mbostock/difference
mbostock bb30385
more docs
mbostock 00ab6f0
fix z documentation
mbostock 2426fb1
fix space
mbostock 5b22d28
Merge branch 'main' into mbostock/difference
mbostock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type {Data, RenderableMark} from "../mark.js"; | ||
import type {AreaYOptions} from "./area.js"; | ||
|
||
/** TODO */ | ||
export function differenceY(data?: Data, options?: AreaYOptions): Difference; | ||
|
||
/** The difference mark. */ | ||
export class Difference extends RenderableMark {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import {area as shapeArea, line as shapeLine} from "d3"; | ||
import {create} from "../context.js"; | ||
import {maybeCurve} from "../curve.js"; | ||
import {Mark} from "../mark.js"; | ||
import {identity, indexOf, isColor} from "../options.js"; | ||
import {applyIndirectStyles, applyTransform, getClipId} from "../style.js"; | ||
import {maybeDenseIntervalX} from "../transforms/bin.js"; | ||
|
||
const defaults = { | ||
ariaLabel: "difference", | ||
fill: "none", | ||
stroke: "currentColor", | ||
strokeWidth: 1.5, | ||
strokeLinecap: "round", | ||
strokeLinejoin: "round", | ||
strokeMiterlimit: 1 | ||
}; | ||
|
||
function maybeColor(value) { | ||
if (value == null) return "none"; | ||
if (!isColor(value)) throw new Error(`invalid color: ${value}`); | ||
return value; | ||
} | ||
|
||
class Difference extends Mark { | ||
constructor(data, options = {}) { | ||
const {x1, y1, x2, y2, curve, tension, positiveColor = "green", negativeColor = "blue"} = options; | ||
super( | ||
data, | ||
{ | ||
x1: {value: x1, scale: "x"}, | ||
y1: {value: y1, scale: "y"}, | ||
x2: {value: x2, scale: "x", optional: true}, | ||
y2: {value: y2, scale: "y", optional: true} | ||
}, | ||
options, | ||
defaults | ||
); | ||
this.curve = maybeCurve(curve, tension); | ||
this.positiveColor = maybeColor(positiveColor); | ||
this.negativeColor = maybeColor(negativeColor); | ||
} | ||
filter(index) { | ||
return index; | ||
} | ||
mbostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render(index, scales, channels, dimensions, context) { | ||
const {x1: X1, y1: Y1, x2: X2 = X1, y2: Y2 = Y1} = channels; | ||
const {negativeColor, positiveColor} = this; | ||
const {height} = dimensions; | ||
const clipPositive = getClipId(); | ||
const clipNegative = getClipId(); | ||
return create("svg:g", context) | ||
.call(applyIndirectStyles, this, dimensions, context) | ||
.call(applyTransform, this, scales, 0, 0) | ||
.call((g) => | ||
g | ||
.append("clipPath") | ||
.attr("id", clipPositive) | ||
.append("path") | ||
.attr("d", renderArea(X1, Y1, height, this, index)) | ||
) | ||
.call((g) => | ||
g | ||
.append("clipPath") | ||
.attr("id", clipNegative) | ||
.append("path") | ||
.attr("d", renderArea(X1, Y1, 0, this, index)) | ||
) | ||
.call((g) => | ||
g | ||
.append("path") | ||
.attr("fill", positiveColor) | ||
.attr("stroke", "none") | ||
.attr("clip-path", `url(#${clipPositive})`) | ||
.attr("d", renderArea(X2, Y2, 0, this, index)) | ||
) | ||
.call((g) => | ||
g | ||
.append("path") | ||
.attr("fill", negativeColor) | ||
.attr("stroke", "none") | ||
.attr("clip-path", `url(#${clipNegative})`) | ||
.attr("d", renderArea(X2, Y2, height, this, index)) | ||
) | ||
.call((g) => g.append("path").attr("d", renderLine(X1, Y1, this, index))) | ||
.node(); | ||
} | ||
} | ||
|
||
function renderArea(X, Y, y0, {curve}, index) { | ||
return shapeArea() | ||
.curve(curve) | ||
.defined((i) => i >= 0) | ||
mbostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.x((i) => X[i]) | ||
.y1((i) => Y[i]) | ||
.y0(y0)(index); | ||
} | ||
|
||
function renderLine(X, Y, {curve}, index) { | ||
return shapeLine() | ||
.curve(curve) | ||
.defined((i) => i >= 0) | ||
mbostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.x((i) => X[i]) | ||
.y((i) => Y[i])(index); | ||
} | ||
|
||
export function differenceY(data, {x = indexOf, x1 = x, x2 = x, y = identity, y1 = y, y2 = y, ...options} = {}) { | ||
return new Difference(data, maybeDenseIntervalX({...options, x1, x2, y1, y2})); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as Plot from "@observablehq/plot"; | ||
import * as d3 from "d3"; | ||
|
||
export async function differenceY() { | ||
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType); | ||
const goog = await d3.csv<any>("data/goog.csv", d3.autoType); | ||
const x = aapl.map((d) => d.Date); | ||
const y1 = aapl.map((d, i, data) => d.Close / data[0].Close); | ||
const y2 = goog.map((d, i, data) => d.Close / data[0].Close); | ||
mbostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Plot.plot({ | ||
marks: [Plot.differenceY(aapl, {x, y1, y2})] | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.