Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

06-Advanced.md 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ---
  2. title: Advanced usage
  3. anchor: advanced-usage
  4. ---
  5. ### Prototype methods
  6. For each chart, there are a set of global prototype methods on the shared `ChartType` which you may find useful. These are available on all charts created with Chart.js, but for the examples, let's use a line chart we've made.
  7. ```javascript
  8. // For example:
  9. var myLineChart = new Chart(ctx).Line(data);
  10. ```
  11. #### .clear()
  12. Will clear the chart canvas. Used extensively internally between animation frames, but you might find it useful.
  13. ```javascript
  14. // Will clear the canvas that myLineChart is drawn on
  15. myLineChart.clear();
  16. // => returns 'this' for chainability
  17. ```
  18. #### .stop()
  19. Use this to stop any current animation loop. This will pause the chart during any current animation frame. Call `.render()` to re-animate.
  20. ```javascript
  21. // Stops the charts animation loop at its current frame
  22. myLineChart.stop();
  23. // => returns 'this' for chainability
  24. ```
  25. #### .resize()
  26. Use this to manually resize the canvas element. This is run each time the browser is resized, but you can call this method manually if you change the size of the canvas nodes container element.
  27. ```javascript
  28. // Resizes & redraws to fill its container element
  29. myLineChart.resize();
  30. // => returns 'this' for chainability
  31. ```
  32. #### .destroy()
  33. Use this to destroy any chart instances that are created. This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js.
  34. ```javascript
  35. // Destroys a specific chart instance
  36. myLineChart.destroy();
  37. ```
  38. #### .toBase64Image()
  39. This returns a base 64 encoded string of the chart in it's current state.
  40. ```javascript
  41. myLineChart.toBase64Image();
  42. // => returns png data url of the image on the canvas
  43. ```
  44. #### .generateLegend()
  45. Returns an HTML string of a legend for that chart. The template for this legend is at `legendTemplate` in the chart options.
  46. ```javascript
  47. myLineChart.generateLegend();
  48. // => returns HTML string of a legend for this chart
  49. ```
  50. ### External Tooltips
  51. You can enable custom tooltips in the global or chart configuration like so:
  52. ```javascript
  53. var myPieChart = new Chart(ctx).Pie(data, {
  54. customTooltips: function(tooltip) {
  55. // tooltip will be false if tooltip is not visible or should be hidden
  56. if (!tooltip) {
  57. return;
  58. }
  59. // Otherwise, tooltip will be an object with all tooltip properties like:
  60. // tooltip.caretHeight
  61. // tooltip.caretPadding
  62. // tooltip.chart
  63. // tooltip.cornerRadius
  64. // tooltip.fillColor
  65. // tooltip.font...
  66. // tooltip.text
  67. // tooltip.x
  68. // tooltip.y
  69. // etc...
  70. };
  71. });
  72. ```
  73. See files `sample/pie-customTooltips.html` and `sample/line-customTooltips.html` for examples on how to get started.
  74. ### Writing new chart types
  75. Chart.js 1.0 has been rewritten to provide a platform for developers to create their own custom chart types, and be able to share and utilise them through the Chart.js API.
  76. The format is relatively simple, there are a set of utility helper methods under `Chart.helpers`, including things such as looping over collections, requesting animation frames, and easing equations.
  77. On top of this, there are also some simple base classes of Chart elements, these all extend from `Chart.Element`, and include things such as points, bars and scales.
  78. ```javascript
  79. Chart.Type.extend({
  80. // Passing in a name registers this chart in the Chart namespace
  81. name: "Scatter",
  82. // Providing a defaults will also register the deafults in the chart namespace
  83. defaults : {
  84. options: "Here",
  85. available: "at this.options"
  86. },
  87. // Initialize is fired when the chart is initialized - Data is passed in as a parameter
  88. // Config is automatically merged by the core of Chart.js, and is available at this.options
  89. initialize: function(data){
  90. this.chart.ctx // The drawing context for this chart
  91. this.chart.canvas // the canvas node for this chart
  92. },
  93. // Used to draw something on the canvas
  94. draw: function() {
  95. }
  96. });
  97. // Now we can create a new instance of our chart, using the Chart.js API
  98. new Chart(ctx).Scatter(data);
  99. // initialize is now run
  100. ```
  101. ### Extending existing chart types
  102. We can also extend existing chart types, and expose them to the API in the same way. Let's say for example, we might want to run some more code when we initialize every Line chart.
  103. ```javascript
  104. // Notice now we're extending the particular Line chart type, rather than the base class.
  105. Chart.types.Line.extend({
  106. // Passing in a name registers this chart in the Chart namespace in the same way
  107. name: "LineAlt",
  108. initialize: function(data){
  109. console.log('My Line chart extension');
  110. Chart.types.Line.prototype.initialize.apply(this, arguments);
  111. }
  112. });
  113. // Creates a line chart in the same way
  114. new Chart(ctx).LineAlt(data);
  115. // but this logs 'My Line chart extension' in the console.
  116. ```
  117. ### Community extensions
  118. - <a href="https://github.com/Regaddi/Chart.StackedBar.js" target"_blank">Stacked Bar Chart</a> by <a href="https://twitter.com/Regaddi" target="_blank">@Regaddi</a>
  119. - <a href="https://github.com/CAYdenberg/Chart.js" target"_blank">Error bars (bar and line charts)</a> by <a href="https://twitter.com/CAYdenberg" target="_blank">@CAYdenberg</a>
  120. ### Creating custom builds
  121. Chart.js uses <a href="http://gulpjs.com/" target="_blank">gulp</a> to build the library into a single JavaScript file. We can use this same build script with custom parameters in order to build a custom version.
  122. Firstly, we need to ensure development dependencies are installed. With node and npm installed, after cloning the Chart.js repo to a local directory, and navigating to that directory in the command line, we can run the following:
  123. ```bash
  124. npm install
  125. npm install -g gulp
  126. ```
  127. This will install the local development dependencies for Chart.js, along with a CLI for the JavaScript task runner <a href="http://gulpjs.com/" target="_blank">gulp</a>.
  128. Now, we can run the `gulp build` task, and pass in a comma seperated list of types as an argument to build a custom version of Chart.js with only specified chart types.
  129. Here we will create a version of Chart.js with only Line, Radar and Bar charts included:
  130. ```bash
  131. gulp build --types=Line,Radar,Bar
  132. ```
  133. This will output to the `/custom` directory, and write two files, Chart.js, and Chart.min.js with only those chart types included.