-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scatterplot/ Add basic chart with points (#831)
- Loading branch information
Showing
5 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
/* | ||
* Copyright 2017-2022 Allegro.pl | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as React from "react"; | ||
|
||
import { Datum } from "plywood"; | ||
import { ConcreteSeries } from "../../../common/models/series/concrete-series"; | ||
import "./scatterplot.scss"; | ||
|
||
import { LinearScale } from "../../utils/linear-scale/linear-scale"; | ||
|
||
interface PointProps { | ||
datum: Datum; | ||
xScale: LinearScale; | ||
yScale: LinearScale; | ||
xSeries: ConcreteSeries; | ||
ySeries: ConcreteSeries; | ||
} | ||
|
||
const POINT_RADIUS = 3; | ||
|
||
export const Point: React.SFC<PointProps> = ({ datum, xScale, yScale, xSeries, ySeries }) => { | ||
const xValue = xSeries.selectValue(datum); | ||
const yValue = ySeries.selectValue(datum); | ||
|
||
return (<circle | ||
cx={xScale(xValue)} | ||
cy={yScale(yValue)} | ||
r={POINT_RADIUS} | ||
className="point" | ||
/> | ||
); | ||
}; |
This file contains 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 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 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,52 @@ | ||
/* | ||
* Copyright 2017-2022 Allegro.pl | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as React from "react"; | ||
|
||
import "./scatterplot.scss"; | ||
|
||
import { Stage } from "../../../common/models/stage/stage"; | ||
import { Unary } from "../../../common/utils/functional/functional"; | ||
import { roundToHalfPx } from "../../utils/dom/dom"; | ||
import { LinearScale } from "../../utils/linear-scale/linear-scale"; | ||
|
||
const TEXT_OFFSET_X = 16; | ||
|
||
interface XAxisProps { | ||
stage: Stage; | ||
ticks: number[]; | ||
tickSize: number; | ||
scale: LinearScale; | ||
formatter: Unary<number, string>; | ||
} | ||
|
||
export const XAxis: React.SFC<XAxisProps> = ({ stage, ticks, scale, formatter, tickSize }) => { | ||
const labelY = tickSize + TEXT_OFFSET_X; | ||
const linePositionY = roundToHalfPx(0); | ||
const lines = ticks.map((tick: number) => { | ||
const x = roundToHalfPx(scale(tick)); | ||
return <line className="tick" key={String(tick)} x1={x} y1={0} x2={x} y2={tickSize} />; | ||
}); | ||
const labels = ticks.map((tick: number) => { | ||
const x = scale(tick); | ||
return <text className="label axis-label-x" key={String(tick)} x={x} y={labelY}>{formatter(tick)}</text>; | ||
}); | ||
|
||
return (<g className="axis" transform={stage.getTransform()}> | ||
{lines} | ||
{labels} | ||
<line className="border" y1={linePositionY} y2={linePositionY} x1={0} x2={stage.width}/> | ||
</g>); | ||
}; |
This file contains 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,54 @@ | ||
/* | ||
* Copyright 2017-2022 Allegro.pl | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as React from "react"; | ||
|
||
import "./scatterplot.scss"; | ||
|
||
import { Stage } from "../../../common/models/stage/stage"; | ||
import { Unary } from "../../../common/utils/functional/functional"; | ||
import { roundToHalfPx } from "../../utils/dom/dom"; | ||
import { LinearScale } from "../../utils/linear-scale/linear-scale"; | ||
|
||
const TEXT_OFFSET_Y = 4; | ||
|
||
interface YAxisProps { | ||
stage: Stage; | ||
ticks: number[]; | ||
tickSize: number; | ||
scale: LinearScale; | ||
formatter: Unary<number, string>; | ||
} | ||
|
||
export const YAxis: React.SFC<YAxisProps> = ({ formatter, stage, tickSize, ticks, scale }) => { | ||
const linePositionX = roundToHalfPx(stage.width); | ||
|
||
const lines = ticks.map((tick: number) => { | ||
const y = roundToHalfPx(scale(tick)); | ||
return <line className="tick" key={String(tick)} x1={stage.width - tickSize} y1={y} x2={stage.width} y2={y} />; | ||
}); | ||
|
||
const labels = ticks.map((tick: number) => { | ||
const y = scale(tick); | ||
const labelX = y + TEXT_OFFSET_Y; | ||
return <text className="label" key={String(tick)} x={0} y={labelX}>{formatter(tick)}</text>; | ||
}); | ||
|
||
return <g className="axis" transform={stage.getTransform()}> | ||
<line className="border" x1={linePositionX} y1={0} x2={linePositionX} y2={stage.height} /> | ||
{lines} | ||
{labels} | ||
</g>; | ||
}; |