从csv

时间:2017-07-21 09:36:09

标签: javascript csv highcharts

我尝试从CSV数据加载数据以创建饼图。 总是切片显示“切片:0.0%”。但是,在我的CSV数据中,没有称为切片的数据。我想知道这是因为highchart的默认设置还是因为我用错误的方式读取CSV数据文件。

Jsfiddle_code

下面是图表的照片。chart

var pie_bl = Papa.parse(document.getElementById('pie_bl').innerHTML);
Highcharts.chart('container', {
  chart: {
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false,
    type: 'pie'
  },
  data: {
    rows: pie_bl.data
  },
  title: {
    text: 'Pie Chart_csv'
  },
  tooltip: {
    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
  },
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      dataLabels: {
        enabled: true,
        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
        style: {
          color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
        }
      }
    }
  },
});
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/data.js"></script>
<script src="https://rawgit.com/mholt/PapaParse/master/papaparse.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

<pre id="pie_bl" style="display:none">Category,Allocation
a,0.45,
b,0.5,
c,1.25,
d,0.15, 
e,0.1,
f,0.55,
</pre>

2 个答案:

答案 0 :(得分:0)

您的值将使用正确的逗号

<pre id="pie_bl" style="display:none">Category,Allocation,
a,0.45,
b,0.5,
c,1.25,
d,0.15, 
e,0.1,
f,0.55,</pre>

Fiddle

答案 1 :(得分:0)

<pre>标记中的CSV数据以a和new-line结尾。在CSV中,这将被视为另一个条目。这就是为什么它显示Slice:0.0。

默认情况下,Highcharts将标签指定为Slice,如果未提及,则将值指定为0。因此,您的<pre>标记看起来应该是这样的

<pre id="pie_bl" style="display:none">
    Category,Allocation
    a,0.45
    a,0.45,
    b,0.5,
    c,1.25,
    d,0.15, 
    e,0.1,
    f,0.55</pre>

更新你的小提琴here