eleventy-plugin-uncharted 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,18 +1,18 @@
1
1
  # Uncharted
2
2
 
3
- A CSS-based charting plugin for Eleventy. Renders charts as pure HTML/CSS with no JavaScript dependencies.
3
+ A CSS-based chart plugin for Eleventy. Renders charts as pure HTML/CSS with no JavaScript dependencies.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- npm install uncharted
8
+ npm install eleventy-plugin-uncharted
9
9
  ```
10
10
 
11
11
  ## Setup
12
12
 
13
13
  ```javascript
14
14
  // eleventy.config.js
15
- import uncharted from 'uncharted';
15
+ import uncharted from 'eleventy-plugin-uncharted';
16
16
 
17
17
  export default function(eleventyConfig) {
18
18
  eleventyConfig.addPlugin(uncharted);
@@ -40,13 +40,13 @@ If you set `injectCss: false`, you'll need to manually include the stylesheet in
40
40
 
41
41
  ## Chart Types
42
42
 
43
- | Type | Description |
44
- |------|-------------|
45
- | `donut` | Pie/donut chart using conic-gradient |
46
- | `stacked-bar` | Horizontal bars with stacked segments |
47
- | `stacked-column` | Vertical columns with stacked segments |
48
- | `dot` | Categorical dot chart with Y-axis positioning |
49
- | `scatter` | XY scatter plot with continuous axes |
43
+ | Type | Description | Negative Values |
44
+ |------|-------------|-----------------|
45
+ | `donut` | Pie/donut chart using conic-gradient | No |
46
+ | `stacked-bar` | Horizontal bars with stacked segments | No |
47
+ | `stacked-column` | Vertical columns with stacked segments | Yes |
48
+ | `dot` | Categorical dot chart with Y-axis positioning | Yes |
49
+ | `scatter` | XY scatter plot with continuous axes | Yes (X and Y) |
50
50
 
51
51
  ## Usage
52
52
 
@@ -110,10 +110,10 @@ charts:
110
110
 
111
111
  ## CSV Format
112
112
 
113
- CSV files use the first column as labels and subsequent columns as data series:
113
+ CSV files use the first column as labels and subsequent columns as data series. The column names can be anything descriptive:
114
114
 
115
115
  ```csv
116
- label,existing,new
116
+ department,existing,new
117
117
  Finance,11,11
118
118
  Sales,16,2
119
119
  Core,8,0
@@ -128,6 +128,22 @@ Point B,25,78,alpha
128
128
  Point C,15,32,beta
129
129
  ```
130
130
 
131
+ ## Negative Values
132
+
133
+ Stacked column, dot, and scatter charts support negative values. When negative values are present, a zero axis line appears automatically and values are positioned relative to it.
134
+
135
+ For stacked columns, positive values stack upward from zero and negative values stack downward:
136
+
137
+ ```csv
138
+ quarter,Cost,Profit
139
+ Q1,20,10
140
+ Q2,25,-10
141
+ Q3,15,25
142
+ Q4,30,-10
143
+ ```
144
+
145
+ The chart automatically calculates the range from the maximum positive stack to the minimum negative stack, ensuring proper scaling.
146
+
131
147
  ## Configuration Options
132
148
 
133
149
  | Option | Type | Description |
@@ -137,9 +153,12 @@ Point C,15,32,beta
137
153
  | `subtitle` | string | Subtitle below title |
138
154
  | `file` | string | Path to CSV file (relative to dataDir) |
139
155
  | `data` | array | Inline data array |
140
- | `max` | number | Maximum value for scaling |
156
+ | `max` | number | Maximum Y value for scaling |
157
+ | `min` | number | Minimum Y value for scaling (column, dot) |
141
158
  | `maxX` | number | Maximum X value (scatter only) |
142
159
  | `maxY` | number | Maximum Y value (scatter only) |
160
+ | `minX` | number | Minimum X value (scatter only) |
161
+ | `minY` | number | Minimum Y value (scatter only) |
143
162
  | `legend` | array | Custom legend labels |
144
163
  | `center` | object | Donut center content (`value`, `label`) |
145
164
  | `animate` | boolean | Override global animation setting |
package/css/uncharted.css CHANGED
@@ -201,6 +201,7 @@
201
201
  height: 100%;
202
202
  width: var(--value);
203
203
  transition: width 0.3s ease;
204
+ background-color: var(--color);
204
205
  }
205
206
 
206
207
  .chart-stacked-bar .bar-value {
@@ -243,6 +244,7 @@
243
244
  width: 100%;
244
245
  height: var(--value);
245
246
  transition: height 0.3s ease;
247
+ background-color: var(--color);
246
248
  }
