| commit | author | age | ||
| 83c3f6 | 1 | # New Charts |
| SP | 2 | |
| 3 | Chart.js 2.0 introduces the concept of controllers for each dataset. Like scales, new controllers can be written as needed. | |
| 4 | ||
| 5 | ```javascript | |
| 6 | Chart.controllers.MyType = Chart.DatasetController.extend({ | |
| 7 | ||
| 8 | }); | |
| 9 | ||
| 10 | ||
| 11 | // Now we can create a new instance of our chart, using the Chart.js API | |
| 12 | new Chart(ctx, { | |
| 13 | // this is the string the constructor was registered at, ie Chart.controllers.MyType | |
| 14 | type: 'MyType', | |
| 15 | data: data, | |
| 16 | options: options | |
| 17 | }); | |
| 18 | ``` | |
| 19 | ||
| 20 | ## Dataset Controller Interface | |
| 21 | ||
| 22 | Dataset controllers must implement the following interface. | |
| 23 | ||
| 24 | ```javascript | |
| 25 | { | |
| 26 | // Create elements for each piece of data in the dataset. Store elements in an array on the dataset as dataset.metaData | |
| 27 | addElements: function() {}, | |
| 28 | ||
| 29 | // Create a single element for the data at the given index and reset its state | |
| 30 | addElementAndReset: function(index) {}, | |
| 31 | ||
| 32 | // Draw the representation of the dataset | |
| 33 | // @param ease : if specified, this number represents how far to transition elements. See the implementation of draw() in any of the provided controllers to see how this should be used | |
| 34 | draw: function(ease) {}, | |
| 35 | ||
| 36 | // Remove hover styling from the given element | |
| 37 | removeHoverStyle: function(element) {}, | |
| 38 | ||
| 39 | // Add hover styling to the given element | |
| 40 | setHoverStyle: function(element) {}, | |
| 41 | ||
| 42 | // Update the elements in response to new data | |
| 43 | // @param reset : if true, put the elements into a reset state so they can animate to their final values | |
| 44 | update: function(reset) {}, | |
| 45 | } | |
| 46 | ``` | |
| 47 | ||
| 48 | The following methods may optionally be overridden by derived dataset controllers | |
| 49 | ```javascript | |
| 50 | { | |
| 51 | // Initializes the controller | |
| 52 | initialize: function(chart, datasetIndex) {}, | |
| 53 | ||
| 54 | // Ensures that the dataset represented by this controller is linked to a scale. Overridden to helpers.noop in the polar area and doughnut controllers as these | |
| 55 | // chart types using a single scale | |
| 56 | linkScales: function() {}, | |
| 57 | ||
| 58 | // Called by the main chart controller when an update is triggered. The default implementation handles the number of data points changing and creating elements appropriately. | |
| 59 | buildOrUpdateElements: function() {} | |
| 60 | } | |
| 61 | ``` | |
| 62 | ||
| 63 | ## Extending Existing Chart Types | |
| 64 | ||
| 65 | Extending or replacing an existing controller type is easy. Simply replace the constructor for one of the built in types with your own. | |
| 66 | ||
| 67 | The built in controller types are: | |
| 68 | * `Chart.controllers.line` | |
| 69 | * `Chart.controllers.bar` | |
| 70 | * `Chart.controllers.radar` | |
| 71 | * `Chart.controllers.doughnut` | |
| 72 | * `Chart.controllers.polarArea` | |
| 73 | * `Chart.controllers.bubble` | |
| 74 | ||
| 75 | For example, to derive a new chart type that extends from a bubble chart, you would do the following. | |
| 76 | ||
| 77 | ```javascript | |
| 78 | // Sets the default config for 'derivedBubble' to be the same as the bubble defaults. | |
| 79 | // We look for the defaults by doing Chart.defaults[chartType] | |
| 80 | // It looks like a bug exists when the defaults don't exist | |
| 81 | Chart.defaults.derivedBubble = Chart.defaults.bubble; | |
| 82 | ||
| 83 | // I think the recommend using Chart.controllers.bubble.extend({ extensions here }); | |
| 84 | var custom = Chart.controllers.bubble.extend({ | |
| 85 | draw: function(ease) { | |
| 86 | // Call super method first | |
| 87 | Chart.controllers.bubble.prototype.draw.call(this, ease); | |
| 88 | ||
| 89 | // Now we can do some custom drawing for this dataset. Here we'll draw a red box around the first point in each dataset | |
| 90 | var meta = this.getMeta(); | |
| 91 | var pt0 = meta.data[0]; | |
| 92 | var radius = pt0._view.radius; | |
| 93 | ||
| 94 | var ctx = this.chart.chart.ctx; | |
| 95 | ctx.save(); | |
| 96 | ctx.strokeStyle = 'red'; | |
| 97 | ctx.lineWidth = 1; | |
| 98 | ctx.strokeRect(pt0._view.x - radius, pt0._view.y - radius, 2 * radius, 2 * radius); | |
| 99 | ctx.restore(); | |
| 100 | } | |
| 101 | }); | |
| 102 | ||
| 103 | // Stores the controller so that the chart initialization routine can look it up with | |
| 104 | // Chart.controllers[type] | |
| 105 | Chart.controllers.derivedBubble = custom; | |
| 106 | ||
| 107 | // Now we can create and use our new chart type | |
| 108 | new Chart(ctx, { | |
| 109 | type: 'derivedBubble', | |
| 110 | data: data, | |
| 111 | options: options, | |
| 112 | }); | |
| 113 | ``` | |
| 114 | ||
| 115 | ### Bar Controller | |
| 116 | The bar controller has a special property that you should be aware of. To correctly calculate the width of a bar, the controller must determine the number of datasets that map to bars. To do this, the bar controller attaches a property `bar` to the dataset during initialization. If you are creating a replacement or updated bar controller, you should do the same. This will ensure that charts with regular bars and your new derived bars will work seamlessly. | |