I'm actually trying to use HighCharts for displaying multiple threshold.
Here is the portion of my code.
Especially here :
var translatedThreshold = series.yAxis.translate(threshold1),
y1 = Math.round(series.yAxis.len - translatedThreshold),
y2 = y1 + 2; // 0.01 would be fine, but IE9 requires 2
// Apply gradient to the path
series.graph.attr({
stroke: {
linearGradient: [0, y1, 0, y2],
stops: [
[0, colorAbove],
[1, colorBelow]
]
}
});
// Apply gradient to the area
if (series.area) {
series.area.attr({
fill: {
linearGradient: [0, y1, 0, y2],
stops: [
[0, colorAbove],
[1, colorBelow]
]
}
});
}
Actually, there is two thresholds, one on 0 and one on 3.
For the dots, the threshold is correct (a blue dot is displayed because its value is between 0 and 3 and its color value is ColorMiddle).
For the curve, and the area, the colorMiddle values are not displayed though, because I don't know how to add them on the series graph.
I think I must use a translatedThreshold2 with series.yAxis.translate(threshold2), and add them on the series.area.attr but I don't know how exactly.
Does anyone have an idea ?
EDIT1 : I managed to display the threshold line (in blue on the example below) but the area is not filled correctly. The area should be in blue between 0 and 300 and it's the color green (AKA colorAbove, and not colorMiddle).
Result is displayed here : http://jsfiddle.net/LeLwu/27/ (it happens to be different in Chrome and Firefox ...)
Related
I'm creating a chart with 2 series. The series share the same axis but have different Ys for different Xs. For example:
Axis A: [{ x: 2, y: 5}, { x: 4, y: 10}]
Axis B: [{ x: 1, y: 2}, { x: 3, y: 6}, { x: 3, y: 9}]
I want to show a tooltip for each X value that appears in axis A (so in this case, for x = 2, 4) and in the tooltip show the value that both axis would have there.
So the problem is that for axis B I don't have a point for x = 2 and x = 4. Is there a way to calculate dinamically the Y value that the axis B would have in those Xs?
This is an example: the tooltip shows info of the blue point, but also calculates what the corresponding green point would be and shows it in the tooltip.
Thank you!
Apples Metal hello 2D triangle example doesn't appear to be 2D in the way I had hoped.
I was expecting to have 1 on x and 1 on y to be exactly 1 pixel and for it to start at the top left.
I would have thought that 2D implies flat and with no concept of depth it naturally follows to have 1 unit to map to 1 pixel, how can I fix the example to work in the way I expected it to? General concepts of coarse, no need to actually produce the code unless you really like unicorns in which case please do; so I can communicate to the world my brilliance.
https://developer.apple.com/documentation/metal/hello_triangle
static const AAPLVertex triangleVertices[] =
{
// 2D positions, RGBA colors
{ { 0, 0 }, { 1, 0, 0, 1 } },
{ { 0, 4 }, { 0, 1, 0, 1 } },
{ { 4, 0 }, { 0, 0, 1, 1 } },
{ { 4, 4 }, { 0, 1, 0, 1 } },
};
These coordinates as a line strip produces a 4 by 5 px N!
Update
I was unable to resolve/understand why a 4 by 5 px line strip is drawn. I believe the line strip algorithm is incorrect.
Triangle strip with expected result
vs
Same but line strip with unexpected results
Consider this line strip 3 vertex corner:
static const AAPLVertex triangleVertices[] =
{
// 2D positions, RGBA colors
{ { 0, 4 }, { 1, 0, 0, 1 } },
{ { 0, 0 }, { 0, 1, 0, 1 } },
{ { 4, 0 }, { 0, 0, 1, 1 } },
};
corner as line strip image
It seems you basically want to specify your initial vertex positions in window coordinates. That will get you the 1:1 pixel-to-unit mapping that you're after.
The job here is to come up with a sequence of transformations that allows you to specify vertices in window space, while honoring the fact that vertex positions returned from your vertex function must be in clip space. The inverse of this transformation is applied during rasterization, so we're trying to cancel it out.
We'll ignore depth. Clip space ranges from -1 to 1 in both X and Y directions. So we want to map the origin of our space (0, 0) to (-1, 1) in clip space. Similarly, we want to map the bottom-right (width, height) of our space to (1, -1) in clip space.
We can get most of the way there by multiplying the pixel coordinates by 2, subtracting the viewport size, and finally dividing by the viewport size. This replaces the computation of the clip space coordinates in the example's vertex function:
out.clipSpacePosition.xy = ((pixelSpacePosition * 2) - viewportSize) / viewportSize;
In this space, Y increases downward, but the Y axis in clip space increases upward, so we need to flip it:
out.clipSpacePosition.y *= -1;
I am trying to create a reference line that runs through the origin and passes from negative to positive. See an example of what i am trying to achieve - see the threshold line. This threshold line must run through all three x, y coordinates (-1,-45,000), (0.0), (1, 45,000).
enter image description here
Below is my work so far.
http://jsfiddle.net/catio6/rhf6yre5/1/
I've looked at this for reference but have had had no luck after several hours of attempts of replicating this with all three x, y coordinates (-1,-45,000), (0.0), (1, 45,000): http://jsfiddle.net/phpdeveloperrahul/XvjfL/
$(function () {
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar']
},
series: [{
data: [29.9, 71.5, 256]
}]
}, function(chart) { // on complete
console.log("chart = ");
console.log(chart);
//chart.renderer.path(['M', 0, 0, 'L', 100, 100, 200, 50, 300, 100])
chart.renderer.path(['M', 75, 223.5,'L', 259, 47])//M 75 223.5 L 593 223.5
.attr({
'stroke-width': 2,
stroke: 'red'
})
.add();
});
});
So Highcharts doesn't have, as far as I know, a way to define a line that goes from/to infinity.
One idea I had to solve this issue for you is dynamically calculate the values for the line series based on your data. The idea is simple: Given the maximum values for X and Y you want to plot, we just need to limit the axis to a certain value that makes sense and calculate the values for the asymptote series in order to make it seems infinite. My algorithm looks like this:
// Get all your other data in a well formated way
let yourData = [
{x: 0.57, y: 72484},
{x: 0.57, y: 10000}
];
// Find which are the maximum x and y values
let maxX = yourData.reduce((max, v) => max > v.x ? max : v.x, -999999999);
let maxY = yourData.reduce((max, v) => max > v.y ? max : v.y, -999999999);
// Now you will limit you X and Y axis to a value that makes sense due the maximum values
// Here I will limit it to 10K above or lower on Y and 2 above or lower on X
let maxXAxis = maxX + 2;
let minXAxis = - (maxX + 2);
let maxYAxis = maxY + 10000;
let minYAxis = -(maxY + 10000);
// Now you need to calculate the values for the Willingness to pay series
let fn = (x) => 45000 * x; // This is the function that defines the Willingness to pay line
// Calculate the series values
let willingnessSeries = [];
for(let i = Math.floor(minXAxis); i <= Math.ceil(maxXAxis); i++) {
willingnessSeries.push([i, fn(i)]);
}
Here is a working fiddle: http://jsfiddle.net/n5xg1970/
I tested with several values for your data and all of them seem to be working ok.
Hope it helps
Regards
On a Highcharts heat map I specify the data in JSON format as follows:
var dataJson = [];
if (model.ActivityData != undefined) {
var index = 0;
if (model.ActivityData.length > 0) {
model.ActivityData.forEach(function(timeLineItem) {
dataJson.push({
x: $scope.getChartDate(timeLineItem.Date.split("T")[0]),
y: timeLineItem.IndicatorMeasure,
value: Math.round(timeLineItem.Amount * 100) / 100,
name: $scope.getChartDate(timeLineItem.Date.split("T")[0]),
});
});
}
}
"Value" is always between 0 and 1. I have added a tooltip showing "value" when hovering over a point.
I specify the colour stops as follows (it is
Orange: 0
White: 0.5
Green: 1
colorAxis: {
stops: [
[0, '#F9A847'],
[0.5, '#ffffff'],
[1, '#b1d4b1']
]
},
I would expect any values below 0.5 to be a shade of orange and any values over 0.5 to be a shade of green. However when the chart is rendered some values above 0.5 are orange.
Does Highcharts use "value" explicitly to set the colour and if so, why is it not respecting the specified colour tops. Or does highcharts adjust the colours by averaging (or something similar) to determine the colours.
Live demo at jsFiddle.
As you can see from the color axis, it extends from 0.4 to 0.7, which means that your middle color stop is actually as 0.55 in terms of axis values. The axis extremes is based on the data values.
So in order to make the axis extend from 0 to 1, like you probably expect, you need to set min and max. View jsFiddle.
colorAxis: {
stops: [
[0, '#F9A847'],
[0.5, '#ffffff'],
[1, '#b1d4b1']
],
min: 0,
max: 1
},
From the functions for MinAreaRect, does it return angles in the range of 0-360 degrees?
I am unsure as i have an object that is oriented at 90 degrees or so but I keep getting either -1 or -15 degrees. Could this be an openCV error?
Any guidance much appreciated.
Thanks
I'm going to assume you're using C++, but the answer should be the same if you're using C or Python.
The function minAreaRect seems to give angles ranging from -90 to 0 degrees, not including zero, so an interval of [-90, 0).
The function gives -90 degrees if the rectangle it outputs isn't rotated, i.e. the rectangle has two sides exactly horizontal and two sides exactly vertical. As the rectangle rotates clockwise, the angle increases (goes towards zero). When zero is reached, the angle given by the function ticks back over to -90 degrees again.
So if you have a long rectangle from minAreaRect, and it's lying down flat, minAreaRect will call the angle -90 degrees. If you rotate the image until the rectangle given by minAreaRect is perfectly upright, then the angle will say -90 degrees again.
I didn't actually know any of this (I procrastinated from my OpenCV project to find out how it works :/). Anyway, here's an OpenCV program that demonstrates minAreaRect if I haven't explained it clear enough already:
#include <stdio.h>
#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main() {
float angle = 0;
Mat image(200, 400, CV_8UC3, Scalar(0));
RotatedRect originalRect;
Point2f vertices[4];
vector<Point2f> vertVect;
RotatedRect calculatedRect;
while (waitKey(5000) != 27) {
// Create a rectangle, rotating it by 10 degrees more each time.
originalRect = RotatedRect(Point2f(100,100), Size2f(100,50), angle);
// Convert the rectangle to a vector of points for minAreaRect to use.
// Also move the points to the right, so that the two rectangles aren't
// in the same place.
originalRect.points(vertices);
for (int i = 0; i < 4; i++) {
vertVect.push_back(vertices[i] + Point2f(200, 0));
}
// Get minAreaRect to find a rectangle that encloses the points. This
// should have the exact same orientation as our original rectangle.
calculatedRect = minAreaRect(vertVect);
// Draw the original rectangle, and the one given by minAreaRect.
for (int i = 0; i < 4; i++) {
line(image, vertices[i], vertices[(i+1)%4], Scalar(0, 255, 0));
line(image, vertVect[i], vertVect[(i+1)%4], Scalar(255, 0, 0));
}
imshow("rectangles", image);
// Print the angle values.
printf("---\n");
printf("Original angle: %7.2f\n", angle);
printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
printf("---\n");
// Reset everything for the next frame.
image = Mat(200, 400, CV_8UC3, Scalar(0));
vertVect.clear();
angle+=10;
}
return 0;
}
This lets you easily see how the angle, and shape, of a manually drawn rectangle compares to the minAreaRect interpretation of the same rectangle.
Improving on the answer of #Adam Goodwin i want to add my little code that changes the behaviour a little bit:
I wanted to have the angle between the longer side and vertical (to me it is the most natural way to think about rotated rectangles):
If you need the same, just use this code:
void printAngle(RotatedRect calculatedRect){
if(calculatedRect.size.width < calculatedRect.size.height){
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+180);
}else{
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+90);
}
}
To see it in action just insert it in Adam Goodwins code:
printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
printAngle(calculatedRect);
printf("---\n");
After experiment, I find that if the long side is in the left of the bottom Point, the angle value is between long side and Y+ axis, but if the long side is in the right of the bottom Point, the angle value is between long side and X+ axis.
So I use the code like this(java):
rRect = Imgproc.minAreaRect(mop2f);
if(rRect.size.width<rRect.size.height){
angle = 90 -rRect.angle;
}else{
angle = -rRect.angle;
}
The angle is from 0 to 180.
After much experiment, I have found that the relationship between the rectangle orientation and output angle of minAreaRect(). It can be summarized in the following image
The following description assume that we have a rectangle with unequal height and width length, i.e., it is not square.
If the rectangle lies vertically (width < height), then the detected angle is -90. If the rectangle lies horizontally, then the detected angle is also -90 degree.
If the top part of the rectangle is in first quadrant, then the detected angle decreases as the rectangle rotate from horizontal to vertical position, until the detected angle becomes -90 degrees. In first quadrant, the width of detected rectangle is longer than its height.
If the top part of the detected rectangle is in second quadrant, then the angle decreases as the rectangle rotate from vertical to horizontal position. But there is a difference between second and first quadrant. If the rectangle approaches vertical position but has not been in vertical position, its angle approaches 0. If the rectangle approaches horizontal position but has not been in horizontal position, its angle approaches -90 degrees.
This post here is also good in explaining this.
It depends on the version of opencv, at least for Python.
For opencv-python='4.5.4.60'. The angle is that between positive x-axis and the first line the axis meets when it rotates anti-clock wise. The following is the code to snippet.
import cv2
import numpy as np
box1 = [[0, 0], [1, 0], [1, 2], [0, 2]]
cv2.minAreaRect(np.asarray(box1)) # angel = 90.0
box2 = [[0, 0], [2, 0], [2, 1], [0, 1]]
cv2.minAreaRect(np.asarray(box2)) # angel = 90.0
box3 = [[0, 0], [2**0.5, 2**0.5], [0.5*2**0.5, 1.5*2**0.5], [-0.5*2**0.5, 0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box3, dtype=np.float32)) # angle = 44.999
box4 = [[0, 0], [-2**0.5, 2**0.5], [-0.5*2**0.5, 1.5*2**0.5], [0.5*2**0.5, 0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box4, dtype=np.float32)) # angle = 45.0
box5 = [[0, 0], [-0.5*2**0.5, 0.5*2**0.5], [-2**0.5, 0], [-0.5*2**0.5, -0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box5, dtype=np.float32)) # angle = 45.0
For opencv-python='3.4.13.47'. The angle is that between positive x-axis and the first line the axis meets when it rotates clock wise. The following is the code to snippet.
import cv2
import numpy as np
box1 = [[0, 0], [1, 0], [1, 2], [0, 2]]
cv2.minAreaRect(np.asarray(box1)) # angel = -90.0
box2 = [[0, 0], [2, 0], [2, 1], [0, 1]]
cv2.minAreaRect(np.asarray(box2)) # angel = -90.0
box3 = [[0, 0], [2**0.5, 2**0.5], [0.5*2**0.5, 1.5*2**0.5], [-0.5*2**0.5, 0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box3, dtype=np.float32)) # angle = -44.999
box4 = [[0, 0], [-2**0.5, 2**0.5], [-0.5*2**0.5, 1.5*2**0.5], [0.5*2**0.5, 0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box4, dtype=np.float32)) # angle = -45.0
box5 = [[0, 0], [-0.5*2**0.5, 0.5*2**0.5], [-2**0.5, 0], [-0.5*2**0.5, -0.5*2**0.5]]
cv2.minAreaRect(np.asarray(box5, dtype=np.float32)) # angle = -45.0