如何解决如何通过highcharts中的原点制作回归线
我试图让回归从原点开始,x=y=0。这可以做到吗。有人说这样做不好,但出于某些目的,我需要使这条线穿过原点。我正在使用 highcharts。
解决方法
如何在 x = y = 0 的回归序列中添加一个点并将标记设置为禁用以隐藏它?
let discipline = [
{
name: "Football",data: "football"
}
];
Highcharts.getJSON(
"https://raw.githubusercontent.com/mekhatria/demo_highcharts/master/olympic2012.json?callback=?",function (data) {
function regression(arrWeight,arrHeight) {
let r,sy,sx,b,a,meanX,meanY;
r = jStat.corrcoeff(arrHeight,arrWeight);
sy = jStat.stdev(arrWeight);
sx = jStat.stdev(arrHeight);
meanY = jStat(arrWeight).mean();
meanX = jStat(arrHeight).mean();
b = r * (sy / sx);
a = meanY - meanX * b;
//Set up a line
let y1,y2,x1,x2;
x1 = jStat.min(arrHeight);
x2 = jStat.max(arrHeight);
y1 = a + b * x1;
y2 = a + b * x2;
return {
line: [
//Add x = 0,y = 0 to your regression logic?
{x: 0,y: 0,marker: {enabled: false}},{x: x1,y: y1,marker: {enabled: true}},{x: x2,y: y2,],r
};
}
const getData = (continentName) => {
let temp = [],tempWeight = [],tempHeight = [];
data.forEach((elm) => {
if (
elm.continent == continentName &&
elm.weight > 0 &&
elm.height > 0
) {
temp.push([elm.height,elm.weight]);
tempWeight.push(elm.weight);
tempHeight.push(elm.height);
}
});
let { line,r } = regression(tempWeight,tempHeight);
return [temp,line,r];
};
const getDataSport = (sportName) => {
let temp = [],tempHeight = [];
data.forEach((elm) => {
if (elm.sport == sportName && elm.weight > 0 && elm.height > 0) {
temp.push([elm.height,r];
};
let series = [],visible = false,index = 0,activate = ["Football"];
discipline.forEach((e) => {
if (activate.indexOf(e.name) > -1) {
visible = true;
} else {
visible = false;
}
let [scatterData,r] = getDataSport(e.data);
series.push(
{
type: "scatter",visible: visible,name: e.name,data: scatterData
},{
name: e.name,r: r,data: line
}
);
});
Highcharts.chart("container",{
chart: {
type: "line",zoomType: "y",},title: {
text: "2012 Olympic football athletes' weight and height relationship"
},xAxis: {
title: {
text: "Height"
},labels: {
format: "{value} m"
},yAxis: {
title: {
text: "Weight"
},labels: {
format: "{value} kg"
}
},legend: {
enabled: true
},plotOptions: {
scatter: {
marker: {
radius: 2.5,symbol: "circle",states: {
hover: {
enabled: true,lineColor: "rgb(100,100,100)"
}
}
},states: {
hover: {
marker: {
enabled: false
}
}
}
},line: {
lineWidth: 2.5
}
},tooltip: {
formatter: function () {
if (this.series.data.length > 2) {
return (
this.series.name +
"<br/>Height: " +
this.x +
" m<br/>Weight: " +
this.y +
" kg"
);
} else {
return (
this.series.name +
"<br/>r: " +
this.series.userOptions.r.toFixed(2)
);
}
}
},series: series
});
}
);
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jstat@latest/dist/jstat.min.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。