项目作者: gampleman

项目描述 :
Angular + Highcharts Integration
高级语言: JavaScript
项目地址: git://github.com/gampleman/chartkit.git
创建时间: 2016-03-04T11:44:11Z
项目社区:https://github.com/gampleman/chartkit

开源协议:MIT License

下载


Charkit

Chartkit provides deep integration between the Angular framework and the wonderful
Highcharts library.

The Problem

When building a serious data visualization app, you often end up with a Highcharts
configuration object that runs several hundred or perhaps thousands lines long.
Since this is a single object, it is not particularly modular. Similarly, Angular
uses directives for its view layer, which are also configuration objects. Hence
you end up with a long object in a link function in another long object.

In Chartkit, charts are their own DI object. So instead of writing something like this:

  1. angular.module('MyApp').directive('overviewChart', function() {
  2. return {
  3. restrict: 'E',
  4. scope: {
  5. data: '='
  6. },
  7. link: function(scope, element) {
  8. var chart = new Highcharts.Chart({
  9. chart: {
  10. renderTo: element[0]
  11. },
  12. title: {
  13. text: 'Monthly Average Temperature',
  14. },
  15. xAxis: {
  16. categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  17. },
  18. yAxis: {
  19. title: {
  20. text: 'Temperature (°C)'
  21. },
  22. plotLines: [{
  23. value: 0,
  24. width: 1,
  25. color: '#808080'
  26. }]
  27. },
  28. legend: {
  29. layout: 'vertical',
  30. align: 'right',
  31. verticalAlign: 'middle',
  32. borderWidth: 0
  33. }
  34. });
  35. // code to initialize the data
  36. }
  37. };
  38. });

You can simplify to this:

  1. angular.module('MyApp').chart('overview', function() {
  2. return {
  3. title: {
  4. text: 'Monthly Average Temperature',
  5. },
  6. xAxis: {
  7. categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  8. },
  9. yAxis: {
  10. title: {
  11. text: 'Temperature (°C)'
  12. },
  13. plotLines: [{
  14. value: 0,
  15. width: 1,
  16. color: '#808080'
  17. }]
  18. },
  19. legend: {
  20. layout: 'vertical',
  21. align: 'right',
  22. verticalAlign: 'middle',
  23. borderWidth: 0
  24. }
  25. };
  26. });

Which will achieve the same result.

Native HTML rendering

Highcharts support HTML in tooltips and a bunch of other places. However, the templating
system seems designed for very simple HTML formatting. Building real world data visualization
software, you find yourself building tooltips like this:

Complex Tooltip

For this rendering tooltips via string concatenation becomes quickly annoying. Finally it
usually involves slightly different templating language then you use in the rest of the
app (we used Lo-dash’s _.template function before Charkit). This becomes quickly annoying.

Chartkit solves this problem by allowing you to pass an object anywhere where Highcharts
supports a formatter function (i.e. tooltips, dataLabels, axis labels). This object
can have either a template or templateUrl key, which is the Angular templating
system. It runs natively (i.e. doesn’t try to extract HTML strings and parse them
again) and supports the whole Angular system, including directives, controllers, etc.

So instead of doing something like:

  1. {
  2. tooltip: {
  3. useHTML: true,
  4. formatter: function() {
  5. return _.template('<div class="tooltip">' +
  6. '<% _.each(points, function(point) { %>' +
  7. '<span class="point-label <%=_.dasherize(point.series.name) %>' +
  8. '<%= point.series.name %">' +
  9. '<span class="value"><%= point.y %></span>' +
  10. '</span>' +
  11. '})' +
  12. '</div>')(this);
  13. }
  14. }
  15. }

You can now do:

  1. {
  2. tooltip: {
  3. useHTML: true,
  4. formatter: {
  5. templateUrl: 'views/overview/chart_tooltip.html'
  6. }
  7. }
  8. }
  1. <div class="tooltip">
  2. <span ng-repeat="point in points" class="point-label" ng-class="point.series.name | dasherize">
  3. {{point.series.name}} <span class="value">{{ point.y}}</span>
  4. </span>
  5. </div>

Which is much nicer. This allows you to also abstract complex logic into its own directive.

Note: Currently this requires you to set the useHTML option to true. Chartkit will
throw if this is not the case. We may support SVG rendering in the future, but currently
you will still need to use the Highcharts native API for that.

Directive API

