Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master sunburst chart adrm #5756

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions demo/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const demoData = {
colNumber: 26,
rowNumber: 174,
rows: {},
cols: {},
cols: { 22: { size: 166 } },
merges: [],
cells: {
B2: "42",
Expand Down Expand Up @@ -418,9 +418,78 @@ export const demoData = {
W30: "=W29 - RANDBETWEEN(10, 40)",
W31: "=W30 - RANDBETWEEN(10, 40)",
W32: "=W31 - RANDBETWEEN(10, 40)",
V1: "Movie Genre",
V2: "Action",
V3: "Action",
V4: "Action",
V5: "Action",
V6: "Action",
V7: "Animation",
V8: "Animation",
V9: "Animation",
V10: "Animation",
V11: "Animation",
V12: "Drama",
V13: "Drama",
V14: "Drama",
V15: "Drama",
V16: "Drama",
V17: "Action",
V18: "Animation",
V19: "Drama",
V20: "Action",
V21: "Animation",
V22: "Misc",
V23: "Misc",
W1: "Movie Name",
W2: "Avengers: Endgame",
W3: "The Dark Knight",
W4: "Jurassic World",
W5: "Fast & Furious 7",
W6: "Spider-Man: No Way Home",
W7: "Frozen II",
W8: "Minions",
W9: "Toy Story 4",
W10: "The Lion King (2019)",
W11: "Despicable Me 3",
W12: "Titanic",
W13: "Joker",
W14: "Forrest Gump",
W15: "Bohemian Rhapsody",
W16: "The Revenant",
W17: "Black Panther",
W18: "Shrek 2",
W19: "Interstellar",
W20: "Iron Man 3",
W21: "Coco",
X1: "Revenue",
X2: "2798000000",
X3: "1006000000",
X4: "1671000000",
X5: "1516000000",
X6: "1921000000",
X7: "1450000000",
X8: "1159000000",
X9: "1073000000",
X10: "1657000000",
X11: "1035000000",
X12: "2201000000",
X13: "1074000000",
X14: "678200000",
X15: "910000000",
X16: "533000000",
X17: "1347000000",
X18: "935300000",
X19: "701800000",
X20: "1215000000",
X21: "807800000",
X22: "3215000000",
X23: "1212000000",
},
styles: {},
formats: {},
formats: {
"X1:X23": 7,
},
borders: {},
conditionalFormats: [],
dataValidationRules: [],
Expand Down Expand Up @@ -695,6 +764,29 @@ export const demoData = {
region: "world",
},
},
{
id: "11",
x: 100,
y: 1800,
width: 400,
height: 400,
tag: "chart",
data: {
type: "sunburst",
dataSetsHaveTitle: true,
dataSets: [
{
dataRange: "V1:V23",
},
{
dataRange: "W1:W23",
},
],
legendPosition: "top",
labelRange: "X1:X23",
title: { text: "Movie Revenue by Genre" },
},
},
{
id: "12",
x: 950,
Expand Down Expand Up @@ -730,6 +822,21 @@ export const demoData = {
styleId: "TableStyleMedium7",
},
},
{
range: "V1:X23",
type: "static",
config: {
hasFilters: false,
totalRow: false,
firstColumn: true,
lastColumn: false,
numberOfHeaders: 1,
bandedRows: true,
bandedColumns: false,
automaticAutofill: true,
styleId: "TableStyleMedium3",
},
},
{
range: "V27:W32",
type: "static",
Expand Down Expand Up @@ -3282,6 +3389,7 @@ export const demoData = {
4: "hh:mm:ss a",
5: "d/m/yyyy",
6: "[$$]#,##0.00",
7: '$#,##0,,"K"',
},
borders: {
1: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ActiveDataPoint, ChartType, Plugin } from "chart.js";
import { lightenColor } from "../../../../helpers";
import { GHOST_SUNBURST_VALUE } from "../../../../helpers/figures/charts/runtime/chartjs_dataset";
import { SunburstChartRawData } from "../../../../types/chart";

export interface ChartSunburstHoverPluginOptions {
enabled: boolean;
}

declare module "chart.js" {
interface PluginOptionsByType<TType extends ChartType> {
sunburstHoverPlugin?: ChartSunburstHoverPluginOptions;
}
}

/**
* When a chart element is hovered (active), this plugin also activates all of its child elements and
* lightens the color of the other elements.
*/
export const sunburstHoverPlugin: Plugin = {
id: "sunburstHoverPlugin",
afterEvent(chart, args, options: ChartSunburstHoverPluginOptions) {
if (!options.enabled) {
return;
}
const chartActiveElements = chart.getActiveElements();
let activeDataPoints: ActiveDataPoint[] = chartActiveElements.map((el) => ({
datasetIndex: el.datasetIndex,
index: el.index,
}));

for (const activeEl of chartActiveElements) {
const activeDataset = chart.data.datasets[activeEl.datasetIndex];
const activeData = activeDataset.data[activeEl.index] as unknown as SunburstChartRawData;

for (let datasetIndex = 0; datasetIndex < chart.data.datasets.length; datasetIndex++) {
const dataset = chart.data.datasets[datasetIndex];
for (let index = 0; index < dataset.data.length; index++) {
const data = dataset.data[index] as unknown as SunburstChartRawData;
if (isChildGroup(activeData.groups, data.groups)) {
activeDataPoints.push({ datasetIndex, index });
}
}
}
}

activeDataPoints = activeDataPoints.filter((point) => {
const { datasetIndex, index } = point;
const data = chart.data.datasets[datasetIndex].data[index] as unknown as SunburstChartRawData;
return data.label !== GHOST_SUNBURST_VALUE;
});

chart.setActiveElements(activeDataPoints);

for (const metaSet of chart.getSortedVisibleDatasetMetas()) {
for (const arcElement of metaSet.data) {
const context = arcElement["$context"];
const { datasetIndex, index, dataset, raw } = context;
if (raw.label === GHOST_SUNBURST_VALUE) {
continue;
}

const originalBackgroundColor =
typeof dataset.backgroundColor === "function"
? dataset.backgroundColor(context)
: dataset.backgroundColor;
if (
activeDataPoints.length &&
!activeDataPoints.some((el) => el.datasetIndex === datasetIndex && el.index === index)
) {
arcElement.options.backgroundColor = lightenColor(originalBackgroundColor, 0.5);
} else {
arcElement.options.backgroundColor = originalBackgroundColor;
}
}
}
},
};

function isChildGroup(parentGroup: string[], childGroup: string[]) {
return (
childGroup.length > parentGroup.length &&
parentGroup.every((group, i) => group === childGroup[i])
);
}
116 changes: 116 additions & 0 deletions src/components/figures/chart/chartJs/chartjs_sunburst_labels_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { ChartType, Plugin } from "chart.js";
import {
getDefaultContextFont,
isDefined,
relativeLuminance,
sliceTextToFitWidth,
} from "../../../../helpers";
import { GHOST_SUNBURST_VALUE } from "../../../../helpers/figures/charts/runtime/chartjs_dataset";
import { Style } from "../../../../types";
import { SunburstChartRawData } from "../../../../types/chart";

export interface ChartSunburstLabelsPluginOptions {
showValues: boolean;
showLabels: boolean;
style: Style;
callback: (value: number, axisId?: string) => string;
}

declare module "chart.js" {
interface PluginOptionsByType<TType extends ChartType> {
sunburstLabelsPlugin?: ChartSunburstLabelsPluginOptions;
}
}

const Y_PADDING = 3;

export const sunburstLabelsPlugin: Plugin = {
id: "sunburstLabelsPlugin",
afterDatasetsDraw(chart: any, args, options: ChartSunburstLabelsPluginOptions) {
if ((!options.showValues && !options.showLabels) || chart.config.type !== "doughnut") {
return;
}
const ctx = chart.ctx as CanvasRenderingContext2D;
drawSunburstChartValues(chart, options, ctx);
},
};

function drawSunburstChartValues(
chart: any,
options: ChartSunburstLabelsPluginOptions,
ctx: CanvasRenderingContext2D
) {
const style = options.style;
const fontSize = style.fontSize || 13;
const lineHeight = fontSize + Y_PADDING;

for (const dataset of chart._metasets) {
for (let i = 0; i < dataset._dataset.data.length; i++) {
const rawData = dataset._dataset.data[i] as SunburstChartRawData;
if (rawData.label === GHOST_SUNBURST_VALUE) {
continue;
}
const valuesToDisplay = [
options.showLabels ? rawData.label : undefined,
options.showValues ? options.callback(rawData.value, "y") : undefined,
].filter(isDefined);

const arc = dataset.data[i];
let { startAngle, endAngle, innerRadius, outerRadius, circumference } = arc;
// Same computations as in ChartJs ArcElement's draw method. Don't ask me why they divide by 4.
const offset = arc.options.offset / 4;
const fix = 1 - Math.sin(Math.min(Math.PI, circumference || 0));
const radiusOffset = offset * fix;
innerRadius += radiusOffset;
outerRadius += radiusOffset;

const midAngle = (startAngle + endAngle) / 2;
const midRadius = (innerRadius + outerRadius) / 2;

const availableWidth = (outerRadius - innerRadius) * 0.9;
const angle = endAngle - startAngle;
const availableHeight =
angle >= Math.PI ? outerRadius : Math.sin(angle / 2) * innerRadius * 2;
if (availableHeight < valuesToDisplay.length * lineHeight) {
continue;
}

ctx.save();

const centerOffset = { x: Math.cos(midAngle) * offset, y: Math.sin(midAngle) * offset };
const centerX = chart.chartArea.left + chart.chartArea.width / 2 + centerOffset.x;
const centerY = chart.chartArea.top + chart.chartArea.height / 2 + centerOffset.y;
ctx.translate(centerX, centerY);

let x: number;
if (midAngle > Math.PI / 2) {
ctx.rotate(midAngle - Math.PI);
x = -midRadius;
} else {
x = midRadius;
ctx.rotate(midAngle);
}

const backgroundColor = arc.options.backgroundColor;
const defaultColor = relativeLuminance(backgroundColor) > 0.7 ? "#666666" : "#FFFFFF";
ctx.fillStyle = style.textColor || defaultColor;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = getDefaultContextFont(fontSize, style.bold, style.italic);

const y = -((valuesToDisplay.length - 1) * lineHeight) / 2;
for (let j = 0; j < valuesToDisplay.length; j++) {
const fittedText = sliceTextToFitWidth(
ctx,
availableWidth,
valuesToDisplay[j],
style,
"px"
);
ctx.fillText(fittedText, x, y + j * lineHeight);
}

ctx.restore();
}
}
}
Loading