导出Highcharts极坐标图csv,并用类别代替极坐标

时间:2019-03-16 12:59:23

标签: highcharts export-to-csv

我实现了一个极坐标图,其中每个系列都有对应于4个类别的4个值。当我导出图表csv时,category列包含极坐标。我想将它们替换为相应的类别名称。我该怎么做?

将类别添加到每个系列都没有效果。我还尝试将类别属性添加到xAxis,但没有效果。 xAxis.label格式化程序成功返回每个数据极坐标的类别名称。

const options = {
  chart: {
    polar: true,
  },
  title: {
    text: '',
  },
  tooltip: {
    valueDecimals: 2,
    headerFormat: '<br/>',
  },
  legend: {},
  pane: {
    startAngle: 0,
    endAngle: 360,
  },
  xAxis: {
    tickInterval: 45,
    min: 0,
    max: 360,
    labels: {
      // eslint-disable-next-line
      formatter: function() {
        switch (this.value) {
          case 45:
            return '<b>Experience</b>'
          case 135:
            return '<b>Frictionless</b>'
          case 225:
            return '<b>Low Price</b>'
          case 315:
            return '<b>Brand</b>'
          default:
            return ''
        }
      },
    },
  },
  yAxis: {
    min: 0,
    max: 10,
    labels: {
      format: '{}',
    },
  },
  plotOptions: {
    series: {
      pointStart: 45,
      pointInterval: 90,
    },
    column: {
      pointPadding: 0,
      groupPadding: 0,
    },
  },
  series: kahnSeries,
}

2 个答案:

答案 0 :(得分:1)

您需要使用categories属性,但不能使用以下选项:pointIntervalpointStartminmax

xAxis: {
    categories: ['Experience', 'Frictionless', 'Low Price', 'Brand']
},

实时演示:http://jsfiddle.net/BlackLabel/z8cm1p39/

API参考:https://api.highcharts.com/highcharts/xAxis.categories

答案 1 :(得分:0)

为避免更改图表的当前显示,我包装了getCSV函数并替换了CSV类别值。如果有更简单的方法,请分享。

{
  (function (H) {
    H.wrap(H.Chart.prototype, 'getCSV', function (
      proceed,
      useLocalDecimalPoint
    ) {
      // Run the original proceed method
      const result = proceed.apply(
        this,
        Array.prototype.slice.call(arguments, 1)
      )
      const itemDelimiter = ','
      const lineDelimiter = '\n'
      const rows = result.split(lineDelimiter)
      let newResult = ''
      let rowCategories = false
      rows.forEach((row, rowIndex) => {
          const columns = row.split(itemDelimiter)
          if (rowIndex === 0 && columns[0] === '"Category"') {
            rowCategories = true
          }
          if (rowIndex > 0 && rowCategories) {
            let newRow = formatter(columns[0])
            columns.forEach((column, columnIndex) => {
              if (columnIndex > 0) {
                newRow += itemDelimiter
                newRow += column
              }
            })
            newResult += newRow
          } else {
            newResult += row
          }
          if (rowIndex < rows.length - 1) {
            newResult += lineDelimiter
          }
        }
      )
      return newResult
    })
  }(Highcharts))
}