DAY 15
0

Day15 這什麼水平

• Histogram

• Calculation

``````	let totalNumber = data.length;
let dataMin = "";
let dataMax = "";
for (let i = 0; i < totalNumber; i++) {
let tempData = data[i];
if (dataMin) {
if (tempData < dataMin) {
dataMin = tempData;
}
}
else {
dataMin = tempData;
}
if (dataMax) {
if (tempData > dataMax) {
dataMax = tempData;
}
}
else {
dataMax = tempData;
}
}
let sum = data.reduce((preValue, curValue) => preValue + curValue);
let dataMean = sum / totalNumber;
let squrSum = data.reduce((preValue, curValue) => preValue  + (curValue - dataMean)** 2);
let dataSTD = (squrSum / totalNumber) ** 0.5;
``````

``````	let botValue = dataMin;
let topValue;
if (Number.isInteger(binNumber)) {
binWidth = (dataMax - dataMin) / binNumber;
}
else if (!Number.isNaN(binWidth)) {
let dataSwing = dataMax - dataMin;
binNumber = Math.ceil(dataSwing / binWidth);
botValue = botValue + 0.5 * dataSwing - 0.5 * binWidth * binNumber;
}
let dataset = [];
for (let i = 0; i < binNumber; i++) {
topValue = botValue + binWidth;
let subList;
if (i === binNumber - 1) {
subList = data.filter(function(item) {
return (item >= botValue) & (item <= dataMax);
});
}
else {
subList = data.filter(function(item) {
return (item >= botValue) & (item < topValue);
});
}
dataset.push({x: (topValue + botValue) / 2, y: subList.length})
botValue = topValue
}
``````
• Chart

``````const data = {
datasets: [
{
label: 'DATA',
data: [],
borderColor: CHART_COLORS.blue,
backgroundColor: RGBwithA(CHART_COLORS.blue, 0.8),
borderWidth: 1,
barPercentage: 1,
categoryPercentage: 1,
},
]
};
const config = {
type: "bar",
data: data,
options: {
scales: {
x: {
type: 'linear',
offset: false,
grid: {
offset: false,
},
ticks: {
//stepSize: ,
},
//min:
//max:
},
y: {

},
},
plugins:{
legend:{
display: false,
},
}
},
};
``````

``````		let histData = calculateHistogram(data, binWidth, binNumber);
chart.data.datasets.forEach(dataset => {
dataset.data = histData.dataset
});
chart.options.scales.x.min = histData.histMin;
chart.options.scales.x.max = histData.histMax;
chart.options.scales.x.ticks.stepSize = histData.binWidth;
chart.update();
``````