2013-04-02 76 views
1

我正在嘗試將一些圖表集成到一個項目中,並且我已經得到了一個與示例一樣工作的示例,但正確顯示了我的數據,但是我希望能夠要做的就是將基線從0改爲另一個值,因爲我的大部分數據點數大於700,使得線條相對平坦,因爲它們都是高值。Raphael示例修改 - 分析的新基準

任何人都可以告訴我在這裏設置基準值的位置嗎?我不是一個JavaScript傢伙,所以我遇到了麻煩。

編輯:我也想能夠設置固定頂線,理想情況下,如果有人看到一個簡單的方法來做到這一點?

編輯2:找到固定頂線的方法:有一個「最大」變量。只是改變這一點。

示例在這裏。 http://raphaeljs.com/analytics.html

的JavaScript的問題是:

Raphael.fn.drawGrid = function (x, y, w, h, wv, hv, color) { 
    color = color || "#000"; 
    var path = ["M", Math.round(x) + .5, Math.round(y) + .5, "L", Math.round(x + w) + .5, Math.round(y) + .5, Math.round(x + w) + .5, Math.round(y + h) + .5, Math.round(x) + .5, Math.round(y + h) + .5, Math.round(x) + .5, Math.round(y) + .5], 
     rowHeight = h/hv, 
     columnWidth = w/wv; 
    for (var i = 1; i < hv; i++) { 
     path = path.concat(["M", Math.round(x) + .5, Math.round(y + i * rowHeight) + .5, "H", Math.round(x + w) + .5]); 
    } 
    for (i = 1; i < wv; i++) { 
     path = path.concat(["M", Math.round(x + i * columnWidth) + .5, Math.round(y) + .5, "V", Math.round(y + h) + .5]); 
    } 
    return this.path(path.join(",")).attr({stroke: color}); 
}; 

$(function() { 
    $("#data").css({ 
     position: "absolute", 
     left: "-9999em", 
     top: "-9999em" 
    }); 
}); 

window.onload = function() { 
    function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) { 
     var l1 = (p2x - p1x)/2, 
      l2 = (p3x - p2x)/2, 
      a = Math.atan((p2x - p1x)/Math.abs(p2y - p1y)), 
      b = Math.atan((p3x - p2x)/Math.abs(p2y - p3y)); 
     a = p1y < p2y ? Math.PI - a : a; 
     b = p3y < p2y ? Math.PI - b : b; 
     var alpha = Math.PI/2 - ((a + b) % (Math.PI * 2))/2, 
      dx1 = l1 * Math.sin(alpha + a), 
      dy1 = l1 * Math.cos(alpha + a), 
      dx2 = l2 * Math.sin(alpha + b), 
      dy2 = l2 * Math.cos(alpha + b); 
     return { 
      x1: p2x - dx1, 
      y1: p2y + dy1, 
      x2: p2x + dx2, 
      y2: p2y + dy2 
     }; 
    } 
    // Grab the data 
    var labels = [], 
     data = []; 
    $("#data tfoot th").each(function() { 
     labels.push($(this).html()); 
    }); 
    $("#data tbody td").each(function() { 
     data.push($(this).html()); 
    }); 

    // Draw 
    var width = 800, 
     height = 250, 
     leftgutter = 30, 
     bottomgutter = 20, 
     topgutter = 20, 
     colorhue = .6 || Math.random(), 
     color = "hsl(" + [colorhue, .5, .5] + ")", 
     r = Raphael("holder", width, height), 
     txt = {font: '12px Helvetica, Arial', fill: "#fff"}, 
     txt1 = {font: '10px Helvetica, Arial', fill: "#fff"}, 
     txt2 = {font: '12px Helvetica, Arial', fill: "#000"}, 
     X = (width - leftgutter)/labels.length, 
     max = Math.max.apply(Math, data), 
     Y = (height - bottomgutter - topgutter)/max; 
    r.drawGrid(leftgutter + X * .5 + .5, topgutter + .5, width - leftgutter - X, height - topgutter - bottomgutter, 10, 10, "#000"); 
    var path = r.path().attr({stroke: color, "stroke-width": 4, "stroke-linejoin": "round"}), 
     bgp = r.path().attr({stroke: "none", opacity: .3, fill: color}), 
     label = r.set(), 
     lx = 0, ly = 0, 
     is_label_visible = false, 
     leave_timer, 
     blanket = r.set(); 
    label.push(r.text(60, 12, "24 hits").attr(txt)); 
    label.push(r.text(60, 27, "22 September 2008").attr(txt1).attr({fill: color})); 
    label.hide(); 
    var frame = r.popup(100, 100, label, "right").attr({fill: "#000", stroke: "#666", "stroke-width": 2, "fill-opacity": .7}).hide(); 

    var p, bgpp; 
    for (var i = 0, ii = labels.length; i < ii; i++) { 
     var y = Math.round(height - bottomgutter - Y * data[i]), 
      x = Math.round(leftgutter + X * (i + .5)), 
      t = r.text(x, height - 6, labels[i]).attr(txt).toBack(); 
     if (!i) { 
      p = ["M", x, y, "C", x, y]; 
      bgpp = ["M", leftgutter + X * .5, height - bottomgutter, "L", x, y, "C", x, y]; 
     } 
     if (i && i < ii - 1) { 
      var Y0 = Math.round(height - bottomgutter - Y * data[i - 1]), 
       X0 = Math.round(leftgutter + X * (i - .5)), 
       Y2 = Math.round(height - bottomgutter - Y * data[i + 1]), 
       X2 = Math.round(leftgutter + X * (i + 1.5)); 
      var a = getAnchors(X0, Y0, x, y, X2, Y2); 
      p = p.concat([a.x1, a.y1, x, y, a.x2, a.y2]); 
      bgpp = bgpp.concat([a.x1, a.y1, x, y, a.x2, a.y2]); 
     } 
     var dot = r.circle(x, y, 4).attr({fill: "#333", stroke: color, "stroke-width": 2}); 
     blanket.push(r.rect(leftgutter + X * i, 0, X, height - bottomgutter).attr({stroke: "none", fill: "#fff", opacity: 0})); 
     var rect = blanket[blanket.length - 1]; 
     (function (x, y, data, lbl, dot) { 
      var timer, i = 0; 
      rect.hover(function() { 
       clearTimeout(leave_timer); 
       var side = "right"; 
       if (x + frame.getBBox().width > width) { 
        side = "left"; 
       } 
       var ppp = r.popup(x, y, label, side, 1), 
        anim = Raphael.animation({ 
         path: ppp.path, 
         transform: ["t", ppp.dx, ppp.dy] 
        }, 200 * is_label_visible); 
       lx = label[0].transform()[0][1] + ppp.dx; 
       ly = label[0].transform()[0][2] + ppp.dy; 
       frame.show().stop().animate(anim); 
       label[0].attr({text: data + " hit" + (data == 1 ? "" : "s")}).show().stop().animateWith(frame, anim, {transform: ["t", lx, ly]}, 200 * is_label_visible); 
       label[1].attr({text: lbl + " September 2008"}).show().stop().animateWith(frame, anim, {transform: ["t", lx, ly]}, 200 * is_label_visible); 
       dot.attr("r", 6); 
       is_label_visible = true; 
      }, function() { 
       dot.attr("r", 4); 
       leave_timer = setTimeout(function() { 
        frame.hide(); 
        label[0].hide(); 
        label[1].hide(); 
        is_label_visible = false; 
       }, 1); 
      }); 
     })(x, y, data[i], labels[i], dot); 
    } 
    p = p.concat([x, y, x, y]); 
    bgpp = bgpp.concat([x, y, x, y, "L", x, height - bottomgutter, "z"]); 
    path.attr({path: p}); 
    bgp.attr({path: bgpp}); 
    frame.toFront(); 
    label[0].toFront(); 
    label[1].toFront(); 
    blanket.toFront(); 
}; 

