2014-02-21 28 views
2

如何找到兩個系列(Keen.Series)之間的差異,然後將它繪製在折線圖上?Keen IO - 如何找到兩個Keen.Series之間的區別

我想出瞭如何找到兩個度量(Keen.Metric)之間的差異,但無法弄清楚如何從兩個系列中獲取每個結果並相應地減去它們。

如果這會有所幫助,這裏是如何找到這兩個指標之間的區別:

Keen.onChartsReady(function(){ 
    // Create a Metric containing our total $ amount in Keen IO credit card transactions (to and from) 
    // Excludes: refund transactions from Stripe to card and declined cards 
    var myMetric = new Keen.Metric("Stripe_Event", 
    analysisType: "sum", 
    targetProperty: "data.object.amount", 
    filters: [ 
     {"property_name":"data.object.captured","operator":"eq","property_value":true}, 
     {"property_name":"data.object.amount_refunded","operator":"eq","property_value":0}] 
    }); 

    // Create a Metric containing our total number of refunds 
    var myMetric2 = new Keen.Metric("Stripe_Event", { 
    analysisType: "sum", 
    targetProperty: "data.object.amount_refunded", 
    filters: [ 
    {"property_name":"type","operator":"eq","property_value":"charge.refunded"}] 
    }); 

    // Find the difference and convert to dollars 
    myMetric.getResponse(function(response){ 
    firstValue = response.result; 
    myMetric2.getResponse(function(nextResponse){ 
     secondValue = nextResponse.result; 
     difference = firstValue-secondValue; 
     dollar = difference/100; 
     console.log(dollar); 

     //Create a Number visualization for that metric. 
     var myNumberVisualization = new Keen.Number(myMetric2, { 
     height: "300", 
     width: "600", 
     prefix:"$", 
     label: "Revenue" 
     }); 

    //Draw the visualization in a div 
    myNumberVisualization.draw(document.getElementById("myDiv"), 
     {"result":dollar}); 
    }); 
    }); 
}); 

我想出如何採取一個Keen.Series和每個結果便士轉換成美元格式(條帶化API輸出交易必須除以100才能找到美元值):

Keen.onChartsReady(function() { 
    // Create a Series 
    var mySeries = new Keen.Series("Stripe_Event", { 
    analysisType: "sum", 
    timeframe: "this_month", 
    interval: "daily", 
    targetProperty: "data.object.amount", 
    }); 

    // Convert results from pennies to dollars 
    var resultsInDollars = {} 

    mySeries.getResponse(function(response){ 
    result = response.result 
    $.each(result, function(index, object){ 
     result[index]["value"] = object["value"]/100 
    }); 

    resultsInDollars = { 
     result: result 
     } 

    // Line chart settings 
    var myLineChart = new Keen.LineChart(mySeries, { 
      height: 240, 
      width: 300, 
      xAxisLabelAngle:45 
    }); 
    // Draw line chart 
    myLineChart.draw(document.getElementById("overview"),resultsInDollars); 
    }); 
}); 

在此先感謝。

回答

2

下面是一些非常相似的東西 - 分兩個折線圖。

https://gist.github.com/wetzler/9127225

只要改變從分裂的操作減法,你應該是好去。

+2

這是一個共享Josh答案的解決方案。 https://gist.github.com/brentchow/9147264 –

相關問題