Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active March 2, 2016 20:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save poteto/cd2bb47e77bf87c94d33 to your computer and use it in GitHub Desktop.
Save poteto/cd2bb47e77bf87c94d33 to your computer and use it in GitHub Desktop.
Integrating Highcharts.js into Ember
App.HighChartsComponent = Ember.Component.extend(App.HighchartsThemeMixin, {
classNames: [ 'highcharts-wrapper' ],
content: undefined,
chartOptions: undefined,
chart: null,
buildOptions: Em.computed('chartOptions', 'content.@each.isLoaded', function() {
var chartContent, chartOptions, defaults;
chartOptions = this.getWithDefault('chartOptions', {});
chartContent = this.get('content.length') ? this.get('content') : [
{
id: 'noData',
data: 0,
color: '#aaaaaa'
}
];
defaults = {
series: chartContent
};
return Em.merge(defaults, chartOptions);
}),
_renderChart: (function() {
this.drawLater();
this.buildTheme();
}).on('didInsertElement'),
contentDidChange: Em.observer('content.@each.isLoaded', function() {
var chart;
if (!(this.get('content') && this.get('chart'))) {
return;
}
chart = this.get('chart');
return this.get('content').forEach(function(series, idx) {
var _ref;
if ((_ref = chart.get('noData')) != null) {
_ref.remove();
}
if (chart.series[idx]) {
return chart.series[idx].setData(series.data);
} else {
return chart.addSeries(series);
}
});
}),
drawLater: function() {
Ember.run.scheduleOnce('afterRender', this, 'draw');
},
draw: function() {
var options;
options = this.get('buildOptions');
this.set('chart', this.$().highcharts(options).highcharts());
},
_destroyChart: (function() {
this._super();
this.get('chart').destroy();
}).on('willDestroyElement')
});
App.HighchartsThemeMixin = Ember.Mixin.create({
buildTheme: function() {
Highcharts.theme = {
colors: [
'#258be2',
'#666666',
'#f45b5b',
'#8085e9',
'#8d4654',
'#7798bf',
'#aaeeee',
'#ff0066',
'#eeaaee',
'#55bf3b',
'#df5353',
'#7798bf',
'#aaeeee'
],
chart: {
backgroundColor: null,
style: {
fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
}
},
title: {
style: {
color: 'black',
fontSize: '18px',
fontWeight: 'bold'
}
},
subtitle: {
style: {
color: 'black'
}
},
tooltip: {
borderWidth: 0,
style: {
fontSize: '16px'
}
},
legend: {
itemStyle: {
fontWeight: 'bold',
fontSize: '14px'
}
},
xAxis: {
labels: {
style: {
color: '#6e6e70',
fontSize: '16px'
}
},
title: {
style: {
fontSize: '14px'
}
}
},
yAxis: {
labels: {
style: {
color: '#6e6e70',
fontSize: '16px'
}
},
title: {
style: {
fontSize: '14px'
}
}
},
plotOptions: {
series: {
shadow: true
},
candlestick: {
lineColor: '#404048'
}
},
navigator: {
xAxis: {
gridLineColor: '#D0D0D8'
}
},
rangeSelector: {
buttonTheme: {
fill: 'white',
stroke: '#C0C0C8',
'stroke-width': 1,
states: {
select: {
fill: '#D0D0D8'
}
}
}
},
scrollbar: {
trackBorderColor: '#C0C0C8'
},
background2: '#E0E0E8',
global: {
timezoneOffset: new Date().getTimezoneOffset()
}
};
return Highcharts.setOptions(Highcharts.theme);
}
});
@balajinunna
Copy link

Hi,

Can you please give any sample demo through jsbin?

@detournemint
Copy link

^Seconded

@20v100
Copy link

20v100 commented Feb 10, 2015

Lauren, what is the best way to format Ember-Data into an array of objects that fit Highcharts requirements?

@Dhaulagiri
Copy link

@poteto
Copy link
Author

poteto commented Mar 1, 2015

For some reason I wasn't getting notifications from Github about these comments. Sorry!

@20v100 There is no real "easy" way, you'll have to massage your data into a POJO that Highcharts expects, probably using #map or #mapBy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment