Skip to content

Commit

Permalink
fix(charts): edge coordinates can now use rectWidth
Browse files Browse the repository at this point in the history
The edge coordinates where always sizing the width to the text width,
this is now optional.
  • Loading branch information
markmcdowell committed Dec 16, 2019
1 parent e687fb7 commit 5b0a20b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/charts/src/coordinates/EdgeCoordinateV3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ export function drawOnCanvas(ctx: CanvasRenderingContext2D, props) {
ctx.font = `${fontSize}px ${fontFamily}`;
ctx.textBaseline = "middle";

const width = Math.round(ctx.measureText(coordinate).width + 10);
let width = props.rectWidth;
if (props.fitToText) {
width = Math.round(ctx.measureText(coordinate).width + 10);
}

const edge = helper({ ...props, rectWidth: width });
if (edge === null) {
Expand Down
2 changes: 2 additions & 0 deletions packages/charts/src/coordinates/EdgeIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface EdgeIndicatorProps {
readonly displayFormat?: any; // func
readonly edgeAt?: "left" | "right";
readonly fill?: string | any; // func
readonly fitToText?: boolean;
readonly itemType: "first" | "last";
readonly lineStroke?: string | any; // func
readonly lineStrokeDasharray?: strokeDashTypes;
Expand All @@ -29,6 +30,7 @@ export class EdgeIndicator extends React.Component<EdgeIndicatorProps> {
public static defaultProps = {
className: "react-financial-charts-edgeindicator",
type: "horizontal",
fitToText: false,
orient: "right",
edgeAt: "right",
textFill: "#FFFFFF",
Expand Down
5 changes: 4 additions & 1 deletion packages/charts/src/coordinates/MouseCoordinateX.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface MouseCoordinateXProps {
readonly customX: any; // func
readonly displayFormat: any; // func
readonly fill?: string;
readonly fitToText?: boolean;
readonly fontFamily?: string;
readonly fontSize?: number;
readonly opacity?: number;
Expand Down Expand Up @@ -47,6 +48,7 @@ export class MouseCoordinateX extends React.Component<MouseCoordinateXProps> {
at: "bottom",
customX: defaultCustomX,
fill: "#37474F",
fitToText: true,
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
fontSize: 13,
opacity: 1,
Expand Down Expand Up @@ -99,7 +101,7 @@ export class MouseCoordinateX extends React.Component<MouseCoordinateXProps> {
const { customX, orient, at } = props;
const { stroke, strokeOpacity, strokeWidth } = props;
const { rectRadius, rectWidth, rectHeight } = props;
const { fill, opacity, fontFamily, fontSize, textFill } = props;
const { fill, opacity, fitToText, fontFamily, fontSize, textFill } = props;

const edgeAt = (at === "bottom") ? height : 0;

Expand All @@ -115,6 +117,7 @@ export class MouseCoordinateX extends React.Component<MouseCoordinateXProps> {

const coordinateProps = {
coordinate,
fitToText,
show,
type,
orient,
Expand Down
5 changes: 4 additions & 1 deletion packages/charts/src/coordinates/MouseCoordinateY.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface MouseCoordinateYProps {
readonly fontFamily?: string;
readonly fontSize?: number;
readonly fill?: string;
readonly fitToText?: boolean;
readonly opacity?: number;
readonly orient?: "left" | "right";
readonly rectWidth?: number;
Expand All @@ -32,6 +33,7 @@ export class MouseCoordinateY extends React.Component<MouseCoordinateYProps> {
at: "right",
dx: 0,
fill: "#37474F",
fitToText: false,
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
fontSize: 13,
opacity: 1,
Expand Down Expand Up @@ -103,7 +105,7 @@ export function getYCoordinate(y, displayValue, props, moreProps) {
const { width } = moreProps;

const { orient, at, rectWidth, rectHeight, dx } = props;
const { fill, opacity, fontFamily, fontSize, textFill, arrowWidth } = props;
const { fill, opacity, fitToText, fontFamily, fontSize, textFill, arrowWidth } = props;
const { stroke, strokeOpacity, strokeWidth } = props;

const x1 = 0;
Expand All @@ -116,6 +118,7 @@ export function getYCoordinate(y, displayValue, props, moreProps) {
const coordinateProps = {
coordinate: displayValue,
show: true,
fitToText,
type,
orient,
edgeAt,
Expand Down
7 changes: 5 additions & 2 deletions packages/stories/src/features/stockChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ class StockChart extends React.Component<StockChartProps> {
<CandlestickSeries />
<LineSeries yAccessor={ema26.accessor()} stroke={ema26.stroke()} />
<LineSeries yAccessor={ema12.accessor()} stroke={ema12.stroke()} />
<MouseCoordinateY displayFormat={this.pricesDisplayFormat} />
<MouseCoordinateY
rectWidth={margin.right}
displayFormat={this.pricesDisplayFormat} />
<EdgeIndicator
itemType="last"
rectWidth={margin.right}
fill={this.openCloseColor}
lineStroke={this.openCloseColor}
displayFormat={this.pricesDisplayFormat}
Expand Down Expand Up @@ -154,7 +157,7 @@ class StockChart extends React.Component<StockChartProps> {
<YAxis ticks={4} tickFormat={this.pricesDisplayFormat} />

<MouseCoordinateX displayFormat={timeDisplayFormat} />
<MouseCoordinateY displayFormat={this.pricesDisplayFormat} />
<MouseCoordinateY rectWidth={margin.right} displayFormat={this.pricesDisplayFormat} />

<ElderRaySeries yAccessor={elder.accessor()} />

Expand Down

0 comments on commit 5b0a20b

Please sign in to comment.