Skip to content

How can I attach a label to an end of a line? #1788

Answered by williaster
yusuf8ahmed asked this question in Q&A
Discussion options

You must be logged in to vote

hey @yusuf8ahmed, you can easily tap into the chart's DataContext to access data and scales. here's a sandbox (which may not work, they are updating their pay plan in a couple of days, so pasted the Label component below)

import React, { useContext } from "react";
import { DataContext, XYChart, ... } from "@visx/xychart";

// chart
const Chart = () => (
  <XYChart {...}>
     {...}

     {/** data keys for this chart are a/b */}
     <Label dataKey="a" />
     <Label dataKey="b" />
  </XYChart>
);

// custom label component
const Label = ({ dataKey }: { dataKey: string }) => {
  const { theme, xScale, yScale, dataRegistry, colorScale } = useContext(DataContext);
  const data = dataRegistry

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@williaster
Comment options

Answer selected by yusuf8ahmed
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants