diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..84fdc01 --- /dev/null +++ b/bower.json @@ -0,0 +1,8 @@ +{ + "name": "chartjs-chart-matrix", + "description": "Chart.js module for creating matrix charts", + "homepage": "https://github.com/kurkle/chartjs-chart-matrix#readme", + "license": "MIT", + "version": "0.1.3", + "main": "dist/chartjs-chart-matrix.js" +} \ No newline at end of file diff --git a/dist/chartjs-chart-matrix.js b/dist/chartjs-chart-matrix.js new file mode 100644 index 0000000..e7f61f2 --- /dev/null +++ b/dist/chartjs-chart-matrix.js @@ -0,0 +1,149 @@ +/*! + * chartjs-chart-matrix v0.1.3 + * https://github.com/kurkle/chartjs-chart-matrix#readme + * (c) 2019 Jukka Kurkela + * Released under the MIT license + */ +(function (global, factory) { +typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js')) : +typeof define === 'function' && define.amd ? define(['chart.js'], factory) : +(global = global || self, factory(global.Chart)); +}(this, function (Chart) { 'use strict'; + +Chart = Chart && Chart.hasOwnProperty('default') ? Chart['default'] : Chart; + +var resolve = Chart.helpers.options.resolve; + +var Controller = Chart.DatasetController.extend({ + + dataElementType: Chart.elements.Rectangle, + + update: function(reset) { + var me = this; + var meta = me.getMeta(); + var data = meta.data || []; + var i, ilen; + + me._xScale = me.getScaleForId(meta.xAxisID); + me._yScale = me.getScaleForId(meta.yAxisID); + + for (i = 0, ilen = data.length; i < ilen; ++i) { + me.updateElement(data[i], i, reset); + } + }, + + updateElement: function(item, index, reset) { + var me = this; + var dataset = me.getDataset(); + var datasetIndex = me.index; + var value = dataset.data[index]; + var xScale = me._xScale; + var yScale = me._yScale; + var options = me._resolveElementOptions(item, index); + var x = reset ? xScale.getBasePixel() : xScale.getPixelForValue(value, index, datasetIndex); + var y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(value, index, datasetIndex); + var h = options.height; + var w = options.width; + var halfH = h / 2; + + item._xScale = xScale; + item._yScale = yScale; + item._options = options; + item._datasetIndex = datasetIndex; + item._index = index; + + item._model = { + x: x, + base: y - halfH, + y: y + halfH, + width: w, + height: h, + backgroundColor: options.backgroundColor, + borderColor: options.borderColor, + borderSkipped: options.borderSkipped, + borderWidth: options.borderWidth + }; + + item.pivot(); + }, + + draw: function() { + var me = this; + var data = me.getMeta().data || []; + var i, ilen; + + for (i = 0, ilen = data.length; i < ilen; ++i) { + data[i].draw(); + } + }, + + /** + * @private + */ + _resolveElementOptions: function(rectangle, index) { + var me = this; + var chart = me.chart; + var datasets = chart.data.datasets; + var dataset = datasets[me.index]; + var options = chart.options.elements.rectangle; + var values = {}; + var i, ilen, key; + + // Scriptable options + var context = { + chart: chart, + dataIndex: index, + dataset: dataset, + datasetIndex: me.index + }; + + var keys = [ + 'backgroundColor', + 'borderColor', + 'borderSkipped', + 'borderWidth', + 'width', + 'height' + ]; + + for (i = 0, ilen = keys.length; i < ilen; ++i) { + key = keys[i]; + values[key] = resolve([ + dataset[key], + options[key] + ], context, index); + } + + return values; + } + +}); + +Chart.controllers.matrix = Controller; +Chart.defaults.matrix = { + hover: { + mode: 'nearest', + intersect: true + }, + tooltips: { + mode: 'nearest', + intersect: true + }, + scales: { + xAxes: [{ + type: 'linear' + }], + yAxes: [{ + type: 'linear' + }] + }, + elements: { + rectangle: { + borderSkipped: false, + width: 20, + height: 20 + } + } +}; + +})); diff --git a/dist/chartjs-chart-matrix.min.js b/dist/chartjs-chart-matrix.min.js new file mode 100644 index 0000000..58b486e --- /dev/null +++ b/dist/chartjs-chart-matrix.min.js @@ -0,0 +1,7 @@ +/*! + * chartjs-chart-matrix v0.1.3 + * https://github.com/kurkle/chartjs-chart-matrix#readme + * (c) 2019 Jukka Kurkela + * Released under the MIT license + */ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("chart.js")):"function"==typeof define&&define.amd?define(["chart.js"],t):t((e=e||self).Chart)}(this,function(e){"use strict";var t=(e=e&&e.hasOwnProperty("default")?e.default:e).helpers.options.resolve,r=e.DatasetController.extend({dataElementType:e.elements.Rectangle,update:function(e){var t,r,a=this,o=a.getMeta(),d=o.data||[];for(a._xScale=a.getScaleForId(o.xAxisID),a._yScale=a.getScaleForId(o.yAxisID),t=0,r=d.length;t