-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds an alternative display mode for treemap. In the original mode, leaf nodes are displayed inside the rectangle of their parent, that is itself inside the rectangle of its parent, and so on. With the new mode "headerBoxes" the parent rectangles do not contain their children, but are displayed as headers above them. The new mode is activated by setting the displayMode option to "headerBoxes" in the dataset.
- Loading branch information
1 parent
058f36e
commit 83e1698
Showing
17 changed files
with
375 additions
and
12 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
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,91 @@ | ||
# Display Mode | ||
|
||
```js chart-editor | ||
// <block:setup:3> | ||
let DISPLAY_MODE = 'containerBoxes'; | ||
|
||
function capitalizeFirstLetter(string) { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
// </block:setup> | ||
|
||
// <block:options:2> | ||
const options = { | ||
plugins: { | ||
title: { | ||
display: true, | ||
text: 'US area by division / state' | ||
}, | ||
legend: { | ||
display: false | ||
}, | ||
tooltip: { | ||
callbacks: { | ||
title(items) { | ||
return capitalizeFirstLetter(items[0].dataset.key); | ||
}, | ||
label(item) { | ||
const dataItem = item.raw; | ||
const obj = dataItem._data; | ||
const label = obj.state || obj.division || obj.region; | ||
return label + ': ' + dataItem.v; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
// </block:options> | ||
|
||
// <block:config:0> | ||
const config = { | ||
type: 'treemap', | ||
data: { | ||
datasets: [{ | ||
tree: Data.statsByState, | ||
key: 'area', | ||
groups: ['division', 'state'], | ||
spacing: 2, | ||
borderWidth: 1, | ||
borderColor: 'rgba(200,200,200,1)', | ||
backgroundColor: (ctx) => { | ||
if (ctx.type !== 'data') { | ||
return 'transparent'; | ||
} | ||
if (DISPLAY_MODE === 'containerBoxes') { | ||
return 'rgba(220,230,220,0.3)'; | ||
} | ||
return ctx.raw.l ? 'rgb(220,230,220)' : 'lightgray'; | ||
}, | ||
displayMode: DISPLAY_MODE, | ||
captions: { | ||
padding: 6, | ||
}, | ||
}] | ||
}, | ||
options: options | ||
}; | ||
|
||
// </block:config> | ||
function toggle(chart, mode) { | ||
const dataset = {...config.data.datasets[0], displayMode: mode}; | ||
DISPLAY_MODE = mode; | ||
chart.data.datasets = [dataset]; | ||
chart.update(); | ||
} | ||
|
||
const actions = [ | ||
{ | ||
name: 'Container Boxes', | ||
handler: (chart) => toggle(chart, 'containerBoxes') | ||
}, | ||
{ | ||
name: 'Header Boxes', | ||
handler: (chart) => toggle(chart, 'headerBoxes') | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
actions, | ||
config, | ||
}; | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const longCategory1 = 'This is a long category name that should be truncated'; | ||
const longCategory2 = 'This is another long category name that should be truncated'; | ||
const longSubcategory1 = 'This is a long subcategory name that should be truncated'; | ||
const longSubcategory2 = 'This is another long subcategory name that should be truncated'; | ||
|
||
const data = [ | ||
{category: longCategory1, subcategory: longSubcategory1, value: 1}, | ||
{category: longCategory1, subcategory: longSubcategory1, value: 5}, | ||
{category: longCategory1, subcategory: longSubcategory1, value: 3}, | ||
{category: longCategory1, subcategory: longSubcategory2, value: 2}, | ||
{category: longCategory1, subcategory: longSubcategory2, value: 1}, | ||
{category: longCategory1, subcategory: longSubcategory2, value: 8}, | ||
{category: longCategory2, subcategory: longSubcategory1, value: 4}, | ||
{category: longCategory2, subcategory: longSubcategory1, value: 5}, | ||
{category: longCategory2, subcategory: longSubcategory2, value: 4}, | ||
{category: longCategory2, subcategory: longSubcategory2, value: 1}, | ||
]; | ||
|
||
export default { | ||
tolerance: 0.0050, | ||
config: { | ||
type: 'treemap', | ||
data: { | ||
datasets: [{ | ||
tree: data, | ||
key: 'value', | ||
groups: ['category', 'subcategory', 'value'], | ||
backgroundColor: 'lightGreen', | ||
captions: { | ||
display: true, | ||
align: 'center', | ||
padding: 10, | ||
}, | ||
}] | ||
}, | ||
options: { | ||
events: [] | ||
} | ||
}, | ||
options: { | ||
spriteText: true, | ||
canvas: { | ||
height: 256, | ||
width: 512 | ||
} | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,35 @@ | ||
const arrayN = (n) => Array.from({length: n}).map((_, i) => i); | ||
|
||
const groups = arrayN(10); | ||
const tree = groups.reduce((acc, grp) => [ | ||
...acc, | ||
...arrayN(grp * 10).map(i => ({grp: `group: ${grp}`, sub: `sub: ${i}`, value: (i % 10) * 10})) | ||
], []); | ||
|
||
export default { | ||
config: { | ||
type: 'treemap', | ||
data: { | ||
datasets: [{ | ||
tree, | ||
backgroundColor: (ctx) => ctx.raw.l ? 'dimgray' : 'silver', | ||
borderColor: (ctx) => ctx.raw.l ? 'white' : 'black', | ||
borderWidth: 0, | ||
spacing: 1, | ||
key: 'value', | ||
groups: ['grp', 'sub'], | ||
displayMode: 'headerBoxes', | ||
}] | ||
}, | ||
options: { | ||
events: [] | ||
} | ||
}, | ||
options: { | ||
spriteText: true, | ||
canvas: { | ||
height: 300, | ||
width: 800 | ||
} | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.