ECharts的配置优先级策略

Learning by Code Wiki of ECharts.

The  “most specific wins” priority system

ECharts uses a “most specific wins” priority system for various elements and configurations beyond just tooltips and axisPointers. This means that options defined closer to a specific component or data item will generally override more general, global settings. Here are some additional cases and information about priority:

1. General Component Options:

  • Component-specific options vs. Theme vs. Default: When a component is initialized or its options are merged, ECharts first applies the theme, then the component’s defaultOption, and finally merges the user-provided options. For example, ComponentModel.mergeDefaultAndTheme and GlobalModel._mergeOption show this cascading merge. This allows for theme customization and sensible defaults while giving the user ultimate control.

2. Styling (Line, Item, Area, Text Styles):

3. Marker Components (MarkLine, MarkPoint, MarkArea):

4. dataZoom Components:

5. graphic Components:

6. Legend Components:

  • LegendModel Data Items: Each item in a legend’s data array can have its own icon and textStyle properties (LegendModel.getData). These item-specific settings override the general LegendModel properties, as shown in LegendView._createItem.
  • inactiveColor and inactiveBorderColor: These are used when a legend item is not selected, overriding the active colors.
  • selector: The selector option allows for ‘all’ or ‘inverse’ selection buttons. LegendModel._updateSelector normalizes these options, giving precedence to explicitly defined title values over default locale-based titles.
  • pageIcons: For ScrollableLegendModel (src/component/legend/ScrollableLegendModel.ts), pageIcons can be configured globally.

7. Toolbox Features:

  • Feature-specific options: Each toolbox feature (e.g., saveAsImagemagicTypedataZoomrestore) has its own defaultOption and can be configured individually. For example, ToolboxSaveAsImageFeatureOption defines backgroundColornameexcludeComponents, and pixelRatio specifically for the save image feature.
  • icon and title: These can be defined globally for the toolbox or specifically for each feature, with feature-specific settings taking precedence, as seen in ToolboxDataZoomFeature.getDefaultOption and ToolboxMagicType.getIcons.
  • iconStatus: The setIconStatus method (used in ToolboxDataZoomFeature and ToolboxMagicType) allows dynamically changing the visual state of a feature icon (e.g., ‘emphasis’ or ‘normal’), effectively overriding the default appearance.

In summary, ECharts is designed for extensibility and granular control. Configurations are typically applied in a layered fashion: global defaults -> theme -> component-level defaults -> series-level options -> data-item or element-specific options. This allows users to set broad chart styles and behaviors while providing the flexibility to customize individual elements as needed.

A case: grid.tooltip.axisPointer VS. xAxis.axisPointer

ECharts manages tooltip and axisPointer configurations with a hierarchical priority system, where more specific settings override general ones. This allows for fine-grained control over how tooltips and axisPointers behave across different components and series. The priority order, from highest to lowest, is as follows:

  1. Component/Data Item tooltip options:

    • Individual Data Items: The highest priority is given to tooltip options defined directly on individual data items within a series. This is processed by the _showSeriesItemTooltip function in src/component/tooltip/TooltipView.ts.
    • Component-specific tooltipConfig: Certain components (like legend or toolbox) can define their own tooltipConfig, which takes precedence for those specific elements. This is handled by _showComponentItemTooltip in src/component/tooltip/TooltipView.ts.
    • Marker tooltip: Markers such as markPointmarkLine, and markArea also have their own tooltip configurations, for example, markArea.tooltip in src/component/marker/MarkAreaModel.tsmarkLine.tooltip in src/component/marker/MarkLineModel.ts, and markPoint.tooltip in src/component/marker/MarkPointModel.ts. These configurations are typically set to trigger: 'item'.
  2. Series tooltip options: If a tooltip is not defined on a specific data item, ECharts falls back to the tooltip option configured at the series level. This configuration is part of the series.tooltip property. The _showSeriesItemTooltip function in src/component/tooltip/TooltipView.ts also considers this level.

  3. Coordinate System tooltip options:

    • Grid tooltip: For Cartesian coordinate systems (grids), the grid component can have its own tooltip configuration. This is less common for general tooltip settings but applies to axisPointer settings within the grid. The GridModel in src/coord/cartesian/GridModel.ts is where this can be configured.
    • Polar tooltip: Similar to grids, polar coordinate systems can also have their own tooltip configurations.
  4. xAxis / yAxis / angleAxis / radiusAxis axisPointer options:

  5. Global tooltip.axisPointer option:

    • If no specific axisPointer configuration is found on an individual axis, ECharts uses the global tooltip.axisPointer setting, as defined in TooltipModel.defaultOption in src/component/tooltip/TooltipModel.ts. This provides a default behavior for all axisPointers in the chart.
    • This global configuration also supports link property, allowing multiple axisPointers to be linked together, which is handled by the collect function in src/component/axisPointer/modelHelper.ts.
  6. Global tooltip option:

    • The lowest priority is the global tooltip option, defined in the tooltip component directly, as seen in TooltipModel.defaultOption in src/component/tooltip/TooltipModel.ts. This sets the default behavior for all tooltips in the chart if no more specific configuration overrides it. This includes general properties like showtriggeralwaysShowContentbackgroundColortextStyle, etc.

How ECharts Resolves Priorities:

The buildTooltipModel function in src/component/tooltip/TooltipView.ts is crucial for resolving this hierarchy. It takes a cascade of tooltip configurations (from data items, series, coordinate systems, and global settings) and merges them into a single effective tooltip model, with later (more specific) options overriding earlier (more general) ones.

For axisPointers, the modelHelper.collect function in src/component/axisPointer/modelHelper.ts processes axisPointer configurations, prioritizing local settings (e.g., axis.axisPointer) over global tooltip.axisPointer settings. It also creates a temporary axisPointerModel when a tooltip with trigger: 'axis' is used on a coordinate system or series, effectively making the tooltip’s axisPointer configuration take precedence for those triggered axes.

In essence, ECharts follows a “most specific wins” rule, where configurations defined closer to the data or component being interacted with will override more general, higher-level settings.