Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何找到图表下的面积_Javascript_Chart.js - Fatal编程技术网

Javascript 如何找到图表下的面积

Javascript 如何找到图表下的面积,javascript,chart.js,Javascript,Chart.js,我目前正在为我的家庭作业在图形绘制网站上工作。 我已经使用graph.js绘制完图形,我想知道如何计算图形下的面积值 这是我的图表截图 我想找出图表下的所有灰色区域 以下是使用graph.js创建图形的代码: new Chart(document.getElementById("line-chart"), { type: 'line', data: { labels: labels, datasets: [{ data : graphVa

我目前正在为我的家庭作业在图形绘制网站上工作。 我已经使用graph.js绘制完图形,我想知道如何计算图形下的面积值

这是我的图表截图

我想找出图表下的所有灰色区域

以下是使用graph.js创建图形的代码:

new Chart(document.getElementById("line-chart"), {
    type: 'line',
    data: {
    labels: labels,
    datasets: [{
            data : graphValue,
            borderwidth : 1,
            label : lineLabel,
            fill : true,
            lineTension : 0,
            pointBackgroundColor : 'rgba (83,81,84,1)',
            borderColor : 'rgba(83,81,84,1)'
    }]
    },
    options: {
        responsive: false,
        title: {
            display: true,
            text: graphName
      },
        scales : {
            yAxes : [{
                ticks : {
                    beginAtZero : true,
                    min :0,
                    max :200
                }
            }]
        }
    }
    });
我的值就是我从html页面传递的数字数组:

var graphValue = [98,12,19,64,85,91,56,181,171,90];

在图形中,每个部分都是高度为1的梯形。 您只需在
graphValue
数组中循环,计算每个梯形的面积,并将其添加到曲线下的总面积中

梯形面积公式为:

面积=
0.5*(a+b)*h
其中,
a
b
是梯形的两个平行边,
h
是它们之间的距离

下面的代码可以解决您的问题

var graphValue = [98,12,19,64,85,91,56,181,171,90];
var area = 0; // Initially set area to zero
var height = 1; // distance between parallel sides of the trapezium
for(var i=1; i<graphValue.length; i++) {
    area += 0.5 * (graphValue[i] + graphValue[i-1]) * height;
    // here a = graphValue[i] 
    // b = graphValue[i-1]
}

console.log(area); // 773 in this example
var-graphValue=[98,12,19,64,85,91,56181171,90];
变量面积=0;//最初将区域设置为零
变量高度=1;//梯形平行边之间的距离
对于(var i=1;i