Ember js integrating with D3 - d3.js

I want to include a graphs which have 2-y axis with relevant 2 data sets in my ember project.
Since I am newbie to D3 js as well as ember js I do some googling and come up with npm packages for doing that like these ember-d3, ember-charts, ember-d3-helpers. etc..
But all of them seems to me there is a bit of learning curve.
My questions are, from using those kind of packages can I integrate and draw my graphs?
Or else can I use directly D3 without any npm plunging?
Are there any suitable way to integrate D3 in ember project?

The simplested solution I am currently using is to import your third part libraries using ember-cli-build.js (see code below)
/*jshint node:true*/
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
// Add options here
});
app.import('./bower_components/d3/d3.js');
app.import('./bower_components/c3-0.4.11/c3.js');
app.import('./bower_components/c3-0.4.11/c3.css');
return app.toTree();
};
The code above let you have global access to d3 and c3 (which means which lib can be direct accessed in your window object ).
C3 is a really simple and powerful reusable d3 chart library and I recommend to every new d3 users.
http://c3js.org/
After you have your libs ready, put your chart generate code inside the component's didInsertElement hook (below just a sample and you can make it better for sure)
didInsertElement() {
this._super(...arguments);
const chart = c3.generate({
data: {
columns: []
},
axis: {
x: {
type: 'category',
categories: [
]
}
},
zoom: {
enabled: true
},
legend: {
show: false
},
grid: {
y: {show: true}
}
});
this.set('globalChart.lineChart', chart);
},
You can read more about Ember dependencies management at,
https://guides.emberjs.com/v2.11.0/addons-and-dependencies/managing-dependencies/

Related

Using C3.js, when loading data via URL/JSON, is there a way to include groups in the data?

If I'm creating a C3.js graph using:
var chart = c3.generate({
bindto: '#my-chart-div',
data: {
url: '/data/data.json',
mimeType: 'json',
type: 'area',
groups: [
['thing1', 'thing2', 'thing3']
]
},
});
Is there a way for me to include the groupings (thing1, etc) in the URL-loaded JSON data?
I couldn't find a way to include groups in JSON data.
The best work-around I found to to this was using the onrendered callback, and inside that callback, retrieve the data headers from the chart itself, then apply them in the grouping you want using groups:
onrendered: function () {
if ( $('#chart-div').data('stacked') != true ) {
chart.groups( [ chart.data().map(function(x){return x.id}) ]);
$('#chart-div').data('stacked', true);
}
}
Note the line that checks to see if the chart has already been stacked - applying a grouping to the chart re-renders it, which triggers this callback again (prevents an infinite loop).

Use Android AccessibilityService with nativescript

Currently I make myself familiar with nativescript and trying to figure out what you can do with it. One thing that I find very interesting and for which I am more interested in is how I can use native Android functions.
As challenge for understanding and learning I have picked out the possibility in apps fill in text fields with specific values like some password managers apps do. If I understand it right, it shouldn't big voodoo.
How, I found two examples. The first one is the autofill feature from the keepass2android app. Sources: https://keepass2android.codeplex.com/SourceControl/changeset/view/9ddc142d987880118be38ee1d69943f2809738d3#src/java/KP2ASoftkeyboard_AS/app/src/main/java/keepass2android/autofill/AutoFillService.java
Other sample I found here: AccessibilityNodeInfo - send text
First quick and dirty I hack the app.js, find out how the android native functions works in nativescript. So I want trigger somehow the different events but only init is triggered (so far I also understand it also).
var applicationModule = require("application");
var platform = require("platform");
var context = applicationModule.android.context;
android.accessibilityservice.AccessibilityService;
var aa = android.accessibilityservice.AccessibilityService.extend({
init: function() {
console.log("init()");
},
onInit: function() {
console.log("onInit()");
},
create: function() {
console.log("create()");
},
onCreate: function() {
console.log("onCreate()");
},
destroy: function() {
console.log("destroy()");
},
onDestroy: function() {
console.log("onDestroy()");
},
accessibilityEvent: function(event) {
console.log("accessibilityEvent");
},
onAccessibilityEvent: function(event) {
console.log("onAccessibilityEvent");
},
interrupt: function() {
console.log("interrupt");
},
onInterrupt: function() {
console.log("interrupt");
},
serviceConnected: function() {
console.log("serviceConnected");
},
onServiceConnected: function() {
console.log("onServiceConnected");
}
});
var a = new aa();
Probably android.accessibilityservice.AccessibilityService must somehow be extended in a different way, get it working. But how?
It would be a big deal if the onAccessibilityEvent function would be triggered in nativescript.
Edit:
I made some progress. Found some other nativescript sources and samples and modified my code. Now:
var applicationModule = require("application");
var platform = require("platform");
var utils = require("utils/utils");
var context = android.content.Context;
var ad = utils.ad.getApplicationContext();
var aa = ad.getSystemService(context.ACCESSIBILITY_SERVICE);
But the way, I can register onAccessibilityEvent is currently not clear for me.
You need to register your service (the class from your first code block) similar to this example:
http://developer.android.com/training/accessibility/service.html
and place your logic in onAccessibilityEvent override.

Create highcharts chart using AJAX/JSON

I am building a website that uses the Highcharts library to display a single line series chart. I am using AJAX to retrieve historical financial data from yahoo finance using their YQL.
The ajax call is working correctly and I can view the returned data in the console for example
console.log( data.query.results.quote[0].Close );
returns the closing price value 457.84
How do I now build the single line series chart using this data?
I cannot find a simple explanation anywhere of how to create the chart using AJAX data.
Thanks
edit: I am fetching the data in AJAX and it works correctly but trying to make the chart work with JSON is where im having difficulties.
This is what my code currently looks like: http://jsfiddle.net/JbGvx/
This is the demo code off the HighStocks website: http://jsfiddle.net/xDhkz/
I think the problem is with the date formatting of the AJAX request. The date is being returned like so 2013-02-25 but the chart wants JS timestamps. Is there a way that I can extract the date from the AJAX, convert it using Date.UTC and then make the chart using the converted data?
If you are considering stock data, consider using HighStocks instead of HighCharts. It can handle many data points much faster than HighCharts is able to.
HighStocks has an example where they pull in AJAX data (1.6 million points) asynchronously here: http://www.highcharts.com/stock/demo/lazy-loading
var chartSeriesData = [];
var chartCategory = [];
$.each(response, function() {
if(this.name!="TOTAL" && this.no!="0")
{
var series_name = this.name;
var series_data = this.no;
var series = [
series_name,
parseFloat(series_data)
];
chartSeriesData.push(series);
}
});
//initialize options for highchart
var options = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'SalesOrder '
},
tooltip: {
pointFormat: '{series.name}: <b>{point.y}</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
center:['60%','60%'],
size:150
,
dataLabels: {
enabled: true,
color: '#000000',
distance: 40,
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.y} '
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data:chartSeriesData //load array created from json
}]
}
//options.series[0].setData(datavaluejson);
var chart= $('#container').highcharts(options);

how to integrate d3 with require.js

I'm having problems trying to integrate d3 into a require/backbone application. My main.js contains something like:
require.config({
paths: {
d3: 'libs/d3/d3.v2.min'
backbone: ...
...
}
});
And my backbone view something like (in coffeescript)
define ['backbone','d3',...], (Backbone,d3,...) ->
MyView = Backbone.View.extend
initialize: () ->
d3.somefunction
Console log says d3 is null. Is there a simple way to integrate d3 into this type of application?
d3 does not call define() to declare a module, so the local d3 reference to the backbone view will not be what you want. Either use the global variable made by d3:
define(['backbone', 'd3'], function (backbone, ignore) {
//Use global d3
console.log(d3);
});
Or use the shim config to declare an exports value for d3:
requirejs.config({
shim: {
d3: {
exports: 'd3'
}
}
});
That will tell requirejs to use the global d3 as the module value for d3.
Since d3.v3 now registers itself as an AMD module if a compatible library is present, you'll need to use this workaround (from http://pastebin.com/d5ZDXzL2):
requirejs.config({
paths: {
d3: "scripts/d3.v3",
nvd3: "scripts/nv.d3"
},
shim: {
nvd3: {
exports: 'nv',
deps: ['d3.global']
}
}
});
// workaround for nvd3 using global d3
define("d3.global", ["d3"], function(_) {
d3 = _;
});
define('myModule', ['nvd3'], function(nc) { /* .... */ });
I am not sure why but this works. I am not sure if it is the proper way of loading a module.
require(['libs/jquery', 'libs/d3'], function($, ignore) {
d3 = require('libs/d3');
});

Extjs How to access the name of the chart being used by store

I have a store being used by several charts. I get my data remotely with an ajax call. In the php script that I link it too, I'm just going to change the parameters of my query to adjust for the different charts.
Here's my idea: I pass the title of the chart's panel as a parameter to my php script. That'll tell me which chart it is.
How do I access the title of whatever chart the store is being used by?
var my_store = Ext.create('Ext.data.JsonStore', {
fields: ['project', 'accepted', 'rejected', 'deleted', 'undefined'],
proxy: {
type: 'ajax',
url: 'generate_proj.php',
extraParams: {foo: **chart.id**},
reader: {
type: 'json'
}
},
autoLoad: true,
listeners: {
beforeload: function(store,operation) {
//operation.params.foo = this.idname;
},
load: function(obj,records) {
var text = Ext.decode(obj.responseText);
Ext.each(records,function(rec) {
});
}
}
});
Here's what I've done so far. Getting the name of a single chart/panel is no problem. I want the store to be able to dynamically read the name of what's using it. How?
Somewhere, you have some code that switches between the various charts. During that code, you could do something like
activeChart.getChartStore().proxy.extraParams.foo = activeChart.getId();
where activeChart is whatever reference you have to the chart you are about to show. Then when you load the store, it'll send the correct parameter.

Resources