2020-12-11 13:13:23 +02:00
|
|
|
import colors from "./colors";
|
2022-03-02 20:06:07 +05:30
|
|
|
import {
|
|
|
|
DEFAULT_FONT_FAMILY,
|
|
|
|
DEFAULT_FONT_SIZE,
|
|
|
|
ENV,
|
|
|
|
VERTICAL_ALIGN,
|
|
|
|
} from "./constants";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { newElement, newLinearElement, newTextElement } from "./element";
|
|
|
|
import { NonDeletedExcalidrawElement } from "./element/types";
|
2020-12-11 13:13:23 +02:00
|
|
|
import { randomId } from "./random";
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
export type ChartElements = readonly NonDeletedExcalidrawElement[];
|
|
|
|
|
2020-12-11 13:13:23 +02:00
|
|
|
const BAR_WIDTH = 32;
|
|
|
|
const BAR_GAP = 12;
|
|
|
|
const BAR_HEIGHT = 256;
|
2020-12-27 18:26:30 +02:00
|
|
|
const GRID_OPACITY = 50;
|
|
|
|
|
2020-09-04 14:58:32 +02:00
|
|
|
export interface Spreadsheet {
|
2020-12-11 13:13:23 +02:00
|
|
|
title: string | null;
|
2020-06-06 13:09:04 -07:00
|
|
|
labels: string[] | null;
|
|
|
|
values: number[];
|
|
|
|
}
|
|
|
|
|
2020-09-04 14:58:32 +02:00
|
|
|
export const NOT_SPREADSHEET = "NOT_SPREADSHEET";
|
|
|
|
export const VALID_SPREADSHEET = "VALID_SPREADSHEET";
|
|
|
|
|
2020-06-06 13:09:04 -07:00
|
|
|
type ParseSpreadsheetResult =
|
2020-12-20 04:08:22 -08:00
|
|
|
| { type: typeof NOT_SPREADSHEET; reason: string }
|
2020-12-11 13:13:23 +02:00
|
|
|
| { type: typeof VALID_SPREADSHEET; spreadsheet: Spreadsheet };
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
const tryParseNumber = (s: string): number | null => {
|
2020-12-20 04:08:22 -08:00
|
|
|
const match = /^[$€£¥₩]?([0-9,]+(\.[0-9]+)?)$/.exec(s);
|
2020-06-06 13:09:04 -07:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-20 04:08:22 -08:00
|
|
|
return parseFloat(match[1].replace(/,/g, ""));
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-11 13:13:23 +02:00
|
|
|
const isNumericColumn = (lines: string[][], columnIndex: number) =>
|
|
|
|
lines.slice(1).every((line) => tryParseNumber(line[columnIndex]) !== null);
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
const tryParseCells = (cells: string[][]): ParseSpreadsheetResult => {
|
2020-06-06 13:09:04 -07:00
|
|
|
const numCols = cells[0].length;
|
|
|
|
|
|
|
|
if (numCols > 2) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "More than 2 columns" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (numCols === 1) {
|
|
|
|
if (!isNumericColumn(cells, 0)) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "Value is not numeric" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const hasHeader = tryParseNumber(cells[0][0]) === null;
|
|
|
|
const values = (hasHeader ? cells.slice(1) : cells).map((line) =>
|
|
|
|
tryParseNumber(line[0]),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (values.length < 2) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "Less than two rows" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-09-04 14:58:32 +02:00
|
|
|
type: VALID_SPREADSHEET,
|
2020-06-06 13:09:04 -07:00
|
|
|
spreadsheet: {
|
2020-12-11 13:13:23 +02:00
|
|
|
title: hasHeader ? cells[0][0] : null,
|
2020-06-06 13:09:04 -07:00
|
|
|
labels: null,
|
|
|
|
values: values as number[],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const valueColumnIndex = isNumericColumn(cells, 0) ? 0 : 1;
|
|
|
|
|
|
|
|
if (!isNumericColumn(cells, valueColumnIndex)) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "Value is not numeric" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const labelColumnIndex = (valueColumnIndex + 1) % 2;
|
|
|
|
const hasHeader = tryParseNumber(cells[0][valueColumnIndex]) === null;
|
|
|
|
const rows = hasHeader ? cells.slice(1) : cells;
|
|
|
|
|
|
|
|
if (rows.length < 2) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "Less than 2 rows" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-09-04 14:58:32 +02:00
|
|
|
type: VALID_SPREADSHEET,
|
2020-06-06 13:09:04 -07:00
|
|
|
spreadsheet: {
|
2020-12-11 13:13:23 +02:00
|
|
|
title: hasHeader ? cells[0][valueColumnIndex] : null,
|
2020-06-06 13:09:04 -07:00
|
|
|
labels: rows.map((row) => row[labelColumnIndex]),
|
|
|
|
values: rows.map((row) => tryParseNumber(row[valueColumnIndex])!),
|
|
|
|
},
|
|
|
|
};
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
const transposeCells = (cells: string[][]) => {
|
2020-06-06 13:09:04 -07:00
|
|
|
const nextCells: string[][] = [];
|
|
|
|
for (let col = 0; col < cells[0].length; col++) {
|
|
|
|
const nextCellRow: string[] = [];
|
|
|
|
for (let row = 0; row < cells.length; row++) {
|
|
|
|
nextCellRow.push(cells[row][col]);
|
|
|
|
}
|
|
|
|
nextCells.push(nextCellRow);
|
|
|
|
}
|
|
|
|
return nextCells;
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
export const tryParseSpreadsheet = (text: string): ParseSpreadsheetResult => {
|
2022-03-02 01:07:12 -05:00
|
|
|
// Copy/paste from excel, spreadsheets, tsv, csv.
|
2020-12-11 13:13:23 +02:00
|
|
|
// For now we only accept 2 columns with an optional header
|
|
|
|
|
2020-12-20 04:08:22 -08:00
|
|
|
// Check for tab separated values
|
2020-12-11 13:13:23 +02:00
|
|
|
let lines = text
|
2020-06-06 13:09:04 -07:00
|
|
|
.trim()
|
|
|
|
.split("\n")
|
|
|
|
.map((line) => line.trim().split("\t"));
|
|
|
|
|
2020-12-20 04:08:22 -08:00
|
|
|
// Check for comma separated files
|
2020-12-11 13:13:23 +02:00
|
|
|
if (lines.length && lines[0].length !== 2) {
|
|
|
|
lines = text
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
|
|
|
.map((line) => line.trim().split(","));
|
|
|
|
}
|
|
|
|
|
2020-06-06 13:09:04 -07:00
|
|
|
if (lines.length === 0) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return { type: NOT_SPREADSHEET, reason: "No values" };
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const numColsFirstLine = lines[0].length;
|
2020-12-11 13:13:23 +02:00
|
|
|
const isSpreadsheet = lines.every((line) => line.length === numColsFirstLine);
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-11 13:13:23 +02:00
|
|
|
if (!isSpreadsheet) {
|
2020-12-20 04:08:22 -08:00
|
|
|
return {
|
|
|
|
type: NOT_SPREADSHEET,
|
|
|
|
reason: "All rows don't have same number of columns",
|
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = tryParseCells(lines);
|
2020-09-04 14:58:32 +02:00
|
|
|
if (result.type !== VALID_SPREADSHEET) {
|
2020-06-06 13:09:04 -07:00
|
|
|
const transposedResults = tryParseCells(transposeCells(lines));
|
2020-09-04 14:58:32 +02:00
|
|
|
if (transposedResults.type === VALID_SPREADSHEET) {
|
2020-06-06 13:09:04 -07:00
|
|
|
return transposedResults;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const bgColors = colors.elementBackground.slice(
|
|
|
|
2,
|
|
|
|
colors.elementBackground.length,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Put all the common properties here so when the whole chart is selected
|
|
|
|
// the properties dialog shows the correct selected values
|
|
|
|
const commonProps = {
|
|
|
|
fillStyle: "hachure",
|
|
|
|
fontFamily: DEFAULT_FONT_FAMILY,
|
|
|
|
fontSize: DEFAULT_FONT_SIZE,
|
|
|
|
opacity: 100,
|
|
|
|
roughness: 1,
|
|
|
|
strokeColor: colors.elementStroke[0],
|
|
|
|
strokeSharpness: "sharp",
|
|
|
|
strokeStyle: "solid",
|
|
|
|
strokeWidth: 1,
|
2022-03-02 20:06:07 +05:30
|
|
|
verticalAlign: VERTICAL_ALIGN.MIDDLE,
|
2022-04-07 12:43:29 +01:00
|
|
|
locked: false,
|
2020-12-27 18:26:30 +02:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
const getChartDimentions = (spreadsheet: Spreadsheet) => {
|
|
|
|
const chartWidth =
|
|
|
|
(BAR_WIDTH + BAR_GAP) * spreadsheet.values.length + BAR_GAP;
|
|
|
|
const chartHeight = BAR_HEIGHT + BAR_GAP * 2;
|
|
|
|
return { chartWidth, chartHeight };
|
|
|
|
};
|
|
|
|
|
|
|
|
const chartXLabels = (
|
2020-06-06 13:09:04 -07:00
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-12-27 18:26:30 +02:00
|
|
|
groupId: string,
|
|
|
|
backgroundColor: string,
|
|
|
|
): ChartElements => {
|
|
|
|
return (
|
|
|
|
spreadsheet.labels?.map((label, index) => {
|
|
|
|
return newTextElement({
|
|
|
|
groupIds: [groupId],
|
|
|
|
backgroundColor,
|
|
|
|
...commonProps,
|
|
|
|
text: label.length > 8 ? `${label.slice(0, 5)}...` : label,
|
|
|
|
x: x + index * (BAR_WIDTH + BAR_GAP) + BAR_GAP * 2,
|
|
|
|
y: y + BAR_GAP / 2,
|
|
|
|
width: BAR_WIDTH,
|
|
|
|
angle: 5.87,
|
|
|
|
fontSize: 16,
|
|
|
|
textAlign: "center",
|
|
|
|
verticalAlign: "top",
|
|
|
|
});
|
|
|
|
}) || []
|
|
|
|
);
|
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const chartYLabels = (
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
groupId: string,
|
|
|
|
backgroundColor: string,
|
|
|
|
): ChartElements => {
|
2020-06-06 13:09:04 -07:00
|
|
|
const minYLabel = newTextElement({
|
2020-12-27 18:26:30 +02:00
|
|
|
groupIds: [groupId],
|
|
|
|
backgroundColor,
|
2020-12-11 13:13:23 +02:00
|
|
|
...commonProps,
|
|
|
|
x: x - BAR_GAP,
|
|
|
|
y: y - BAR_GAP,
|
|
|
|
text: "0",
|
|
|
|
textAlign: "right",
|
2020-06-06 13:09:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const maxYLabel = newTextElement({
|
2020-12-27 18:26:30 +02:00
|
|
|
groupIds: [groupId],
|
|
|
|
backgroundColor,
|
2020-12-11 13:13:23 +02:00
|
|
|
...commonProps,
|
|
|
|
x: x - BAR_GAP,
|
|
|
|
y: y - BAR_HEIGHT - minYLabel.height / 2,
|
2020-12-27 18:26:30 +02:00
|
|
|
text: Math.max(...spreadsheet.values).toLocaleString(),
|
2020-12-11 13:13:23 +02:00
|
|
|
textAlign: "right",
|
|
|
|
});
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
return [minYLabel, maxYLabel];
|
|
|
|
};
|
|
|
|
|
|
|
|
const chartLines = (
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
groupId: string,
|
|
|
|
backgroundColor: string,
|
|
|
|
): ChartElements => {
|
|
|
|
const { chartWidth, chartHeight } = getChartDimentions(spreadsheet);
|
|
|
|
const xLine = newLinearElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
2020-12-11 13:13:23 +02:00
|
|
|
type: "line",
|
2020-11-29 18:32:51 +02:00
|
|
|
x,
|
|
|
|
y,
|
2020-12-11 13:13:23 +02:00
|
|
|
startArrowhead: null,
|
|
|
|
endArrowhead: null,
|
2020-12-14 17:29:39 +02:00
|
|
|
width: chartWidth,
|
2020-12-11 13:13:23 +02:00
|
|
|
points: [
|
|
|
|
[0, 0],
|
|
|
|
[chartWidth, 0],
|
|
|
|
],
|
2020-06-06 13:09:04 -07:00
|
|
|
});
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const yLine = newLinearElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
2020-12-11 13:13:23 +02:00
|
|
|
type: "line",
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
startArrowhead: null,
|
|
|
|
endArrowhead: null,
|
2020-12-14 17:29:39 +02:00
|
|
|
height: chartHeight,
|
2020-12-11 13:13:23 +02:00
|
|
|
points: [
|
|
|
|
[0, 0],
|
|
|
|
[0, -chartHeight],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const maxLine = newLinearElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
2020-12-11 13:13:23 +02:00
|
|
|
type: "line",
|
|
|
|
x,
|
|
|
|
y: y - BAR_HEIGHT - BAR_GAP,
|
|
|
|
startArrowhead: null,
|
|
|
|
endArrowhead: null,
|
|
|
|
strokeStyle: "dotted",
|
2020-12-14 17:29:39 +02:00
|
|
|
width: chartWidth,
|
2020-12-27 18:26:30 +02:00
|
|
|
opacity: GRID_OPACITY,
|
2020-12-11 13:13:23 +02:00
|
|
|
points: [
|
|
|
|
[0, 0],
|
|
|
|
[chartWidth, 0],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
return [xLine, yLine, maxLine];
|
|
|
|
};
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
// For the maths behind it https://excalidraw.com/#json=6320864370884608,O_5xfD-Agh32tytHpRJx1g
|
|
|
|
const chartBaseElements = (
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
groupId: string,
|
|
|
|
backgroundColor: string,
|
|
|
|
debug?: boolean,
|
|
|
|
): ChartElements => {
|
|
|
|
const { chartWidth, chartHeight } = getChartDimentions(spreadsheet);
|
2020-06-06 13:09:04 -07:00
|
|
|
|
2020-12-11 13:13:23 +02:00
|
|
|
const title = spreadsheet.title
|
2020-06-06 13:09:04 -07:00
|
|
|
? newTextElement({
|
2020-12-27 18:26:30 +02:00
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
2020-12-11 13:13:23 +02:00
|
|
|
...commonProps,
|
|
|
|
text: spreadsheet.title,
|
|
|
|
x: x + chartWidth / 2,
|
2020-12-27 18:26:30 +02:00
|
|
|
y: y - BAR_HEIGHT - BAR_GAP * 2 - DEFAULT_FONT_SIZE,
|
2020-12-11 13:13:23 +02:00
|
|
|
strokeSharpness: "sharp",
|
|
|
|
strokeStyle: "solid",
|
2020-06-06 13:09:04 -07:00
|
|
|
textAlign: "center",
|
|
|
|
})
|
|
|
|
: null;
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const debugRect = debug
|
|
|
|
? newElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
|
|
|
type: "rectangle",
|
|
|
|
x,
|
|
|
|
y: y - chartHeight,
|
|
|
|
width: chartWidth,
|
|
|
|
height: chartHeight,
|
|
|
|
strokeColor: colors.elementStroke[0],
|
|
|
|
fillStyle: "solid",
|
|
|
|
opacity: 6,
|
|
|
|
})
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return [
|
|
|
|
...(debugRect ? [debugRect] : []),
|
|
|
|
...(title ? [title] : []),
|
|
|
|
...chartXLabels(spreadsheet, x, y, groupId, backgroundColor),
|
|
|
|
...chartYLabels(spreadsheet, x, y, groupId, backgroundColor),
|
|
|
|
...chartLines(spreadsheet, x, y, groupId, backgroundColor),
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
const chartTypeBar = (
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
): ChartElements => {
|
|
|
|
const max = Math.max(...spreadsheet.values);
|
|
|
|
const groupId = randomId();
|
|
|
|
const backgroundColor = bgColors[Math.floor(Math.random() * bgColors.length)];
|
|
|
|
|
|
|
|
const bars = spreadsheet.values.map((value, index) => {
|
|
|
|
const barHeight = (value / max) * BAR_HEIGHT;
|
|
|
|
return newElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
|
|
|
type: "rectangle",
|
|
|
|
x: x + index * (BAR_WIDTH + BAR_GAP) + BAR_GAP,
|
|
|
|
y: y - barHeight - BAR_GAP,
|
|
|
|
width: BAR_WIDTH,
|
|
|
|
height: barHeight,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-11 13:13:23 +02:00
|
|
|
return [
|
|
|
|
...bars,
|
2020-12-27 18:26:30 +02:00
|
|
|
...chartBaseElements(
|
|
|
|
spreadsheet,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
groupId,
|
|
|
|
backgroundColor,
|
|
|
|
process.env.NODE_ENV === ENV.DEVELOPMENT,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
const chartTypeLine = (
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
): ChartElements => {
|
|
|
|
const max = Math.max(...spreadsheet.values);
|
|
|
|
const groupId = randomId();
|
|
|
|
const backgroundColor = bgColors[Math.floor(Math.random() * bgColors.length)];
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
const points = [];
|
|
|
|
for (const value of spreadsheet.values) {
|
|
|
|
const cx = index * (BAR_WIDTH + BAR_GAP);
|
|
|
|
const cy = -(value / max) * BAR_HEIGHT;
|
|
|
|
points.push([cx, cy]);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxX = Math.max(...points.map((element) => element[0]));
|
|
|
|
const maxY = Math.max(...points.map((element) => element[1]));
|
|
|
|
const minX = Math.min(...points.map((element) => element[0]));
|
|
|
|
const minY = Math.min(...points.map((element) => element[1]));
|
|
|
|
|
|
|
|
const line = newLinearElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
|
|
|
type: "line",
|
|
|
|
x: x + BAR_GAP + BAR_WIDTH / 2,
|
|
|
|
y: y - BAR_GAP,
|
|
|
|
startArrowhead: null,
|
|
|
|
endArrowhead: null,
|
|
|
|
height: maxY - minY,
|
|
|
|
width: maxX - minX,
|
|
|
|
strokeWidth: 2,
|
|
|
|
points: points as any,
|
|
|
|
});
|
|
|
|
|
|
|
|
const dots = spreadsheet.values.map((value, index) => {
|
|
|
|
const cx = index * (BAR_WIDTH + BAR_GAP) + BAR_GAP / 2;
|
|
|
|
const cy = -(value / max) * BAR_HEIGHT + BAR_GAP / 2;
|
|
|
|
return newElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
|
|
|
fillStyle: "solid",
|
|
|
|
strokeWidth: 2,
|
|
|
|
type: "ellipse",
|
|
|
|
x: x + cx + BAR_WIDTH / 2,
|
|
|
|
y: y + cy - BAR_GAP * 2,
|
|
|
|
width: BAR_GAP,
|
|
|
|
height: BAR_GAP,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const lines = spreadsheet.values.map((value, index) => {
|
|
|
|
const cx = index * (BAR_WIDTH + BAR_GAP) + BAR_GAP / 2;
|
|
|
|
const cy = (value / max) * BAR_HEIGHT + BAR_GAP / 2 + BAR_GAP;
|
|
|
|
return newLinearElement({
|
|
|
|
backgroundColor,
|
|
|
|
groupIds: [groupId],
|
|
|
|
...commonProps,
|
|
|
|
type: "line",
|
|
|
|
x: x + cx + BAR_WIDTH / 2 + BAR_GAP / 2,
|
|
|
|
y: y - cy,
|
|
|
|
startArrowhead: null,
|
|
|
|
endArrowhead: null,
|
|
|
|
height: cy,
|
|
|
|
strokeStyle: "dotted",
|
|
|
|
opacity: GRID_OPACITY,
|
|
|
|
points: [
|
|
|
|
[0, 0],
|
|
|
|
[0, cy],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return [
|
|
|
|
...chartBaseElements(
|
|
|
|
spreadsheet,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
groupId,
|
|
|
|
backgroundColor,
|
|
|
|
process.env.NODE_ENV === ENV.DEVELOPMENT,
|
|
|
|
),
|
|
|
|
line,
|
|
|
|
...lines,
|
|
|
|
...dots,
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const renderSpreadsheet = (
|
|
|
|
chartType: string,
|
|
|
|
spreadsheet: Spreadsheet,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
): ChartElements => {
|
|
|
|
if (chartType === "line") {
|
|
|
|
return chartTypeLine(spreadsheet, x, y);
|
|
|
|
}
|
|
|
|
return chartTypeBar(spreadsheet, x, y);
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|