Gradient in JavaScript Stock chart control
18 Nov 201816 minutes to read
Gradients add depth and modern styling to charts by smoothly blending multiple colors. The Charts component supports two gradient types:
- Linear gradient
- Radial gradient
Gradients can be applied to:
- Series
- Trendlines
- Technical Indicators
Linear gradient
A linear gradient blends color along side a straight path from a defined start point to an end point. Configure it by adding linearGradient inside the target element (Series, Trendlines or Indicators) and define one or more color stops that control how colors transition across the gradient. Set the start and end positions of the gradient using x1, y1, x2 and y2 properties. The gradient color stop values such as offset, color, opacity, lighten and brighten are set using the gradientColorStop property.
In the linearGradient:
-
x1- Sets the horizontal start position of the gradient (0 to 1). -
y1- Sets the vertical start position of the gradient (0 to 1). -
x2- Sets the horizontal end position of the gradient (0 to 1). -
y2- Sets the vertical end position of the gradient (0 to 1).
In the gradientColorStop:
-
offset- Specifies the position of the color stop along the gradient (0 to 100). -
color- Sets the color at the stop. -
opacity- Defines the transparency of the stop (0 to 1). -
lighten- Adjusts lightness at the stop. Positive values lighten the color. Range: 0 to 1. -
brighten- Adjusts brightness at the stop. Positive values increase brightness; negative values decrease it. Ranges: -1 to 1.
Series
Apply a linear gradient to a series by adding linearGradient inside the target Series. The same gradient is applied to the series markers, legend symbol and tooltip marker for visual consistency.
var stockChart = new ej.charts.StockChart({
primaryXAxis: {
valueType: 'DateTime',
},
primaryYAxis: {
interval: 10
},
series: [
{
name: 'AAPL',
dataSource: StockDetails,
type: 'Candle',
xName: 'Date', high: 'High', low: 'Low', open: 'Open', close: 'Close', volume: 'Volume',
linearGradient: {
x1: 0, y1: 0,
x2: 1, y2: 1,
gradientColorStop: [
{ color: '#FF7E5F', offset: 0, opacity: 1, lighten: 0, brighten: 0 },
{ color: '#FEB47B', offset: 100, opacity: 1, lighten: 0, brighten: 0.5 },
]
}
}
],
title: 'AAPL Stock Price',
});
stockChart.appendTo('#element');<!DOCTYPE html><html lang="en"><head>
<title>Chart dimension</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="./index.css" rel="stylesheet">
<link href="./material.css" rel="stylesheet">
<link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Technical Indicators
Apply a linear gradient to a technical indicator by adding linearGradient inside the target Indicator.
var stockChart = new ej.charts.StockChart({
chartArea: { border: { width: 0 } },
primaryYAxis: {
lineStyle: { color: 'transparent' },
majorTickLines: { color: 'transparent', width: 0 },
},
border: { width: 0 },
primaryXAxis: { majorGridLines: { color: 'transparent' }, crosshairTooltip: { enable: true } },
series: [
{
dataSource: stockchartData, name: 'Apple Inc',
type: 'Candle'
},
],
indicators: [{
type: 'Ema', field: 'Close', seriesName: 'Apple Inc', xName: 'date', high: 'high', low: 'low', open: 'open', close: 'close',
period: 10,
linearGradient: {
x1: 0, y1: 0,
x2: 0, y2: 1,
gradientColorStop: [
{ color: 'red', offset: 0, opacity: 1 },
{ color: 'blue', offset: 100, opacity: 1 },
]
}
}],
seriesType: [],
exportType: [],
trendlineType: [],
tooltip: {
enable: true
},
crosshair: {
enable: true
},
height: '350',
});
stockChart.appendTo('#element');<!DOCTYPE html><html lang="en"><head>
<title>Chart dimension</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="./index.css" rel="stylesheet">
<link href="./material.css" rel="stylesheet">
<link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Radial gradient
A radial gradient blends colors outward from a central point, creating a circular or elliptical color progression. Configure it by adding radialGradient inside the target element (Series, Trendline, or Indicator) and define one or more color stops to control how colors transition from the center to the outer edge. Set the gradient’s center, optional focal point, and radius using radialGradient properties. The color stop values such as offset, color, opacity, lighten, and brighten are set using the gradientColorStop property.
In the radialGradient:
-
cx- Sets the normalized horizontal center of the gradient (0 to 1). -
cy- Sets the normalized vertical center of the gradient (0 to 1). -
fx- Sets the normalized horizontal focal point from which the gradient appears to originate (0 to 1). -
fy- Sets the normalized vertical focal point (0 to 1). -
r- Sets the normalized radius of the gradient circle (0 to 1).
In the gradientColorStop:
-
offset- Specifies the position of the color stop along the gradient (0 to 100). -
color- Sets the color at the stop. -
opacity- Defines the transparency of the stop (0 to 1). -
lighten- Adjusts lightness at the stop. Positive values lighten the color. Range: 0 to 1. -
brighten- Adjusts brightness at the stop. Positive values increase brightness; negative values decrease it. Ranges: -1 to 1.
Series
Apply a radial gradient to a series by adding radialGradient inside the target Series. The same gradient is applied to the series markers, legend symbol and tooltip marker for visual consistency.
var stockChart = new ej.charts.StockChart({
primaryXAxis: {
majorGridLines: {
width: 0
},
},
primaryYAxis: {
majorGridLines: {
width: 0
},
interval: 10
},
series: [
{
dataSource: stockData,
xName: 'x',
radialGradient: {
cx: 0.5, cy: 0.5, r: 0.5,
fx: 0.5, fy: 0.5,
gradientColorStop: [
{ color: '#FFFF00', offset: 0, opacity: 1, lighten: 0, brighten: 0 },
{ color: '#7C3AED', offset: 100, opacity: 1, lighten: 0, brighten: 0 },
]
}
}
],
title: 'AAPL Stock Price',
});
stockChart.appendTo('#element');<!DOCTYPE html><html lang="en"><head>
<title>Chart dimension</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="./index.css" rel="stylesheet">
<link href="./material.css" rel="stylesheet">
<link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Technical Indicators
Apply a radial gradient to a technical indicator by adding radialGradient inside the target Indicator.
var stockChart = new ej.charts.StockChart({
chartArea: { border: { width: 0 } },
primaryYAxis: {
lineStyle: { color: 'transparent' },
majorTickLines: { color: 'transparent', width: 0 },
},
border: { width: 0 },
primaryXAxis: { majorGridLines: { color: 'transparent' }, crosshairTooltip: { enable: true } },
series: [
{
dataSource: stockchartData, name: 'Apple Inc',
type: 'Candle'
},
],
indicators: [{
type: 'Ema', field: 'Close', seriesName: 'Apple Inc', xName: 'date', high: 'high', low: 'low', open: 'open', close: 'close',
period: 10,
radialGradient: {
cx: 0.5, cy: 0.5, r: 0.5,
fx: 0.5, fy: 0.5,
gradientColorStop: [
{ color: 'red', offset: 0, opacity: 1 },
{ color: 'blue', offset: 100, opacity: 1 },
]
}
}],
seriesType: [],
exportType: [],
trendlineType: [],
tooltip: {
enable: true
},
crosshair: {
enable: true
},
height: '350',
});
stockChart.appendTo('#element');<!DOCTYPE html><html lang="en"><head>
<title>Chart dimension</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="./index.css" rel="stylesheet">
<link href="./material.css" rel="stylesheet">
<link href="http://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>