gpt4 book ai didi

javascript - Highcharts:如何创建年龄组、性别、访问次数的详细饼图?

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:38 28 4
gpt4 key购买 nike

是否可以为包含年龄组、性别和访问次数作为数据的 csv 数据集创建带有 highcharts 的钻取饼图?我一直在尝试这样做,但我得到的只是一团糟,只有错误。

 var options = {
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
xAxis: {
type: 'category'
},

legend: {
enabled: false
},

plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true
}
}
},

series: [{
name: 'People',
colorByPoint: true,
data: [{
name: 'Age group',
y: [],
drilldown: 'Agegroup'
}, {
name: 'Gender',
y: [],
drilldown: 'Gender'
}, {
name: 'Sessions',
y: [],
drilldown: 'Sessions'
}]
}],
drilldown: {
series: [{
id: 'Agegroup',
data: []
}, {
id: 'Gender',
data: []
}, {
id: 'Sessions',
data: []
}]
}
};

var lines = csv.split('\n');
$.each(lines, function (lineNo, line) {
var items = line.split(',');
var agegroup=items[0];
var gender=items[1];
var sessions=parseInt(items[2]);
options.drilldown.series.data.push([gender,sessions]);

其 CSV 数据如下:

18-24,female,313,1
18-24,male,209,1
25-34,female,1941,1
25-34,male,1646,1
35-44,female,980,1
35-44,male,1000,1
45-54,female,244,1
45-54,male,272,1
55-64,female,106,1
55-64,male,104,1
65+,female,36,1
65+,male,52,1

最佳答案

试试这个

var yMale, yFemale;
var sessionDataMale = [];
var sessionDataFemale = [];
var ageGroup = []
$.get('data.csv', function(data) {
var lines = csv.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');
var agegroup = items[0];
var gender = items[1];
var sessions = parseInt(items[2]);

if (gender == male) {
yMale += sessions;
sessionDataMale.push(sessions)
}
if (gender == female) {
yFemale += sessions
sessionDataFemale.push(sessions)
}
ageGroup.push(agegroup)
}
//creating uniue age group array
var uniqueAgeGroups = []; $.each(ageGroup, function(i, el) {
if ($.inArray(el, uniqueAgeGroups) === -1) uniqueAgeGrops.push(el);
});
var drillDataMale = []
var drillDataFemale = []

for (var i = 0; i < uniqueAgeGroups.length; i++) {
var arrMale = [];
var arrFemale = []
arrMale.push([uniqueAgeGroups[i], sessionDataMale[i]])
arrFemale.push([uniqueAgeGroups[i], sessionDataFemale[i]])
drillDataMale.push(arrMale)
drillDataFemale.push(arrFemale)
}
)
}
// Create the chart
Highcharts.chart('container', {
chart: {
type: 'pie'
},
title: {
text: 'Test'
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
format: '{point.name}: {point.y}'
}
}
},

tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y}</b> of total<br/>'
},
series: [{
name: 'Gender',
colorByPoint: true,
data: [{
name: 'Female',
y: yFemale,
drilldown: 'female'
}, {
name: 'Male',
y: yMale,
drilldown: 'male'
}],
}],
drilldown: {
series: [{
name: 'Age Group',
id: male,
data: drillDataMale
}, {
name: 'Age Group',
id: female,
data: drillDataFemale
}]
}
});

关于javascript - Highcharts:如何创建年龄组、性别、访问次数的详细饼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42898060/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com