函数图形元素

函数图形元素渲染具有可配置轴、填充和样式的数学函数曲线。表达式在一个沙盒环境中进行评估。

函数图形元素共享来自 基本元素视图元素 的公共字段。

函数图形属性

属性类型必需示例值范围描述
typestringtrue-Function Graph元素的类型。对于函数图形元素,这始终是“函数图形”。
expressionstringtrueMath.sin(x)-要绘制的数学表达式。使用 JavaScript Math 语法,以 x 作为变量。
rangearrayfalse[-10, 10]-x 轴范围,作为一个包含两个元素的数组 [min, max]。
samplesnumberfalse-> 0用于绘制曲线的样本点数量。更高的值会产生更平滑的曲线。
thicknessnumberfalse-> 0函数曲线的线条厚度,以像素为单位。
colorstringfalse--函数曲线的颜色,采用十六进制格式。
backgroundColorstringfalse--图形区域的背景颜色,采用十六进制格式。
backgroundOpacitynumberfalse-0 - 1背景的透明度。
thresholdnumberfalse--裁剪的最大 y 值阈值。超出此值的点将不被渲染。
flipYbooleanfalse--是否翻转 Y 轴方向。
fillColorstringfalse--曲线下方的填充颜色,采用十六进制格式。
fillOpacitynumberfalse-0 - 1曲线下方区域填充的透明度。设置为 0 以禁用填充。

轴属性

属性类型必需示例值范围描述
showAxisbooleanfalse--是否显示坐标轴。
axisColorstringfalse--轴线的颜色,采用十六进制格式。
axisOpacitynumberfalse-0 - 1轴线的透明度。
axisTickCountnumberfalse--每个轴上的刻度线数量。
axisThicknessnumberfalse--轴线的厚度,以像素为单位。
showAxisArrowbooleanfalse--是否在轴的末端显示箭头。
axisArrowSizenumberfalse--轴箭头的大小,以像素为单位。

示例

正弦波

functionGraphElement.json
 
{
  "id": "graph-001",
  "type": "Function Graph",
  "start": 0,
  "duration": 5,
  "trackIndex": 0,
  "x": 200,
  "y": 200,
  "width": 400,
  "height": 200,
  "anchorX": 200,
  "anchorY": 100,
  "rotation": 0,
  "scaleX": 1,
  "scaleY": 1,
  "alpha": 1,
  "expression": "Math.sin(x)",
  "range": [-10, 10],
  "samples": 200,
  "thickness": 2,
  "color": "#C55F73",
  "backgroundColor": "#000000",
  "backgroundOpacity": 1,
  "showAxis": true,
  "axisColor": "#ffffff",
  "axisTickCount": 10
}

填充的二次函数

functionGraphFilled.json
 
{
  "id": "graph-002",
  "type": "Function Graph",
  "start": 0,
  "duration": 8,
  "trackIndex": 0,
  "x": 300,
  "y": 250,
  "width": 500,
  "height": 300,
  "anchorX": 250,
  "anchorY": 150,
  "rotation": 0,
  "scaleX": 1,
  "scaleY": 1,
  "alpha": 1,
  "expression": "x * x - 4",
  "range": [-5, 5],
  "samples": 300,
  "thickness": 3,
  "color": "#4A90E2",
  "fillColor": "#4A90E2",
  "fillOpacity": 0.3,
  "backgroundColor": "#1a1a2e",
  "backgroundOpacity": 0.9,
  "showAxis": true,
  "axisColor": "#cccccc",
  "axisThickness": 2,
  "showAxisArrow": true,
  "axisArrowSize": 10
}

注意事项

  • expression 在一个沙盒环境中进行评估,可以访问 JavaScript 的 Math 对象。使用 Math.sin(x)Math.pow(x, 2)Math.exp(x) 等。
  • 变量 x 在指定的 range 内以给定数量的 samples 进行迭代。
  • 设置 fillOpacity 大于 0 以填充曲线与 x 轴之间的区域。
  • 使用 threshold 来裁剪具有渐近线的函数的极端 y 值(例如,1/x)。