-
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
1 parent
55dbad3
commit 1422ec2
Showing
3 changed files
with
68 additions
and
27 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import {Chart, registerables} from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' | ||
import Color from "https://colorjs.io/dist/color.js"; | ||
|
||
Chart.register(...registerables); | ||
|
||
|
@@ -21,6 +22,46 @@ const getOrCreateLegendList = (chart, id) => { | |
return listContainer; | ||
}; | ||
|
||
/** | ||
* Returns a hash code from a string | ||
* @param {String} str The string to hash. | ||
* @return {Number} A 32bit integer | ||
* @see http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ | ||
*/ | ||
function hashCode(str) { | ||
let hash = 0; | ||
for (let i = 0, len = str.length; i < len; i++) { | ||
let chr = str.charCodeAt(i); | ||
hash = (hash << 5) - hash + chr; | ||
hash |= 0; // Convert to 32bit integer | ||
} | ||
return hash; | ||
} | ||
|
||
const getColor = (context) => { | ||
|
||
const colors = [ | ||
"#37a2eb", | ||
"#ff6384", | ||
"#ff9e40", | ||
"#9966ff", | ||
"#ffcd56", | ||
"#4dbd74" | ||
] | ||
|
||
let hash = hashCode(context) ** 2 // Make sure its positive | ||
|
||
let color = colors[hash % (colors.length - 1)] | ||
|
||
let colorObj = new Color(color).to("lch"); | ||
|
||
// Manipulate the color based on the hash | ||
colorObj = colorObj.lighten((hash % 19) / 100) | ||
colorObj = colorObj.darken((hash % 23) / 100) | ||
|
||
return colorObj.to("srgb").toString(); | ||
} | ||
|
||
const htmlLegendPlugin = { | ||
id: 'htmlLegend', | ||
afterUpdate(chart, args, options) { | ||
|
@@ -118,14 +159,14 @@ export class PieChart { | |
parent.className = 'row gx-0'; | ||
|
||
this.canvasContainer = document.createElement('div'); | ||
this.canvasContainer.className = "col-md-8 col-12" | ||
this.canvasContainer.className = "col-8" | ||
|
||
this.canvas = document.createElement('canvas'); | ||
this.canvas.id = `canvas-${this.id}`; | ||
|
||
this.legend = document.createElement('div'); | ||
this.legend.id = `legend-${this.id}`; | ||
this.legend.className = "col-md-4 col-12" | ||
this.legend.className = "col-4" | ||
|
||
// Empty the children out of the parent element | ||
while (parent.firstChild) { | ||
|
@@ -145,7 +186,8 @@ export class PieChart { | |
datasets: [{ | ||
label: this.label, | ||
data: this.data['data'], | ||
borderWidth: 1 | ||
borderWidth: 1, | ||
backgroundColor: this.data['labels'].map(getColor), | ||
}] | ||
}, | ||
options: { | ||
|
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
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