您如何找到两个系列(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 并将每个结果转换为美元格式(Stripe 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);
});
});
提前致谢。