ECharts LinearGradient的API如何工作?

huangapple go评论88阅读模式
英文:

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)'
            }])
        }

这里还创建了一个 Stackblitz 演示

在我看过的所有示例中,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)是右下角
  • 如果globaltrue,那么坐标以像素为单位,同样(0, 0)是左上角,但右下角的坐标是(w-1, h-1),其中wh是绘图区域的宽度和高度,以像素为单位。

(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是一个带有coloroffset属性的对象数组。偏移量是从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 is false (default): (0, 0) is the top left corner, (1, 1) is the bottom-right corner
  • if global is true, 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), where w and h 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 to LinearGradient(1, 0, 1, 1, ....) or LinearGradient(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)andoffset 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(&#39;chart-container1&#39;), null, {
  renderer: &#39;canvas&#39;,
  useDirtyRect: false
});

const myChart2 = echarts.init(document.getElementById(&#39;chart-container2&#39;), null, {
  renderer: &#39;canvas&#39;,
  useDirtyRect: false
});

const option = {
  xAxis: {
    type: &#39;value&#39;,
    data: [1, 2, 3]
  },
  yAxis: {
    type: &#39;value&#39;
  },
  series: [{
    data: [
      [0, 200],
      [1, 190],
      [2, 200]
    ],
    type: &#39;line&#39;
  }]
};

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: &#39;blue&#39;,
        },
        {
          offset: 1,
          color: &#39;yellow&#39;,
        },
      ]),
    }
  }]
});

myChart2.setOption({
  series: [{
    areaStyle: {
      opacity: 0.9,
      color: new echarts.graphic.LinearGradient(0, 1, 1, 0, [{
          offset: 0,
          color: &#39;blue&#39;,
        },
        {
          offset: 0.2,
          color: &#39;red&#39;,
        },
        {
          offset: 0.8,
          color: &#39;red&#39;,
        },
        {
          offset: 1,
          color: &#39;yellow&#39;,
        },
      ]),
    }
  }]
})

<!-- language: lang-html -->

&lt;div id=&quot;chart-container1&quot; style=&quot;height:300px; width:45vw;display:inline-block&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;chart-container2&quot; style=&quot;height:300px; width:45vw; display:inline-block&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://fastly.jsdelivr.net/npm/echarts@5/dist/echarts.min.js&quot;&gt;&lt;/script&gt;

<!-- 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: &#39;blue&#39; // start of blue stripe
 },
 {
   offset: 0.33,
   color: &#39;blue&#39; // end of blue stripe
 },
 {
   offset: 0.33, // start of red stripe
   color: &#39;red&#39;
 },
 {
   offset: 0.67, // end of red stripe
   color: &#39;red&#39;
 }
//.....
]

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myChart3 = echarts.init(document.getElementById(&#39;chart-container3&#39;), null, {
  renderer: &#39;canvas&#39;,
  useDirtyRect: false
});

const option = {
  xAxis: {
    type: &#39;value&#39;,
    data: [1, 2, 3]
  },
  yAxis: {
    type: &#39;value&#39;
  },
  series: [{
    data: [
      [0, 200],
      [1, 190],
      [2, 200]
    ],
    type: &#39;line&#39;
  }]
};

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: &#39;blue&#39;, // start of blue stripe
          },
          {
            offset: 0.33,
            color: &#39;blue&#39;, // end of blue stripe
          },
          {
            offset: 0.33,
            color: &#39;red&#39;, // start of red stripe
          },
          {
            offset: 0.67, // end of red stripe
            color: &#39;red&#39;,
          },
          {
            offset: 0.67, // start of green stripe
            color: &#39;green&#39;,
          },
          {
            offset: 1,
            color: &#39;green&#39;, // end of green stripe
          },
        ]),
    }
  }]
});

<!-- language: lang-html -->

&lt;div id=&quot;chart-container3&quot; style=&quot;height:300px; width:45vw;display:inline-block&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://fastly.jsdelivr.net/npm/echarts@5/dist/echarts.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月5日 08:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定