计算高图中每个系列的百分比值

Calculating percentage value for each serie in highcharts

我正在尝试计算每个系列的百分比值,我设法找到的功能类似于

 formatter:function() {
                    var pcnt = (this.y / dataSum) * 100;
                    return Highcharts.numberFormat(pcnt) + '%';
                }

但让我感到困惑的是,我怎样才能动态地创建这个函数,以便它可以应用于提供的每个系列对象

强调上面函数的dataSum。 我尝试使用谷歌搜索以防万一 highcharts 已经在他们的文档中提供了一些解决方案,但遗憾的是我没有找到任何东西,我唯一发现的是将已经提供的值与 + '%' 连接起来,这远远超出了百分比的实际值该特定系列

$(function() {
  var chart = new Highcharts.Chart(



  {
  "meta": {
    "drilldownEnabled": false
  },
  "chart": {
  renderTo:"container",
    "additionalData": {
      "dateTime": false,
      "datetype": "string",
      "cliccable": true,
      "drillable": false,
      "drillableChart": false,
      "isCockpit": true,
      "categoryColumn": "city",
      "categoryGroupBy": "",
      "categoryGroupByNamens": "",
      "categoryName": "city",
      "categoryOrderColumn": "",
      "categoryOrderType": "",
      "categoryStacked": "",
      "categoryStackedType": ""
    },
    "zoomType": "xy",
    "panning": true,
    "type": "column",
    "options3d": {
      "enabled": false,
      "alpha": 25,
      "beta": 15,
      "depth": 50,
      "viewDistance": 25
    },
    "backgroundColor": "#FFFFFF",
    "heightDimType": "pixels",
    "widthDimType": "pixels",
    "plotBackgroundColor": null,
    "plotBorderWidth": null,
    "plotShadow": false,
    "borderColor": "#FFFFFF",
    "style": {
      "backgroundColor": "#FFFFFF",
      "fontFamily": "",
      "fontWeight": "",
      "fontSize": ""
    },
    "events": {}
  },
  "colors": [
    "#7cb5ec",
    "#434348",
    "#90ed7d",
    "#f7a35c",
    "#8085e9",
    "#f15c80",
    "#e4d354",
    "#2b908f",
    "#f45b5b",
    "#91e8e1"
  ],
  "title": {
    "text": "",
    "style": {
      "align": "",
      "color": "",
      "fontFamily": "",
      "fontSize": "",
      "fontWeight": ""
    }
  },
  "legend": {
    "itemDistance": 0,
    "symbolPadding": 25,
    "enabled": false
  },
  "xAxis": [
    {
      "plotBands": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "from": 0,
          "to": 0
        }
      ],
      "plotLines": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "dashStyle": "",
          "value": 0,
          "width": 0
        }
      ],
      "type": "category",
      "id": 0,
      "title": {
        "customTitle": false,
        "text": "city",
        "style": {}
      },
      "labels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontSize": "",
          "fontWeight": ""
        },
        "align": ""
      }
    }
  ],
  "yAxis": [
    {
      "plotBands": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "from": 0,
          "to": 0
        }
      ],
      "plotLines": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "dashStyle": "",
          "value": 0,
          "width": 0,
          "zIndex": 1
        }
      ],
      "title": {
        "text": "",
        "customTitle": false,
        "style": {
          "color": "",
          "fontFamily": "",
          "fontWeight": "",
          "fontSize": ""
        }
      },
      "labels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontSize": "",
          "fontWeight": ""
        },
        "align": ""
      },
      "gridLineDashStyle": "$convertedTypeline",
      "minorGridLineDashStyle": "$convertedMinorTpeline"
    }
  ],
  "series": [
    {
      "name": "total_children",
      "dataLabels": {

        "style": {
          "color": "",
          "fontFamily": "",
          "fontWeight": "",
          "fontSize": "",
          "fontStyle": ""
        },
        "enabled": true,
        "labelFormat": "{y:,.2f}"
      },
      "data": [

        {
          "drilldown": false,
          "y": 271,
          "name": "Pomona",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 237,
          "name": "Port Hammond",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 205,
          "name": "Port Orchard",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 239,
          "name": "Portland",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 265,
          "name": "Puyallup",
          "datetype": "string"
        },

      ],
      "selected": true,
      "tooltip": {
        "valueDecimals": 2,
        "scaleFactor": "empty",
        "ttBackColor": "#FCFFC5"
      },
      "yAxis": 0
    },
    {
      "name": "num_cars_owned",
      "dataLabels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontWeight": "",
          "fontSize": "",
          "fontStyle": ""
        },
        "enabled": true,
        "labelFormat": "{y:,.2f}"
      },
      "data": [
        {
          "drilldown": false,
          "y": 228,
          "name": "Acapulco",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 189,
          "name": "Albany",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 212,
          "name": "Altadena",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 219,
          "name": "Anacortes",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 231,
          "name": "Arcadia",
          "datetype": "string"
        },

      ],
      "selected": true,
      "tooltip": {
        "valueDecimals": 2,
        "scaleFactor": "empty",
        "ttBackColor": "#FCFFC5"
      },
      "yAxis": 0
    }
  ],
  "tooltip": {
    "borderWidth": 0,
    "borderRadius": 0,
    "followTouchMove": false,
    "followPointer": true,
    "useHTML": true,
    "backgroundColor": null,
    "style": {
      "padding": 0
    }
  },
  "lang": {
    "noData": ""
  },
  "noData": {
    "style": {
      "fontFamily": "",
      "fontSize": "",
      "color": ""
    },
    "position": {
      "align": "center",
      "verticalAlign": "middle"
    }
  },
  "credits": {
    "enabled": false
  },
  "plotOptions": {
    "line": {
      "marker": {
        "symbol": "circle",
        "lineWidth": 2
      }
    },
    "series": {
      "events": {},
      "showCheckbox": true,
      "cursor": "pointer",
      "point": {
        "events": {}
      },
      "dataLabels": {
        "allowOverlap": true
      },
      "turboThreshold": 2000
    }
  }
}










  );
});

有没有人尝试过在任何情况下都提供系列百分比值的解决方案? 我正在尝试计算 total_children 的百分比和 num_cars_owned 系列的百分比,因为它们是系列数组的分离对象。

您可以在 link 此处找到代码示例 http://jsfiddle.net/JeLrb/538/

您需要计算dataSum值:

        tooltip: {
            ...,
            formatter: function() {
                var dataSum = 0,
                    pcnt;

                this.series.points.forEach(function(point) {
                    dataSum += point.y;
                });

                pcnt = (this.y / dataSum) * 100;

                return Highcharts.numberFormat(pcnt) + '%';
            }
        }

现场演示:http://jsfiddle.net/BlackLabel/9tbynahj/

API参考:https://api.highcharts.com/highcharts/tooltip