gpt4 book ai didi

php - Google 图表 - 获取线性趋势线的方程式

转载 作者:搜寻专家 更新时间:2023-10-31 20:59:54 24 4
gpt4 key购买 nike

我有一张这样的图表(由google charts绘制)该线由谷歌生成,带有线性趋势线选项

图片 enter image description here

代码

google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Diameter', 'Age'],
[8, 37], [4, 19.5], [11, 52], [4, 22], [3, 16.5], [6.5, 32.8], [14, 72]]);

var options = {
title: 'Age of sugar maples vs. trunk diameter, in inches',
hAxis: {title: 'Diameter'},
vAxis: {title: 'Age'},
legend: 'none',
trendlines: { 0: {} } // Draw a trendline for data series 0.
};

var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

如何知道这条线的方程式?

最佳答案

将趋势线添加到图例将显示等式...

trendlines: {
0: {
visibleInLegend: true
}
}

如果需要,您可以从图例中删除系列......

series: {
0: {
visibleInLegend: false
}
},

请参阅以下工作片段...

google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});

function drawChart() {
var data = google.visualization.arrayToDataTable([
['Diameter', 'Age'],
[8, 37], [4, 19.5], [11, 52], [4, 22], [3, 16.5], [6.5, 32.8], [14, 72]]);

var options = {
title: 'Age of sugar maples vs. trunk diameter, in inches',
hAxis: {title: 'Diameter'},
vAxis: {title: 'Age'},
legend: {
alignment: 'end',
position: 'top'
},
series: {
0: {
visibleInLegend: false
}
},
trendlines: {
0: {
visibleInLegend: true
}
}
};

var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>


编辑

等式添加到图例后,
您可以从 <text> 中获取值用于绘制图例标记的元素
但需要等待 'ready'事件第一,
知道图表绘制完成

此外,您还需要一种方法来确定图例标记 <text>元素
来自其他标签,例如图表标题
在这个例子中,标题和图例标记
有一个属性 text-anchor值为 'start'

text-anchor可能会根据图例的 alignment 而改变和 position

字体颜色 ( fill ) 用于将标题与图例标记分开...

请参阅以下工作片段...

google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});

function drawChart() {
var data = google.visualization.arrayToDataTable([
['Diameter', 'Age'],
[8, 37], [4, 19.5], [11, 52], [4, 22], [3, 16.5], [6.5, 32.8], [14, 72]]);

var options = {
title: 'Age of sugar maples vs. trunk diameter, in inches',
hAxis: {title: 'Diameter'},
vAxis: {title: 'Age'},
legend: {
alignment: 'end',
position: 'top'
},
series: {
0: {
visibleInLegend: false
}
},
trendlines: {
0: {
visibleInLegend: true
}
}
};

var container = document.getElementById('chart_div');
var chart = new google.visualization.ScatterChart(container);

google.visualization.events.addListener(chart, 'ready', function () {
var equation = $('text[text-anchor="start"][fill="#222222"]').text();
console.log(equation);
});

chart.draw(data, options);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于php - Google 图表 - 获取线性趋势线的方程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45413670/

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