datalovelace/src/Models/Chart/ChartjsDataset.js
2020-08-06 19:05:42 -05:00

46 lines
1.1 KiB
JavaScript

import Chart from './Chart.js'
import { GroupByNodule } from 'lovelacejs'
class ChartJsDataset extends Chart {
get doughnut () {
const groupByNodule = new GroupByNodule({
id: this.id,
label: `${this.label} groupedBy ${this.groupByValue}`,
tables: [this.table],
groupByValue: this.groupByValue
}).export()
const labels = Object.keys(groupByNodule)
let groupByCounts = []
for (let key in groupByNodule) {
groupByCounts.push(groupByNodule[key].length)
}
return {
labels: labels,
datasets: [{
label: `${this.label} groupedBy ${this.groupByValue}`,
data: groupByCounts,
backgroundColor: this._getbackgroundColors()
}],
}
}
_generateRandomRGBNumber () {
const max = 255
const min = 0
return Math.round(Math.random() * (max - min) - min)
}
_getbackgroundColors () {
const labels = this.labels
const backgroundColors = labels.map(l => {
return `rgb(${this._generateRandomRGBNumber()}, ${this._generateRandomRGBNumber()}, ${this._generateRandomRGBNumber()})`
})
return backgroundColors
}
}
export default ChartJsDataset