-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "1.0.0-alpha2", | ||
"main": "dist/chartjs-chart-matrix.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/*! | ||
* chartjs-chart-matrix v1.0.0-alpha2 | ||
* 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: { | ||
width: 20, | ||
height: 20 | ||
} | ||
} | ||
}; | ||
|
||
})); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.