From: Evert Timberg Date: Sun, 28 Feb 2021 21:02:44 +0000 (-0500) Subject: Update chart extension docs + add samples (#8540) X-Git-Tag: v3.0.0-beta.13~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c5dcf5a20d0f86015491766b3c581e182c878d16;p=thirdparty%2FChart.js.git Update chart extension docs + add samples (#8540) * Update docs on chart extensions * Working sample for derived chart types * Add derived axis example * Remove duplicated line --- diff --git a/docs/docs/developers/charts.md b/docs/docs/developers/charts.md index 652cd41f5..ef326c09f 100644 --- a/docs/docs/developers/charts.md +++ b/docs/docs/developers/charts.md @@ -26,17 +26,21 @@ Dataset controllers must implement the following interface. ```javascript { - // Create elements for each piece of data in the dataset. Store elements in an array on the dataset as dataset.metaData - addElements: function() {}, - - // Draw the representation of the dataset - draw: function() {}, - - // Remove hover styling from the given element - removeHoverStyle: function(element, datasetIndex, index) {}, + // Defaults for charts of this type + defaults: { + // If set to `false` or `null`, no dataset level element is created. + // If set to a string, this is the type of element to create for the dataset. + // For example, a line create needs to create a line element so this is the string 'line' + datasetElementType: string | null | false, + + // If set to `false` or `null`, no elements are created for each data value. + // If set to a string, this is the type of element to create for each data value. + // For example, a line create needs to create a point element so this is the string 'point' + dataElementType: string | null | false, + } - // Add hover styling to the given element - setHoverStyle: function(element, datasetIndex, index) {}, + // ID of the controller + id: string; // Update the elements in response to new data // @param mode : update mode, core calls this method using any of `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'` or `undefined` @@ -48,6 +52,10 @@ The following methods may optionally be overridden by derived dataset controller ```javascript { + // Draw the representation of the dataset. The base implementation works in most cases, and an example of a derived version + // can be found in the line controller + draw: function() {}, + // Initializes the controller initialize: function() {}, @@ -55,8 +63,9 @@ The following methods may optionally be overridden by derived dataset controller // chart types using a single scale linkScales: function() {}, - // 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. - buildOrUpdateElements: function() {} + // Parse the data into the controller meta data. The default implementation will work for cartesian parsing, but an example of an overridden + // version can be found in the doughnut controller + parse: function(start, count) {}, } ``` @@ -83,19 +92,21 @@ For example, to derive a new chart type that extends from a bubble chart, you wo import {BubbleController} from 'chart.js'; class Custom extends BubbleController { draw() { - // Call super method first + // Call bubble controller method to draw all the points super.draw(arguments); // Now we can do some custom drawing for this dataset. Here we'll draw a red box around the first point in each dataset - var meta = this.getMeta(); - var pt0 = meta.data[0]; - var radius = pt0.radius; + const meta = this.getMeta(); + const pt0 = meta.data[0]; + + const {x, y} = pt0.getProps(['x', 'y']); + const {radius} = pt0.options; - var ctx = this.chart.chart.ctx; + const ctx = this.chart.ctx; ctx.save(); ctx.strokeStyle = 'red'; ctx.lineWidth = 1; - ctx.strokeRect(pt0.x - radius, pt0.y - radius, 2 * radius, 2 * radius); + ctx.strokeRect(x - radius, y - radius, 2 * radius, 2 * radius); ctx.restore(); } }); diff --git a/samples/advanced/derived-axis-type.html b/samples/advanced/derived-axis-type.html new file mode 100644 index 000000000..e53a2be25 --- /dev/null +++ b/samples/advanced/derived-axis-type.html @@ -0,0 +1,136 @@ + + + + + Logarithmic Line Chart + + + + + + +
+ +
+ + + + diff --git a/samples/advanced/derived-chart-type.html b/samples/advanced/derived-chart-type.html new file mode 100644 index 000000000..3bb7a1dbb --- /dev/null +++ b/samples/advanced/derived-chart-type.html @@ -0,0 +1,106 @@ + + + + + Derived Chart Type + + + + + + +
+ +
+ + + + diff --git a/samples/samples.js b/samples/samples.js index f0062a659..4f3671dd8 100644 --- a/samples/samples.js +++ b/samples/samples.js @@ -268,6 +268,12 @@ }, { title: 'Programmatic Event Triggers', path: 'advanced/programmatic-events.html' + }, { + title: 'Derived Chart Type', + path: 'advanced/derived-chart-type.html' + }, { + title: 'Derived Axis Type', + path: 'advanced/derived-axis-type.html' }] }];