我将apache-superset中的country_map用于可视化目的。放大多边形时,列中的信息会显示在多边形内部,如下所示:

javascript - 如何在Apache超集的country_map上添加更多指标?-LMLPHP

仅显示一个可用的度量标准选项:
javascript - 如何在Apache超集的country_map上添加更多指标?-LMLPHP

在以下路径中找到度量标准更新的代码:



码:

const updateMetrics = function (region) {
  if (region.length > 0) {
    resultText.text(format(region[0].metric));
  }
};

指标在controls.jsx中定义:


const metrics = {
  type: 'MetricsControl',
  multi: true,
  label: t('Metrics'),
  validators: [v.nonEmpty],
  default: (c) => {
    const metric = mainMetric(c.savedMetrics);
    return metric ? [metric] : null;
  },
  mapStateToProps: (state) => {
    const datasource = state.datasource;
    return {
      columns: datasource ? datasource.columns : [],
      savedMetrics: datasource ? datasource.metrics : [],
      datasourceType: datasource && datasource.type,
    };
  },
  description: t('One or many metrics to display'),
};
const metric = {
  ...metrics,
  multi: false,
  label: t('Metric'),
  default: props => mainMetric(props.savedMetrics),
};

国家/地区 map 使用的是metric,不允许选择多个指标,请在此处找到代码:


  controlPanelSections: [
    {
      label: t('Query'),
      expanded: true,
      controlSetRows: [
        ['entity'],
        ['metric'],
        ['adhoc_filters'],
      ],
    },
    {
      label: t('Options'),
      expanded: true,
      controlSetRows: [
        ['select_country', 'number_format'],
        ['linear_color_scheme'],
      ],
    },
  ],
country_map的python类位于viz.py:
class CountryMapViz(BaseViz):

    """A country centric"""

    viz_type = 'country_map'
    verbose_name = _('Country Map')
    is_timeseries = False
    credits = 'From bl.ocks.org By john-guerra'

    def query_obj(self):
        qry = super(CountryMapViz, self).query_obj()
        qry['metrics'] = [
            self.form_data['metric']]
        qry['groupby'] = [self.form_data['entity']]
        return qry

将CountryMap.js和viz.py中的代码从metric更改为metrics会导致以下错误:
Traceback (most recent call last):
  File "/Documents/superset/superset/superset/viz.py", line 410, in get_df_payload
    df = self.get_df(query_obj)
  File "/Documents/superset/superset/superset/viz.py", line 213, in get_df
    self.results = self.datasource.query(query_obj)
  File "/Documents/superset/superset/superset/connectors/sqla/models.py", line 797, in query
    sql = self.get_query_str(query_obj)
  File "/Documents/superset/superset/superset/connectors/sqla/models.py", line 471, in get_query_str
    qry = self.get_sqla_query(**query_obj)
  File "/Documents/superset/superset/superset/connectors/sqla/models.py", line 585, in get_sqla_query
    elif m in metrics_dict:
TypeError: unhashable type: 'list'

如何添加更多指标以显示在多边形内部?

最佳答案

错误TypeError: unhashable type: 'list'的直接原因是您对文件“viz.py”的修改:



如您在源代码here中看到的,表单数据metrics是一个包含listmetric对象,其中metric可能是字符串或其他可哈希的对象。在python语言中,list对象不可哈希。因为您用不可哈希的对象(metric)替换了可哈希对象(metrics),所以会引发unhashable type错误。

在其他CoutryMapViz.query_obj()类中,可以找到修改metrics以接受Viz查询的正确方法。代码段here是一个非常好的示例:


class CalHeatmapViz(BaseViz):

"""Calendar heatmap."""
...

    def query_obj(self):
        d = super(CalHeatmapViz, self).query_obj()
        fd = self.form_data
        d['metrics'] = fd.get('metrics')
        return d

最后,CoutryMapViz.query_obj()方法应如下所示:

class CountryMapViz(BaseViz):

...

    def query_obj(self):
        qry = super(CountryMapViz, self).query_obj()
        qry['metrics'] = fd.get('metrics')
        qry['groupby'] = [self.form_data['entity']]
        return qry

10-02 15:25