Highcharts - 获取过境点系列的过境点 [英] Highcharts - Get crossing point of crossing series

查看:222
本文介绍了Highcharts - 获取过境点系列的过境点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取特定系列(x)的系列(a,b,c,d)的多个交叉点。我似乎无法找到任何可以帮助我完成这项任务的功能。



我最好的选择是测量x中每个单点与a,b,c,d中每个单点的距离,并假设距离达到在某个门槛之下,这个点必须是一个过境点。我认为这种方法计算量太大,看起来很脏。我相信一定有更简单或更好的方法,甚至可能是highcharts内部的API自带API函数。

我搜索了不同的来源和网站,但我找不到任何解决方案对此。一些人将回归用作他们解决方案的一部分。我对回归知之甚少,但也许回归是唯一的方法?

同样由于我们系列的复杂性质,我也相信回归是宁可难以利用。

解决方案

这是一个深入研究的问题。然而,在优化之前,我会尝试一种计算量大的蛮力方法。借用这里出色的答案,我已经编码与highcharts快速整合(小提琴 here ):

  get_line_intersection = function(p0,p1,p2,p3)
{
var p0_x = p0.x;
var p0_y = p0.y;
var p1_x = p1.x;
var p1_y = p1.y;
var p2_x = p2.x;
var p2_y = p2.y;
var p3_x = p3.x;
var p3_y = p3.y;

var s1_x,s1_y,s2_x,s2_y;
s1_x = p1_x - p0_x; s1_y = p1_y-p0_y;
s2_x = p3_x - p2_x; s2_y = p3_y-p2_y;

var s =( - s1_y *(p0_x - p2_x)+ s1_x *(p0_y - p2_y))/( - s2_x * s1_y + s1_x * s2_y);
var t =(s2_x *(p0_y-p2_y)-s2_y *(p0_x-p2_x))/( - s2_x * s1_y + s1_x * s2_y);如果(s> = 0& s< = 1& t> = 0& t< = 1)


{
return [p0_x +(t * s1_x),p0_y +(t * s1_y)];
}

return false;


$(function(){
$('#container')。highcharts({
series:[{
name:'line1 ',
data:$ .map(new Array(10),function(){return Math.random()* 10;})
},{
name:'line2',
data:$ .map(new Array(10),function(){return Math.random()* 10;})
},{
名称:'intersect',
data:[],
type:'scatter'
}
]
},function(chart){
var s0 = chart.series [0] .points;
var s1 = chart.series [1] .points;
var s2 = chart.series [2];
var n0 = s0.length;
var n1 (j = 1; j var i,j,isect;
){
if(isect = get_line_intersection(s0 [i-1],s0 [i],
s1 [j-1],s1 [j])){
s2.addPoint(isect,false,false);

}
}
}
chart.redraw();
});
});


I am currently trying to extract the points of multiple crossings of series (a,b,c,d) of a specific series (x). I can't seem to find any function that can aid me in this task.

My best bet is to measure the distance of every single point in x with every single point in a,b,c,d... and assume when the distance reaches under some threshold, the point must be a crossing point. I think this approach is far too computational heavy and seems "dirty". I believe there must be easier or better ways, even perhaps functions within highcharts own API.

I have searched various sources and sites, but I can't really find any solutions to this. A few people have used regression as a part of their solution. I don't know much about regression, but perhaps regression is the only way?

Also due to the "complex" nature of our series, I also believe regression is rather difficult to utilize.

解决方案

This is a well studied problem. Before optimizing, though, I'd try the "computational heavy" brute-force approach. Borrowing from the excellent answers here, I've coded up a quick integration with highcharts (fiddle here):

get_line_intersection = function(p0,p1,p2,p3)
{        
    var p0_x = p0.x;
    var p0_y = p0.y;
    var p1_x = p1.x;
    var p1_y = p1.y;
    var p2_x = p2.x;
    var p2_y = p2.y;
    var p3_x = p3.x;
    var p3_y = p3.y;  

    var s1_x, s1_y, s2_x, s2_y;
    s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
    s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;

    var s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
    var t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

    if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
    {
        return [p0_x + (t * s1_x),p0_y + (t * s1_y)];
    }

    return false;
}

$(function () {
    $('#container').highcharts({
        series: [{
            name: 'line1',
            data: $.map(new Array(10), function(){return Math.random() * 10;})
        }, {
            name: 'line2',
            data: $.map(new Array(10), function(){return Math.random() * 10;})
        }, {
            name: 'intersect',
            data: [],
            type: 'scatter'
            }
        ]
    }, function(chart){
        var s0 = chart.series[0].points;
        var s1 = chart.series[1].points;
        var s2 = chart.series[2];
        var n0 = s0.length;
        var n1 = s1.length;
        var i,j,isect;
        for (i = 1; i < n0; i++){
           for (j = 1; j < n1; j++){
               if (isect = get_line_intersection(s0[i-1],s0[i],
                                   s1[j-1],s1[j])){
                   s2.addPoint(isect, false, false);

               }
           } 
        }
        chart.redraw();
    });
}); 

这篇关于Highcharts - 获取过境点系列的过境点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