The chart method accepts an injectable factory function, that should return a
chart definition object (this is exactly like directives). The chart definition
object is passed to the Highcharts constructor (this means you should still be able
to copy & paste snippets of Highcharts config into your chart factories). However,
the chart definition object accepts some extra keys. We’ve mentioned the special
treatment of formatter keys. In addition the object can contain the keys: require,
link and scope, which are the same as the Angular directive API.

The scope defaults to {data: '='} and serves as a standard way for charts to
receive data from controllers.

The link function will be called with the regular parameters. The final controllers
parameter will contain a reference to the Highcharts object containing your chart,
which will have been already initialized by the time the link function runs. This
will allow you to do any custom rendering.

transform

If you have data as a scope variable (i.e. you either keep the defaults or add it
yourself), you can also pass a transform key in the chart definition object. This
is a convenience high-level API, since often chart directive need to wait for API requests to
complete before they render their data. The transform function will be called with
the contents of the data scope variable whenever that changes and is considered
non-empty. The transform(data, chart, scope) function is called and should return
an array of series objects, which should contain an id property and some data.

These series objects are watched and the chart is automatically updated as appropriate
(i.e. missing series will be deleted, new ones added, and changed ones will be updated).

If you have a loading key in
your config, Chartkit will also automatically call
Chart.showLoading and
Chart.hideLoading for
you.

User Input

In Angular user input is handled by directives and scope binding. Highcharts on the
other hand use events objects. We wrap the events objects for you passing an additional
parameter at the end which is the scope object. We will also handle triggering a
digest for you.

  1. {
  2. events: {
  3. click: function(event, scope) {
  4. scope.selected = event.point.category;
  5. }
  6. }
  7. }

Set globals

Often you have a shared style. With Chartkit you can do something like this:

  1. angular.module('MyApp').config(function(Highcharts) {
  2. Highcharts.setOptions({
  3. colors: ['#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'],
  4. chart: {
  5. backgroundColor: {
  6. linearGradient: [0, 0, 500, 500],
  7. stops: [
  8. [0, 'rgb(255, 255, 255)'],
  9. [1, 'rgb(240, 240, 255)']
  10. ]
  11. },
  12. borderWidth: 2,
  13. plotBackgroundColor: 'rgba(255, 255, 255, .9)',
  14. plotShadow: true,
  15. plotBorderWidth: 1
  16. },
  17. title: {
  18. style: {
  19. color: '#000',
  20. font: 'bold 16px "Trebuchet MS", Verdana, sans-serif'
  21. }
  22. },
  23. subtitle: {
  24. style: {
  25. color: '#666666',
  26. font: 'bold 12px "Trebuchet MS", Verdana, sans-serif'
  27. }
  28. },
  29. xAxis: {
  30. gridLineWidth: 1,
  31. lineColor: '#000',
  32. tickColor: '#000',
  33. labels: {
  34. style: {
  35. color: '#000',
  36. font: '11px Trebuchet MS, Verdana, sans-serif'
  37. }
  38. },
  39. title: {
  40. style: {
  41. color: '#333',
  42. fontWeight: 'bold',
  43. fontSize: '12px',
  44. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  45. }
  46. }
  47. },
  48. yAxis: {
  49. alternateGridColor: null,
  50. minorTickInterval: 'auto',
  51. lineColor: '#000',
  52. lineWidth: 1,
  53. tickWidth: 1,
  54. tickColor: '#000',
  55. labels: {
  56. style: {
  57. color: '#000',
  58. font: '11px Trebuchet MS, Verdana, sans-serif'
  59. }
  60. },
  61. title: {
  62. style: {
  63. color: '#333',
  64. fontWeight: 'bold',
  65. fontSize: '12px',
  66. fontFamily: 'Trebuchet MS, Verdana, sans-serif'
  67. }
  68. }
  69. },
  70. legend: {
  71. itemStyle: {
  72. font: '9pt Trebuchet MS, Verdana, sans-serif',
  73. color: 'black'
  74. },
  75. itemHoverStyle: {
  76. color: '#039'
  77. },
  78. itemHiddenStyle: {
  79. color: 'gray'
  80. }
  81. },
  82. credits: false,
  83. labels: {
  84. style: {
  85. color: '#99b'
  86. }
  87. }
  88. });
  89. });

Maintainers

Maintained by Jakub Hampl (@gampleman) - http://gampleman.eu

License

(c) RightScale, inc. 2016

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.