回答

2

這已經有一段時間,因爲你張貼的問題,但也許答案仍然是爲別人有所幫助:

您需要添加一些額外的變量以獲得最小數據並設置新基線:

最大值已在此處定義。

max = Math.max.apply(Math, data), //e.g. 800 

現在你只需做同樣獲得最小值:

min = Math.min.apply(Math, data), //e.g. 700 

從這兩個變量,你可以得到的值的範圍。而不是使用 '最大' '範圍' 來計算Y.

range = max - min, //e.g 100 
Y = (height - bottomgutter - topgutter)/range; 

現在跳轉到Y,Y0和Y2定義的循環和改變

y = Math.round(height - bottomgutter - Y * data[i]),  
Y0 = Math.round(height - bottomgutter - Y * data[i - 1]), 
Y2 = Math.round(height - bottomgutter - Y * data[i + 1]), 

到:

y = Math.round(height - bottomgutter - Y * data[i] + min * Y), 
Y0 = Math.round(height - bottomgutter - Y * data[i - 1] + min * Y), 
Y2 = Math.round(height - bottomgutter - Y * data[i + 1] + min * Y), 

如果你喜歡複製&粘貼,那是一個新的基線完整編輯的window.onload函數():

window.onload = function() { 
    function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) { 
     var l1 = (p2x - p1x)/2, 
      l2 = (p3x - p2x)/2, 
      a = Math.atan((p2x - p1x)/Math.abs(p2y - p1y)), 
      b = Math.atan((p3x - p2x)/Math.abs(p2y - p3y)); 
     a = p1y < p2y ? Math.PI - a : a; 
     b = p3y < p2y ? Math.PI - b : b; 
     var alpha = Math.PI/2 - ((a + b) % (Math.PI * 2))/2, 
      dx1 = l1 * Math.sin(alpha + a), 
      dy1 = l1 * Math.cos(alpha + a), 
      dx2 = l2 * Math.sin(alpha + b), 
      dy2 = l2 * Math.cos(alpha + b); 
     return { 
      x1: p2x - dx1, 
      y1: p2y + dy1, 
      x2: p2x + dx2, 
      y2: p2y + dy2 
     }; 
    } 
    // Grab the data 
    var labels = [], 
     data = []; 
    $("#data tfoot th").each(function() { 
     labels.push($(this).html()); 
    }); 
    $("#data tbody td").each(function() { 
     data.push($(this).html()); 
    }); 

    // Draw 
    var width = 800, 
     height = 250, 
     leftgutter = 30, 
     bottomgutter = 20, 
     topgutter = 20, 
     colorhue = .6 || Math.random(), 
     color = "hsl(" + [colorhue, .5, .5] + ")", 
     r = Raphael("holder", width, height), 
     txt = {font: '12px Helvetica, Arial', fill: "#fff"}, 
     txt1 = {font: '10px Helvetica, Arial', fill: "#fff"}, 
     txt2 = {font: '12px Helvetica, Arial', fill: "#000"}, 
     X = (width - leftgutter)/labels.length, 
     max = Math.max.apply(Math, data), 
     min = Math.min.apply(Math, data), 
     range = max - min, 
     Y = (height - bottomgutter - topgutter)/range; 
    r.drawGrid(leftgutter + X * .5 + .5, topgutter + .5, width - leftgutter - X, height - topgutter - bottomgutter, 10, 10, "#000"); 
    var path = r.path().attr({stroke: color, "stroke-width": 4, "stroke-linejoin": "round"}), 
     bgp = r.path().attr({stroke: "none", opacity: .3, fill: color}), 
     label = r.set(), 
     lx = 0, ly = 0, 
     is_label_visible = false, 
     leave_timer, 
     blanket = r.set(); 
    label.push(r.text(60, 12, "24 hits").attr(txt)); 
    label.push(r.text(60, 27, "22 September 2008").attr(txt1).attr({fill: color})); 
    label.hide(); 
    var frame = r.popup(100, 100, label, "right").attr({fill: "#000", stroke: "#666", "stroke-width": 2, "fill-opacity": .7}).hide(); 

    var p, bgpp; 
    for (var i = 0, ii = labels.length; i < ii; i++) { 
     var y = Math.round(height - bottomgutter - Y * data[i] + min * Y), 
      x = Math.round(leftgutter + X * (i + .5)), 
      t = r.text(x, height - 6, labels[i]).attr(txt).toBack(); 
     if (!i) { 
      p = ["M", x, y, "C", x, y]; 
      bgpp = ["M", leftgutter + X * .5, height - bottomgutter, "L", x, y, "C", x, y]; 
     } 
     if (i && i < ii - 1) { 
      var Y0 = Math.round(height - bottomgutter - Y * data[i - 1] + min * Y), 
       X0 = Math.round(leftgutter + X * (i - .5)), 
       Y2 = Math.round(height - bottomgutter - Y * data[i + 1] + min * Y), 
       X2 = Math.round(leftgutter + X * (i + 1.5)); 
      var a = getAnchors(X0, Y0, x, y, X2, Y2); 
      p = p.concat([a.x1, a.y1, x, y, a.x2, a.y2]); 
      bgpp = bgpp.concat([a.x1, a.y1, x, y, a.x2, a.y2]); 
     } 
     var dot = r.circle(x, y, 4).attr({fill: "#333", stroke: color, "stroke-width": 2}); 
     blanket.push(r.rect(leftgutter + X * i, 0, X, height - bottomgutter).attr({stroke: "none", fill: "#fff", opacity: 0})); 
     var rect = blanket[blanket.length - 1]; 
     (function (x, y, data, lbl, dot) { 
      var timer, i = 0; 
      rect.hover(function() { 
       clearTimeout(leave_timer); 
       var side = "right"; 
       if (x + frame.getBBox().width > width) { 
        side = "left"; 
       } 
       var ppp = r.popup(x, y, label, side, 1), 
        anim = Raphael.animation({ 
         path: ppp.path, 
         transform: ["t", ppp.dx, ppp.dy] 
        }, 200 * is_label_visible); 
       lx = label[0].transform()[0][1] + ppp.dx; 
       ly = label[0].transform()[0][2] + ppp.dy; 
       frame.show().stop().animate(anim); 
       label[0].attr({text: data + " hit" + (data == 1 ? "" : "s")}).show().stop().animateWith(frame, anim, {transform: ["t", lx, ly]}, 200 * is_label_visible); 
       label[1].attr({text: lbl + " September 2008"}).show().stop().animateWith(frame, anim, {transform: ["t", lx, ly]}, 200 * is_label_visible); 
       dot.attr("r", 6); 
       is_label_visible = true; 
      }, function() { 
       dot.attr("r", 4); 
       leave_timer = setTimeout(function() { 
        frame.hide(); 
        label[0].hide(); 
        label[1].hide(); 
        is_label_visible = false; 
       }, 1); 
      }); 
     })(x, y, data[i], labels[i], dot); 
    } 
    p = p.concat([x, y, x, y]); 
    bgpp = bgpp.concat([x, y, x, y, "L", x, height - bottomgutter, "z"]); 
    path.attr({path: p}); 
    bgp.attr({path: bgpp}); 
    frame.toFront(); 
    label[0].toFront(); 
    label[1].toFront(); 
    blanket.toFront(); 
}; 
+0

太棒了,謝謝你的出色答案 –