question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: adding a gradient line using Typescript throws an error

See original GitHub issue

🦄

  • Would you like to work on a fix?

Current and expected behavior

the library cannot compile when i try to use canvas.getProperty using Typescript, while trying to create a gradient line in a react-chart.js Line component

Typescripts throws this error :

 TS2741: Property 'datasets' is missing in type '(canvas: { getContext: (arg0: string) => any; }) => { labels: string[]; datasets: { label: string; data: number[]; tension: number; pointStyle: string; pointRadius: number; pointBorderWidth: number; pointBorderColor: string; borderWidth: number; backgroundColor: string; fill: boolean; borderColor: any; }[]; }' but required in type 'ChartData<"line", (number | ScatterDataPoint | null)[], unknown>'.
    115 |   return (
    116 |     <div className="graph">  
  > 117 |      <Line options={options} data={data} />
        |                                   ^^^^^^
    118 |      <Actions />
    119 |     </div>
    120 |   )

I am expecting to see my chart showing a gradient line

the error is brought up when i try to use canvas.getContext to create a gradient and add it to the borderColor property of my datasets:

  const data = (canvas: { getContext: (arg0: string) => any; }) => {
    const ctx = canvas.getContext("2d");
    const gradient = ctx.createLinearGradient(0, 0, 0, 200);
    gradient.addColorStop(0, 'rgba(250,174,50,1)');   
    gradient.addColorStop(1, 'rgba(250,174,50,0)');

    const result = {
      labels: hours,
      datasets: [
        {
          label: 'currency value in $',
          data : priceLabel,
          tension:0.5,
          pointStyle: 'circle',
          pointRadius:1,
          pointBorderWidth:0,
          pointBorderColor:'black',
          borderWidth:2,
          backgroundColor: 'black',
          fill:true,
          borderColor: gradient,
          
        },
      ]
    }
    return result
  }

return (
    <div className="graph">  
     <Line options={options} data={data} />
     <Actions />
    </div>
  )

Reproduction

https://github.com/regola-Tacens/BinanceChart/tree/bug/gradient-line

chart.js version

3.0.0

react-chartjs-2 version

4.0.0

Possible solution

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
dangreencommented, Jan 5, 2022

@regola-Tacens Hi. data prop must be an object. How to add a gradient to the chart you can see in this example.

0reactions
OlekRiacommented, Jan 5, 2022

@dangreen in Chart.js component you wrapped there is a property fill. Due the documentation in Chart.js I put this fill property value as ‘start’.

In result I have no effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LinearGradient `gradient` prop error with TypeScript · Issue #173
I have defined a gradient inside theme.linearGradients and the Gradient component throws the following error whenever I try accessing the ...
Read more >
how to create a react-chart.js gradient line in typescript
Evertything is fine up to the point where i am trying to create a gradient line, This is where typescript throws an error...
Read more >
CanvasRenderingContext2D.createLinearGradient() - Web APIs
createLinearGradient () method of the Canvas 2D API creates a gradient along the line connecting two given coordinates.
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
parseroptions.project has been set for @typescript-eslint/parser
Create a new tsconfig.eslint.json Add .eslintrc in .eslintignore Ensure that ONLY ... The error occurs when Typescript does not have a file to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found