Weather data value to rotation
-
I've made a barometer gauge, starting from 970 hPa (from: -130 degrees/angle) --> 1070 hPa (to: +130 degrees/angle)
I'm using Openweathermap.org to get actual pressure data (shown bottom of image)
How do I calculate the angle of the needle using the pressure data?, I guess it's a job for javascript and as such, have searched exstensively but haven't found anything relevant and am not really sure how to tackle it. Any info/ideas gratefully received.
-
@Markkyboy quote from my previous post:
//Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew
just implement that in QML if you don't want to use the c++/template
function mapToRange(x, maxRangeOld, minRangeOld,maxRangeNew, minRangeNew) { return (x-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew } CircularSlider { id: barometer width: 400 height: width interactive: false minValue: -130 maxValue: 130 startAngle: -130 endAngle: 130 anchors.centerIn: parent value: 0 //Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew angle: mapToRange(value,1070, 970, 130, -130) ....
-
taken from my function collection:
template<typename typeIn, typename typeOut> auto mapToRange(typeIn value, typeIn inMax = std::numeric_limits<typeIn>::max(), typeIn inMin = std::numeric_limits<typeIn>::min(), typeOut outMax = std::numeric_limits<typeOut>::max(), typeOut outMin = std::numeric_limits<typeOut>::min() ){ //Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew auto mapped = (static_cast<double>(value) - inMin) / (inMax - inMin) * (outMax - outMin) + outMin; return static_cast<typeOut>(mapped); } int main(int argc, char *argv[]) { for(int i{970}; i < 1071; i++){ int result = mapToRange<int,int>(i,970, 1070, -130, 130); qDebug() << i << "to" << result; } }
-
@J-Hilk said in Weather data value to rotation:
template<typename typeIn, typename typeOut>
auto mapToRange(typeIn value,
typeIn inMax = std::numeric_limits<typeIn>::max(),
typeIn inMin = std::numeric_limits<typeIn>::min(),
typeOut outMax = std::numeric_limits<typeOut>::max(),
typeOut outMin = std::numeric_limits<typeOut>::min() ){
//Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew
auto mapped = (static_cast<double>(value) - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;return static_cast<typeOut>(mapped);
}
Thank you, but how do I use that in the QML side of things?, I'm using a Repeater with Rotation (origin.x, origin.y, angle) inside CircularSlider.
I have added your data to my main.cpp successfully, but where to go from there?
CircularSlider { id: barometer width: 400 height: width interactive: false minValue: -130 maxValue: 130 startAngle: -130 endAngle: 130 anchors.centerIn: parent value: 0 Repeater { id: ind model: 1 Rectangle { z: 2 id: indicator width: 3 height: 160 color: "orange" smooth: true antialiasing: true transform: [ Translate { x: barometer.width / 2 - width / 2 y: 35 }, Rotation { origin.x: barometer.width / 2 origin.y: barometer.height / 2 angle: barometer.angle } ] } }
-
@Markkyboy quote from my previous post:
//Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew
just implement that in QML if you don't want to use the c++/template
function mapToRange(x, maxRangeOld, minRangeOld,maxRangeNew, minRangeNew) { return (x-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew } CircularSlider { id: barometer width: 400 height: width interactive: false minValue: -130 maxValue: 130 startAngle: -130 endAngle: 130 anchors.centerIn: parent value: 0 //Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew angle: mapToRange(value,1070, 970, 130, -130) ....
-
@Markkyboy please, you should by able to fix my typo 😅