Personalized dot shapes on a line graph with Rechart

I found a helpful resource for customizing dot shapes in line charts at

In my chart, I have four lines and I want each line to have a different shape for its data points.

I tried implementing the SVG shapes provided in the link, but I need shapes like circles, triangles, rectangles/squares, and diamonds.

Here is an excerpt from my code:

const CustomizedDot = (props) => {
    const { cx, cy, dataKey } = props;

    if (dataKey === 'A') {
        return (
            <svg>
                <rect />
            </svg>
        );
    }
    // Add more conditions for other shapes here...
};

And this is how the line chart is generated:

<LineChart
    data={data.map((data) => ({
        ...data,
        date: data.date,
    }))}
>
    <XAxis dataKey="date" />
    <YAxis />
    <Tooltip />
    <Legend />
    {/* Add your customized dots for each line */}
</LineChart>

However, it seems that something is not working correctly as all lines display circle-shaped dots instead of the specified shapes. Any ideas on what might be causing this issue?

Answer №1

After reviewing the code snippet you provided, it appears that there may be a minor issue in your implementation of the CustomizedDot component. It seems like essential attributes, such as cx, cy, and r for circles, are missing from the SVG shapes. Below is an updated version of the code:

const CustomizedDot = (props) => {
    const { cx, cy, dataKey } = props;

    if (dataKey === 'A') {
        return (
            <svg>
                <rect x={cx - 4} y={cy - 4} width={8} height={8} />
            </svg>
        );
    }
    if (dataKey === 'B') {
        return (
            <svg>
                <polygon points={`${cx},${cy - 4} ${cx + 4},${cy + 4} ${cx - 4},${cy + 4}`} />
            </svg>
        );
    }
    if (dataKey === 'C') {
        return (
            <svg>
                <circle cx={cx} cy={cy} r={4} />
            </svg>
        );
    }
    if (dataKey === 'D') {
        return (
            <svg>
                <polygon points={`${cx - 2},${cy - 4} ${cx + 2},${cy - 4} ${cx},${cy + 4}`} />
            </svg>
        );
    }
};

In this updated version, we have included the necessary cx and cy attributes to ensure proper positioning of the shapes based on the data point coordinates. Additionally, attributes like width, height, r, and points have been added to each shape to enhance their display.

I trust that this information proves beneficial!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The <select><option> is being returned as a String instead of a Long value

Encountering an issue while trying to add a Contact with its email and the id of the associated Company : 2018-06-24 23:03:04.743 WARN 11360 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework ...

How can I incorporate percentage values into input text in Angular?

How can I include a percent sign in an input field using Angular, without relying on jQuery? I am looking for a solution that is identical to what I would achieve with jQuery. Here is the current status of my project: ...

Updating an Angular mat-table when the drawer is located in a separate component: A complete guide

The primary element contains tabs, component A, B, and so on. My component_A comes with preloaded information, but it also includes a filter button. Additionally, there is a button that triggers a drawer from another component (the main component). toggl ...

Upon updating my application from Angular 14 to 16, I encountered an overwhelming number of errors within the npm packages I had incorporated

After upgrading my angular application from v14 to v16, I encountered numerous peer dependencies issues, which led me to use the --force flag for the upgrade process. However, upon compiling, I am now faced with a multitude of errors as depicted in the scr ...

What is the method for removing an item from my TypeScript to-do list?

I am fairly new to TypeScript and I'm currently facing some challenges in deleting an item from my to-do list. I could use some guidance on how to go about implementing this feature. I have created a deleteHandler function that needs to be integrated ...

Guide to sorting through an array for a particular keyword

In my array, I have objects with IDs and names. For example: { [ { "id": 1, "name": "this is book", }, { "id": 2, "name": "this is a test book", }, { "id": 3, "name": "this is a desk", } ] } Now, let's say I want ...

Custom Angular Form Control Implementation

Greetings! I'm currently in the process of developing a collection of custom reactive form controls to make it easier for templates. So far, I've successfully created one using the ControlValueAccessor interface. The form editing functionality is ...

Issue with displaying the X Axis on the KendoUI Line Graph for Angular

I am having trouble displaying the X Axis Label in KendoUI Line Graph for Angular. I attempted to modify the code following an example, but it didn't work as expected. Here is the code snippet that I used: k-series="[ { ...

AngularJS 1.6.1, TypeScript, UI Bootstrap Modal: How is my session storage data getting modified by the modal window?

Seeking assistance from the community. I am utilizing ngStorage to store some global variables: //Global Controller saveGlobalSettings = () => { this.$sessionStorage.QuoteTimes = this.quoteTimes; this.$sessionStorage.QuoteTypes = this.q ...

Zod handling the visual component

I am currently working on a project using react-file-base64 to handle file metadata such as name, type, size, and base64 encoding. Despite registering zod validation, I encountered an "image required error" even after uploading the image. Upon investigatio ...

Encountering a Typescript Type error when attempting to include a new custom property 'tab' within the 'Typography' component in a Material UI theme

Currently, I am encountering a Typescript Type error when attempting to add a custom new property called 'tab' inside 'Typography' in my Material UI Theme. The error message states: Property 'tab' does not exist on type &apos ...

Typescript Error: Trying to access a property that is undefined

I am a beginner in typescript and believe that the issue I am facing is related to typescript. Currently, I am working on an Ionic app where I have a function setwall() being called from home.html. The setwall() function is defined in home.ts. However, whe ...

Pause and await for user input within an Angular function

I am facing an issue with my Angular function where I need to wait for a response from the user. I believe that using "subscribe()" might help, but I am unclear on how and when to use it properly. My current call looks like this: theAnswer:boolean = await ...

Define the static property as an array containing instances of the same type

I created a class called Foo with a static property named instances that holds references to all instances. Then, I have another class called Bar which extends Foo: class Foo { static instances: Foo[]; fooProp = "foo"; constructor() { ...

The TypeScript compilation is missing Carousel.d.ts file. To resolve this issue, ensure that it is included in your tsconfig either through the 'files' or 'include' property

While trying to build an Angular application for server-side execution, I encountered the following errors: ERROR in ./src/app/shared/components/carousel/interface/Carousel.d.ts Module build failed: Error: /home/training/Desktop/vishnu/TemplateAppv6/src ...

Is there a way to conceal automatically generated files from TypeScript in NerdTree?

Is there a way to conceal the automatically created files (.js and .js.map) generated by the TypeScript compiler in NERDTree? ...

Why is my root page not dynamic in Next.js 13?

I am currently working on a website using Next.js version 13.0. After running the next build command, I noticed that all pages are functioning properly except for the root page. The issue is that it's being generated as a static page instead of dynami ...

The bar chart functions perfectly on localhost but encounters issues after being hosted on Gitpage

Localhost Gitpage The bar chart was displaying correctly on localhost, but once it was hosted on Gitpage, it began to show issues. Any suggestions on how to resolve this? Repository Link: https://github.com/mzs21/bar-chart Live Preview: ...

Checking if a string in Typescript contains vowels using Regex

Is there anyone who can assist me with creating a regex to check if a string contains vowels? EX : Hi Team // True H // False I have tried using the following regex but it's not giving me the desired outcome. [aeiou] ...

How to send variables to a function when a value changes in TypeScript and Angular

As someone who is new to Angular and HTML, I am seeking assistance with the following code snippet: <mat-form-field> <mat-select (valueChange)="changeStatus(list.name, card.name)"> <mat-option *ngFor="let i of lists"> {{i.name}} ...