247
249
 
248
250
  .chart-stacked-column .column-labels {
@@ -381,6 +383,7 @@
381
383
  bottom: var(--value);
382
384
  transform: translate(-50%, 50%);
383
385
  cursor: default;
386
+ background-color: var(--color);
384
387
  }
385
388
 
386
389
  .chart-dot .dot[title]:hover {
@@ -443,6 +446,7 @@
443
446
  bottom: var(--value);
444
447
  transform: translate(-50%, 50%);
445
448
  cursor: default;
449
+ background-color: var(--color);
446
450
  }
447
451
 
448
452
  .chart-scatter .dot[title]:hover {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleventy-plugin-uncharted",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "An Eleventy plugin that renders CSS-based charts from CSV data using shortcodes",
5
5
  "main": "eleventy.config.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { renderers } from './renderers/index.js';
2
2
  export { loadCSV, parseCSV } from './csv.js';
3
- export { slugify, calculatePercentages, getSeriesNames, escapeHtml } from './utils.js';
3
+ export { slugify, calculatePercentages, getLabelKey, getValueKey, getSeriesNames, escapeHtml } from './utils.js';
@@ -1,4 +1,4 @@
1
- import { slugify, escapeHtml } from '../utils.js';
1
+ import { slugify, escapeHtml, getLabelKey, getValueKey, getSeriesNames } from '../utils.js';
2
2
 
3
3
  /**
4
4
  * Render a donut/pie chart using conic-gradient
@@ -22,18 +22,22 @@ export function renderDonut(config) {
22
22
 
23
23
  const animateClass = animate ? ' chart-animate' : '';
24
24
 
25
- // Extract values - support both {label, value} format and series format
25
+ // Get column keys positionally
26
+ const labelKey = getLabelKey(data);
27
+ const valueKey = getValueKey(data);
28
+ const seriesKeys = getSeriesNames(data);
29
+
30
+ // Extract values - support both label/value format and series format
26
31
  let segments = [];
27
- if (data[0].value !== undefined) {
28
- // Direct {label, value} format
32
+ if (seriesKeys.length === 1) {
33
+ // Two columns: first is label, second is value (multiple rows)
29
34
  segments = data.map(item => ({
30
- label: item.label,
31
- value: typeof item.value === 'number' ? item.value : parseFloat(item.value) || 0
35
+ label: item[labelKey],
36
+ value: typeof item[valueKey] === 'number' ? item[valueKey] : parseFloat(item[valueKey]) || 0
32
37
  }));
33
38
  } else {
34
- // Series format - first row only for donut
35
- const seriesNames = Object.keys(data[0]).filter(k => k !== 'label');
36
- segments = seriesNames.map(name => ({
39
+ // Series format - first row only, columns after the first are series
40
+ segments = seriesKeys.map(name => ({
37
41
  label: name,
38
42
  value: typeof data[0][name] === 'number' ? data[0][name] : parseFloat(data[0][name]) || 0
39
43
  }));
@@ -1,4 +1,4 @@
1
- import { slugify, escapeHtml, getSeriesNames } from '../utils.js';
1
+ import { slugify, escapeHtml, getLabelKey, getSeriesNames } from '../utils.js';
2
2
 
3
3
  /**
4
4
  * Render a categorical dot chart (columns with dots at different Y positions)
@@ -8,18 +8,20 @@ import { slugify, escapeHtml, getSeriesNames } from '../utils.js';
8
8
  * @param {string} [config.subtitle] - Chart subtitle
9
9
  * @param {Object[]} config.data - Chart data with label column and value columns
10
10
  * @param {number} [config.max] - Maximum Y value (defaults to max in data)
11
+ * @param {number} [config.min] - Minimum Y value (defaults to min in data or 0)
11
12
  * @param {string[]} [config.legend] - Legend labels (defaults to series names)
12
13
  * @param {boolean} [config.animate] - Enable animations
13
14
  * @returns {string} - HTML string
14
15
  */
15
16
  export function renderDot(config) {
16
- const { title, subtitle, data, max, legend, animate } = config;
17
+ const { title, subtitle, data, max, min, legend, animate } = config;
17
18
 
18
19
  if (!data || data.length === 0) {
19
20
  return `<!-- Dot chart: no data provided -->`;
20
21
  }
21
22
 
22
- // Get series keys from data columns (excluding 'label')
23
+ // Get label key (first column) and series keys (remaining columns)
24
+ const labelKey = getLabelKey(data);
23
25
  const seriesKeys = getSeriesNames(data);
24
26
  const legendLabels = legend ?? seriesKeys;
25
27
  const animateClass = animate ? ' chart-animate' : '';
@@ -34,7 +36,7 @@ export function renderDot(config) {
34
36
  const dataMax = Math.max(...allValues);
35
37
  const dataMin = Math.min(...allValues);
36
38
  const maxValue = max ?? dataMax;
37
- const minValue = dataMin < 0 ? dataMin : 0;
39
+ const minValue = min ?? (dataMin < 0 ? dataMin : 0);
38
40
  const range = maxValue - minValue;
39
41
  const hasNegativeY = minValue < 0;
40
42
 
@@ -81,7 +83,7 @@ export function renderDot(config) {
81
83
 
82
84
  // Each row becomes a column with dots for each series
83
85
  data.forEach(row => {
84
- const label = row.label ?? '';
86
+ const label = row[labelKey] ?? '';
85
87
 
86
88
  html += `<div class="dot-col">`;
87
89
 
@@ -109,7 +111,7 @@ export function renderDot(config) {
109
111
  // X-axis labels
110
112
  html += `<div class="dot-labels">`;
111
113
  data.forEach(row => {
112
- const label = row.label ?? '';
114
+ const label = row[labelKey] ?? '';
113
115
  html += `<span class="dot-label">${escapeHtml(label)}</span>`;
114
116
  });
115
117
  html += `</div>`;
@@ -8,12 +8,14 @@ import { slugify, escapeHtml } from '../utils.js';
8
8
  * @param {Object[]} config.data - Chart data (with label, x, y, and optionally series)
9
9
  * @param {number} [config.maxX] - Maximum X value (defaults to max in data)
10
10
  * @param {number} [config.maxY] - Maximum Y value (defaults to max in data)
11
+ * @param {number} [config.minX] - Minimum X value (defaults to min in data or 0)
12
+ * @param {number} [config.minY] - Minimum Y value (defaults to min in data or 0)
11
13
  * @param {string[]} [config.legend] - Legend labels for series
12
14
  * @param {boolean} [config.animate] - Enable animations
13
15
  * @returns {string} - HTML string
14
16
  */
15
17
  export function renderScatter(config) {
16
- const { title, subtitle, data, maxX, maxY, legend, animate } = config;
18
+ const { title, subtitle, data, maxX, maxY, minX, minY, legend, animate } = config;
17
19
 
18
20
  if (!data || data.length === 0) {
19
21
  return `<!-- Scatter chart: no data provided -->`;
@@ -51,8 +53,8 @@ export function renderScatter(config) {
51
53
 
52
54
  const calcMaxX = maxX ?? dataMaxX;
53
55
  const calcMaxY = maxY ?? dataMaxY;
54
- const calcMinX = dataMinX < 0 ? dataMinX : 0;
55
- const calcMinY = dataMinY < 0 ? dataMinY : 0;
56
+ const calcMinX = minX ?? (dataMinX < 0 ? dataMinX : 0);
57
+ const calcMinY = minY ?? (dataMinY < 0 ? dataMinY : 0);
56
58
  const rangeX = calcMaxX - calcMinX;
57
59
  const rangeY = calcMaxY - calcMinY;
58
60
 
@@ -1,4 +1,4 @@
1
- import { slugify, calculatePercentages, getSeriesNames, escapeHtml } from '../utils.js';
1
+ import { slugify, calculatePercentages, getLabelKey, getSeriesNames, escapeHtml } from '../utils.js';
2
2
 
3
3
  /**
4
4
  * Render a stacked bar chart (horizontal)
@@ -18,7 +18,8 @@ export function renderStackedBar(config) {
18
18
  return `<!-- Stacked bar chart: no data provided -->`;
19
19
  }
20
20
 
21
- // Get actual data keys from the first row (excluding 'label')
21
+ // Get label key (first column) and series keys (remaining columns)
22
+ const labelKey = getLabelKey(data);
22
23
  const seriesKeys = getSeriesNames(data);
23
24
  // Use legend for display labels, fall back to data keys
24
25
  const legendLabels = legend ?? seriesKeys;
@@ -49,7 +50,7 @@ export function renderStackedBar(config) {
49
50
  html += `<div class="chart-bars">`;
50
51
 
51
52
  data.forEach(row => {
52
- const label = row.label ?? '';
53
+ const label = row[labelKey] ?? '';
53
54
  const values = seriesKeys.map(key => {
54
55
  const val = row[key];
55
56
  return typeof val === 'number' ? val : parseFloat(val) || 0;
@@ -1,4 +1,4 @@
1
- import { slugify, getSeriesNames, escapeHtml } from '../utils.js';
1
+ import { slugify, getLabelKey, getSeriesNames, escapeHtml } from '../utils.js';
2
2
 
3
3
  /**
4
4
  * Render a stacked column chart (vertical)
@@ -6,19 +6,21 @@ import { slugify, getSeriesNames, escapeHtml } from '../utils.js';
6
6
  * @param {string} config.title - Chart title
7
7
  * @param {string} [config.subtitle] - Chart subtitle
8
8
  * @param {Object[]} config.data - Chart data
9
- * @param {number} [config.max] - Maximum value for percentage calculation
9
+ * @param {number} [config.max] - Maximum value for Y-axis scaling
10
+ * @param {number} [config.min] - Minimum value for Y-axis scaling (for negative values)
10
11
  * @param {string[]} [config.legend] - Legend labels (defaults to series names)
11
12
  * @param {boolean} [config.animate] - Enable animations
12
13
  * @returns {string} - HTML string
13
14
  */
14
15
  export function renderStackedColumn(config) {
15
- const { title, subtitle, data, max, legend, animate } = config;
16
+ const { title, subtitle, data, max, min, legend, animate } = config;
16
17
 
17
18
  if (!data || data.length === 0) {
18
19
  return `<!-- Stacked column chart: no data provided -->`;
19
20
  }
20
21
 
21
- // Get actual data keys from the first row (excluding 'label')
22
+ // Get label key (first column) and series keys (remaining columns)
23
+ const labelKey = getLabelKey(data);
22
24
  const seriesKeys = getSeriesNames(data);
23
25
  // Use legend for display labels, fall back to data keys
24
26
  const legendLabels = legend ?? seriesKeys;
@@ -45,9 +47,9 @@ export function renderStackedColumn(config) {
45
47
  minNegativeStack = Math.min(minNegativeStack, negativeSum);
46
48
  });
47
49
 
48
- const hasNegativeY = minNegativeStack < 0;
50
+ const hasNegativeY = minNegativeStack < 0 || min < 0;
49
51
  const maxValue = max ?? maxPositiveStack;
50
- const minValue = minNegativeStack;
52
+ const minValue = min ?? minNegativeStack;
51
53
  const range = maxValue - minValue;
52
54
  const zeroPct = hasNegativeY ? ((0 - minValue) / range) * 100 : 0;
53
55
 
@@ -89,7 +91,7 @@ export function renderStackedColumn(config) {
89
91
  html += `<div class="chart-columns"${columnsStyle}>`;
90
92
 
91
93
  data.forEach(row => {
92
- const label = row.label ?? '';
94
+ const label = row[labelKey] ?? '';
93
95
  html += `<div class="column-track" title="${escapeHtml(label)}">`;
94
96
 
95
97
  if (hasNegativeY) {
@@ -172,7 +174,7 @@ export function renderStackedColumn(config) {
172
174
  // X-axis labels
173
175
  html += `<div class="column-labels">`;
174
176
  data.forEach(row => {
175
- const label = row.label ?? '';
177
+ const label = row[labelKey] ?? '';
176
178
  html += `<span class="column-label">${escapeHtml(label)}</span>`;
177
179
  });
178
180
  html += `</div>`;
package/src/utils.js CHANGED
@@ -25,13 +25,33 @@ export function calculatePercentages(values, max) {
25
25
  }
26
26
 
27
27
  /**
28
- * Extract series names from CSV data (all columns except 'label')
28
+ * Get the label key (first column name) from CSV data
29
+ * @param {Object[]} data - Array of data objects
30
+ * @returns {string|undefined} - The first column name, or undefined if no data
31
+ */
32
+ export function getLabelKey(data) {
33
+ if (!data || data.length === 0) return undefined;
34
+ return Object.keys(data[0])[0];
35
+ }
36
+
37
+ /**
38
+ * Get the value key (second column name) from CSV data
39
+ * @param {Object[]} data - Array of data objects
40
+ * @returns {string|undefined} - The second column name, or undefined if no data
41
+ */
42
+ export function getValueKey(data) {
43
+ if (!data || data.length === 0) return undefined;
44
+ return Object.keys(data[0])[1];
45
+ }
46
+
47
+ /**
48
+ * Extract series names from CSV data (all columns except the first)
29
49
  * @param {Object[]} data - Array of data objects
30
50
  * @returns {string[]} - Array of series names
31
51
  */
32
52
  export function getSeriesNames(data) {
33
53
  if (!data || data.length === 0) return [];
34
- return Object.keys(data[0]).filter(key => key !== 'label');
54
+ return Object.keys(data[0]).slice(1);
35
55
  }
36
56
 
37
57
  /**