I am trying to fill the background color with the gradient color just like below image. I have tried multiple methods to achieve the gradient but failed in most of them.
This is what I have tried till now:
import React from "react";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler,
} from "chart.js";
import { Line } from "react-chartjs-2";
import { colors, hexToRGB } from "@/constant/data";
import useDarkMode from "@/hooks/useDarkMode";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler
);
const LineChart = () => {
const [isDark] = useDarkMode();
//using the variable to add gradient color
const gradientColor = {
type: "linear",
x0: 0,
y0: 0,
x1: 0,
y1: 1,
colorStops: [
{ offset: 0, color: "rgba(126, 234, 184, 1)" },
{ offset: 0.5, color: "rgba(126, 234, 184, 0.7)" },
{ offset: 1, color: "rgba(126, 234, 184, 0)" },
],
};
const data = {
labels: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140],
datasets: [
{
label: " data one",
data: [
80, 150, 180, 270, 210, 160, 160, 202, 265, 210, 270, 255, 290, 360,
375,
],
fill: true,
// adding the defined variable with gradient color in backgroundColor
backgroundColor: gradientColor,
borderColor: colors.primary,
borderSkipped: "bottom",
barThickness: 40,
pointRadius: 1,
pointHoverRadius: 5,
pointHoverBorderWidth: 5,
pointBorderColor: "transparent",
lineTension: 0.5,
pointStyle: "circle",
pointShadowOffsetX: 1,
pointShadowOffsetY: 1,
pointShadowBlur: 5,
},
{
label: " data two",
data: [
80, 125, 105, 130, 215, 195, 140, 160, 230, 300, 220, 170, 210, 200,
280,
],
fill: true,
backgroundColor: "rgb(126, 234, 184, 0.7)",
borderColor: colors.success,
borderSkipped: "bottom",
barThickness: 40,
pointRadius: 1,
pointHoverRadius: 5,
pointHoverBorderWidth: 5,
pointBorderColor: "transparent",
lineTension: 0.5,
pointStyle: "circle",
pointShadowOffsetX: 1,
pointShadowOffsetY: 1,
pointShadowBlur: 5,
},
{
label: " data three",
data: [
80, 99, 82, 90, 115, 115, 74, 75, 130, 155, 125, 90, 140, 130, 180,
],
//here i tried adding the gradient color directly but did not work
fill: true,
backgroundColor:
"linear-gradient(90deg, rgba(71,128,233,1) 5%, rgba(233,104,137,1) 68%)",
borderColor: colors.danger,
borderSkipped: "bottom",
barThickness: 40,
pointRadius: 1,
pointHoverRadius: 5,
pointHoverBorderWidth: 5,
pointBorderColor: "transparent",
lineTension: 0.5,
pointStyle: "circle",
pointShadowOffsetX: 1,
pointShadowOffsetY: 1,
pointShadowBlur: 5,
},
],
};
const options = {
responsive: true,
tension: 0.3,
maintainAspectRatio: false,
plugins: {
legend: {
labels: {
color: isDark ? "#cbd5e1" : "#475569",
},
},
},
scales: {
y: {
stacked: true,
grid: {
color: isDark ? "#334155" : "#e2e8f0",
},
ticks: {
display: false,
},
},
x: {
grid: {
color: isDark ? "#334155" : "#e2e8f0",
},
ticks: {
color: isDark ? "#cbd5e1" : "#475569",
},
},
},
};
return (
<div>
<Line options={options} data={data} height={350} />
</div>
);
};
export default LineChart;
This is what I got from above code:
I guess this is possible using canvas but I am not sure how to use that.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/eqPQ0vr
Comments
Post a Comment