Skip to content

Commit

Permalink
Fix color stops of portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Sep 7, 2023
1 parent 598a92a commit 4828dac
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions web/components/charts/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import React, {
import { Contract } from 'common/contract'
import { useMeasureSize } from 'web/hooks/use-measure-size'
import { clamp } from 'lodash'
import { HistoryPoint } from 'common/chart'

export interface ContinuousScale<T> extends AxisScale<T> {
invert(n: number): T
Expand Down Expand Up @@ -450,23 +451,30 @@ export const formatDateInRange = (d: Date, start: Date, end: Date) => {
}
return formatDate(d, opts)
}
export const computeColorStops = <P,>(

// assumes linear interpolation
export const computeColorStops = <P extends HistoryPoint>(
data: P[],
pc: (p: P) => string,
px: (p: P) => number
) => {
const segments: { x: number; color: string }[] = []
let currOffset = px(data[0])
let currColor = pc(data[0])
for (const p of data) {
const c = pc(p)
if (c !== currColor) {
segments.push({ x: currOffset, color: currColor })
currOffset = px(p)
currColor = c

if (data.length === 0) return segments

segments.push({ x: px(data[0]), color: pc(data[0]) })

for (let i = 1; i < data.length - 1; i++) {
const prev = data[i - 1]
const curr = data[i]
if (pc(prev) !== pc(curr)) {
// given a line through points (x0, y0) and (x1, y1), find the x value where y = 0 (intersects with x axis)
const xIntersect =
prev.x + (prev.y * (curr.x - prev.x)) / (prev.y - curr.y)

segments.push({ x: px({ ...prev, x: xIntersect }), color: pc(curr) })
}
}
segments.push({ x: currOffset, color: currColor })

const stops: { x: number; color: string }[] = []
stops.push({ x: segments[0].x, color: segments[0].color })
Expand Down

3 comments on commit 4828dac

@vercel
Copy link

@vercel vercel bot commented on 4828dac Sep 7, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs-mantic.vercel.app
docs-git-main-mantic.vercel.app
docs-pi-teal.vercel.app
docs.manifold.markets

@vercel
Copy link

@vercel vercel bot commented on 4828dac Sep 7, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

dev – ./web

dev-manifold.vercel.app
dev-git-main-mantic.vercel.app
dev.manifold.markets
dev-mantic.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4828dac Sep 7, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.