英文:
How does the API for ECharts LinearGradient work?
问题
正如在这个问题中所示,我们可以像这样向 ECharts 添加线性渐变:
areaStyle: {
color: new graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: 'rgb(255, 158, 68)'
},
{
offset: 1,
color: 'rgb(255, 70, 131)'
}])
}
在我看过的所有示例中,LinearGradient
实例都以 LinearGradient(0, 0, 0, 1
开始... 我想知道参数 0, 0, 0, 1
的含义/作用是什么?
英文:
As illustrated in this question we can add a linear gradient to an echart like this:
areaStyle: {
color: new graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: 'rgb(255, 158, 68)'
},
{
offset: 1,
color: 'rgb(255, 70, 131)'
}])
}
Also created a Stackblitz demo here.
In all the examples I've seen the LinearGradient
instance starts with LinearGradient(0, 0, 0, 1
... and I'm wondering what the arguments 0, 0, 0, 1
mean / do?
答案1
得分: 3
以下是翻译好的部分:
对于LinearGradient
,坐标是整个绘图区域:
- 如果
global
参数为false
(默认值):(0, 0)
是左上角,(1, 1)
是右下角 - 如果
global
为true
,那么坐标以像素为单位,同样(0, 0)
是左上角,但右下角的坐标是(w-1, h-1)
,其中w
和h
是绘图区域的宽度和高度,以像素为单位。
(x0, y0)
- (x1, y1)
是在这些坐标中的一段线段的端点:沿着这个线段(以及所有平行线)颜色根据stops
中的数据变化。沿着与这个线段垂直的线,颜色将保持不变。
梯度只能以整个绘图区域的术语来表示的事实通常是一个缺点:人们可能希望填充颜色是所绘制的函数值的一部分,但这是不可能的。
示例:
LinearGradient(0, 0, 0, 1, ....)
表示一个垂直变化的梯度(从上=0到下=1),而所有水平线都具有相同的颜色。它等同于LinearGradient(1, 0, 1, 1, ....)
或LinearGradient(0.43, 0, 0.43, 1, ....)
。LinearGradient(0, 0, 1, 0, ....)
表示一个水平变化的梯度(从左=0到右=1),而所有垂直线都具有相同的颜色。LinearGradient(0, 0, 1, 1, ....)
沿着绘图区域的左上到右下以依赖于绘图区域的宽高比的角度变化(如果宽度和高度相等,则为45°)。
colorStops
是一个带有color
和offset
属性的对象数组。偏移量是从0到1的数字,表示在(x0, y0)
-(x1, y1)上的点,显然,
offset: 0是
(x0, y0),
offset: 1表示
(x1, y1),
offset 0.5`是线段的中间。
梯度的颜色在该线段上由线性插值计算,从每两个连续的colorStops
之间进行插值。
在偏移之外(如果第一个偏移大于0或最后一个偏移小于1)以及线段之外(如果(x0, y0)
和/或(x1, y1)
是(0, 0)
- (1, 1)
内部的点),颜色是恒定的(即第一个偏移的颜色或最后一个偏移的颜色)。
在下面的示例中,左侧图表有两个颜色停止,顶部左侧是蓝色,底部右侧是黄色。右侧的图表有四个颜色停止:底部左侧是蓝色,顶部右侧和两个红色颜色停止之间有两个红色颜色停止 - 在这两者之间,颜色始终是红色。
另一种常常使用的技术是为不同的偏移间隔设置颜色常量(就像上面的红色一样),并为零长度的间隔改变颜色(使用不同的颜色重复相同的偏移),从而产生均匀的彩色条纹。
希望这有所帮助!如果您需要更多信息,请随时提问。
英文:
For the LinearGradient
coordinates are for the whole plot area:
- if
global
parameter isfalse
(default):(0, 0)
is the top left corner,(1, 1)
is the bottom-right corner - if
global
istrue
, then the coordinates are in pixels, again(0, 0)
is the top left-corner, but the coordinates of the right-bottom corner are(w-1, h-1)
, wherew
andh
are the width and height of the plot area, in pixels.
(x0, y0)
- (x1, y1)
are the ends of a segment in these coordinates: along the line of this segment (and along all parallel lines) colors change according to the data in the stops
. Along lines perpendicular to this segment the colors will be the same.
The fact that the gradient can be expressed only in terms of the whole plot area is often a disadvantage: one would want for instance the fill colors to be a fraction of the value of the function that is plotted, but that is not possible.
Examples:
LinearGradient(0, 0, 0, 1, ....)
means a gradient that changes vertically (from top=0 to bottom=1), while all horizontal
lines have the same color. It is equivalent toLinearGradient(1, 0, 1, 1, ....)
orLinearGradient(0.43, 0, 0.43, 1, ....)
for that matter.LinearGradient(0, 0, 1, 0, ....)
means a gradient that changes horizontally (from left=0 to right=1), while all vertical
lines have the same color.LinearGradient(0, 0, 1, 1, ....)
changes along the top-left to bottom-right at an angle that depends on the aspect ratio of the plot area (would be 45<sup>0</sup> if the width and height were equal).
colorStops
is an array of objects with color
and offset
properties. Offsets are numbers from 0 to 1, signifying points on the (x0, y0)
-(x1, y1), obviously,
offset: 0is
(x0, y0),
offset: 1means
(x1, y1)and
offset 0.5` is the middle of the segment.
The colors of the gradient are calculated along that segment by linear interpolation between each two consecutive colorStops
.
Outside the offsets (if the first offset is greater than 0 or the last one less than 1) and outside the segment (if (x0, y0)
and/or (x1, y1)
are points interior to the (0, 0)
- (1, 1)
the color is constant (that is the color of the first or the color of the last offset).
In the example below, the left chart has two color stops, blue at top-left and yellow at bottom-right. The chart on the right has four color stops: blue at bottom-left and yellow at top-right and in between two red color stops - in between those the color is constant as the interpolation yields always red.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myChart1 = echarts.init(document.getElementById('chart-container1'), null, {
renderer: 'canvas',
useDirtyRect: false
});
const myChart2 = echarts.init(document.getElementById('chart-container2'), null, {
renderer: 'canvas',
useDirtyRect: false
});
const option = {
xAxis: {
type: 'value',
data: [1, 2, 3]
},
yAxis: {
type: 'value'
},
series: [{
data: [
[0, 200],
[1, 190],
[2, 200]
],
type: 'line'
}]
};
myChart1.setOption(option);
myChart2.setOption(option);
myChart1.setOption({
series: [{
areaStyle: {
opacity: 0.9,
color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
offset: 0,
color: 'blue',
},
{
offset: 1,
color: 'yellow',
},
]),
}
}]
});
myChart2.setOption({
series: [{
areaStyle: {
opacity: 0.9,
color: new echarts.graphic.LinearGradient(0, 1, 1, 0, [{
offset: 0,
color: 'blue',
},
{
offset: 0.2,
color: 'red',
},
{
offset: 0.8,
color: 'red',
},
{
offset: 1,
color: 'yellow',
},
]),
}
}]
})
<!-- language: lang-html -->
<div id="chart-container1" style="height:300px; width:45vw;display:inline-block"></div>
<div id="chart-container2" style="height:300px; width:45vw; display:inline-block"></div>
<script src="https://fastly.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
<!-- end snippet -->
Another technique used often is to set the color constant for various offset intervals (as was the case with red above) and change it for intervals of zero length, (repeating the same offset with a different color) producing uniform color stripes, like in:
[{
offset: 0,
color: 'blue' // start of blue stripe
},
{
offset: 0.33,
color: 'blue' // end of blue stripe
},
{
offset: 0.33, // start of red stripe
color: 'red'
},
{
offset: 0.67, // end of red stripe
color: 'red'
}
//.....
]
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myChart3 = echarts.init(document.getElementById('chart-container3'), null, {
renderer: 'canvas',
useDirtyRect: false
});
const option = {
xAxis: {
type: 'value',
data: [1, 2, 3]
},
yAxis: {
type: 'value'
},
series: [{
data: [
[0, 200],
[1, 190],
[2, 200]
],
type: 'line'
}]
};
myChart3.setOption(option);
myChart3.setOption({
series: [{
areaStyle: {
opacity: 0.9,
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0.1, // almost vertical
[{
offset: 0,
color: 'blue', // start of blue stripe
},
{
offset: 0.33,
color: 'blue', // end of blue stripe
},
{
offset: 0.33,
color: 'red', // start of red stripe
},
{
offset: 0.67, // end of red stripe
color: 'red',
},
{
offset: 0.67, // start of green stripe
color: 'green',
},
{
offset: 1,
color: 'green', // end of green stripe
},
]),
}
}]
});
<!-- language: lang-html -->
<div id="chart-container3" style="height:300px; width:45vw;display:inline-block"></div>
<script src="https://fastly.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论