3

x 軸に沿ってミリ秒を使用して gRaphael で折れ線グラフを描画すると、データ ポイントの配置に矛盾が生じることがよくあります。ほとんどの場合、最初のデータ ポイントは y 軸の左側にあります (以下のフィドルを参照)。最後のデータ ポイントがビュー ボックスの右側を超えたり、x 軸の終端を超えたりする場合もあります。 .

誰も知っていますか:1)これが発生する理由、2)それを防ぐ方法、および/または3)それを確認する方法(いつ発生したか/どれだけ発生したかがわかれば、変換を使用して線/点を移動できます) .

私のコード:

var r = Raphael("holder"),
txtattr = { font: "12px sans-serif" };
var r2 = Raphael("holder2"),
txtattr2 = { font: "12px sans-serif" };

var x = [], y = [], y2 = [], y3 = [];

for (var i = 0; i < 1e6; i++) {
    x[i] = i * 10;
    y[i] = (y[i - 1] || 0) + (Math.random() * 7) - 3;
}
var demoX = [[1, 2, 3, 4, 5, 6, 7],[3.5, 4.5, 5.5, 6.5, 7, 8]];
var demoY = [[12, 32, 23, 15, 17, 27, 22], [10, 20, 30, 25, 15, 28]];

var xVals = [1288885800000, 1289929440000, 1290094500000, 1290439560000, 1300721700000,   1359499228000, 1359499308000, 1359499372000];
var yVals = [80, 76, 70, 74, 74, 78, 77, 72];
var xVals2 = [1288885800000, 1289929440000];
var yVals2 = [80, 76];

var lines = r.linechart(10, 10, 300, 220, xVals, yVals, { nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: true })
.hoverColumn(function () {
    this.tags = r.set();

    for (var i = 0, ii = this.y.length; i < ii; i++) {
        this.tags.push(r.tag(this.x, this.y[i], this.values[i], 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
        }
    }, function () {
        this.tags && this.tags.remove();
        });

lines.symbols.attr({ r: 3 });


var lines2 = r2.linechart(10, 10, 300, 220, xVals2, yVals2, { nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: true })
.hoverColumn(function () {
    this.tags = r2.set();

    for (var i = 0, ii = this.y.length; i < ii; i++) {
        this.tags.push(r.tag(this.x, this.y[i], this.values[i], 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
        }
    }, function () {
        this.tags && this.tags.remove();
        });

lines2.symbols.attr({ r: 3 });

gRaphael を使用する必要があり、x 軸はミリ秒単位である必要があります (後でカスタマイズされた日付文字列でラベル付けされます)

主なフィドルの例: http://jsfiddle.net/kcar/aNJxf/

2 番目のフィドルの例 (ページの 4 番目の例では、両方の軸のエラーが頻繁に表示されます): http://jsfiddle.net/kcar/saBnT/

根本的な原因は snapEnds 関数 (g.raphael.js の 718 行目) です。丸めは、場合によっては問題ありませんが、日付に年を加算または減算する場合もあります。

この時点以降はまだ完全には進んでいませんが、丸めが狂うたびにデータポイントがずれるので、そうでないときではなく、先に進み、これがチャート列の計算で問題を引き起こしていると仮定します、また、snapEnds に送信される前に、値が正しく計算されているだけでなく、誤って計算されたデータを受信して​​いないことを確認します。

g.raphael.js からのその関数のコード

snapEnds: function(from, to, steps) {
    var f = from,
        t = to;

    if (f == t) {
        return {from: f, to: t, power: 0};
    }

    function round(a) {
        return Math.abs(a - .5) < .25 ? ~~(a) + .5 : Math.round(a);
    }

    var d = (t - f) / steps,
        r = ~~(d),
        R = r,
        i = 0;

    if (r) {
        while (R) {
            i--;
            R = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
        }

        i ++;
    } else {
        if(d == 0 || !isFinite(d)) {
            i = 1;
        } else {
            while (!r) {
                i = i || 1;
                r = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
                i++;
            }
        }

        i && i--;
    }

    t = round(to * Math.pow(10, i)) / Math.pow(10, i);

    if (t < to) {
        t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i);
    }

    f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i);
    return { from: f, to: t, power: i };
},
4

4 に答える 4

2

snapEnds から丸めのナンセンスを削除し、問題がなくなりました。軸またはグラフの他の領域からの欠点に気づきませんでした。もし見かけたらぜひ聴いてみたいものです。

g.raphael.js からのその関数のコード:

snapEnds: function(from, to, steps) {
     return {from: from, to: to, power: 0};       
},
于 2013-02-14T20:31:17.547 に答える
1

v0.50 から v0.51 にアップグレードすると、問題が解決しました。

于 2013-04-25T10:27:35.233 に答える
1

こんにちは、これにコメントする場合:

        if (valuesy[i].length > width - 2 * gutter) {
            valuesy[i] = shrink(valuesy[i], width - 2 * gutter);
            len = width - 2 * gutter;
        }

        if (valuesx[i] && valuesx[i].length > width - 2 * gutter) {
            valuesx[i] = shrink(valuesx[i], width - 2 * gutter);
        }

g.line.jsでは、問題を解決するようで、y軸の値についても同様の問題を解決します。

于 2013-03-11T17:48:19.373 に答える
0

なぜそれが発生するのかはまだわかりませんが、透過的なセットを追加することは望ましいオプションではありませんでした。データポイントがグラフの外側にレンダリングされているかどうかを確認する最も簡単な方法は、軸セットの境界ボックスとデータポイントの境界ボックスを取得し、x値とx2値の差を確認することです。

誰かがデータポイントセットのスケーリングを手伝ってくれるか、これをまったく起こさないようにする方法を見つけてくれるなら、私はまだ喜んで感謝/賛成票の回答をします

//assuming datapoints  is the Raphael Set for the datapoints, axes is the 
//Raphael Set for the axis, and datalines is the Raphael Set for the 
//datapoint lines
var pointsBBox = datapoints.getBBox();
var axesBBox = axes.getBBox();
var xGapLeft = Math.ceil(axesBBox.x - pointsBBox.x); 
//rounding up to integer to simplify, and the extra boost from y-axis doesn't 
//hurt, <1 is a negligible distance in transform
var xGapRight = Math.ceil(axesBBox.x2 - pointsBBox.x2);
var xGap = 0;
if(xGapLeft > 0){
     datapoints.transform('t' +xGapLeft +',0');
     datalines.transform('t' +xGapLeft +',0');
     xGap = xGapLeft;
}else if (xGapRight < 0) { //using else if because if it is a scale issue it will
//be too far right & too far left, meaning both are true and using transform will
//just shift it right then left and you are worse off than before, using 
//set.transform(scale) works great on dataline but when using on datapoints scales
// symbol radius not placement
     if (xGapLeft < 0 && xGapRight < xGapLeft) { xGapRight = xGapLeft; }  
//in this case the initial point is right of y-axis, the end point is right of 
//x-axis termination, and the difference between last point/axis is greater than
//difference between first point/axis

     datapoints.transform('t' +xGapRight +',0');
     datalines.transform('t' +xGapRight +',0');
     xGap = xGapRight;
}
rehookHoverOverEvent(xGap);  //there are so many ways to do this just leaving it
//here as a call to do so, if you don't the hover tags will be over the original 
//datapoints instead of the new location, at least they were in my case.
于 2013-02-13T17:54:47.320 に答える