decantr 0.9.0
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/AGENTS.md +868 -0
- package/CHANGELOG.md +255 -0
- package/CLAUDE.md +178 -0
- package/LICENSE +21 -0
- package/README.md +229 -0
- package/cli/art.js +127 -0
- package/cli/commands/a11y.js +61 -0
- package/cli/commands/audit.js +225 -0
- package/cli/commands/build.js +38 -0
- package/cli/commands/dev.js +18 -0
- package/cli/commands/doctor.js +197 -0
- package/cli/commands/figma-sync.js +48 -0
- package/cli/commands/figma-tokens.js +55 -0
- package/cli/commands/generate.js +26 -0
- package/cli/commands/init.js +116 -0
- package/cli/commands/lint.js +209 -0
- package/cli/commands/mcp.js +530 -0
- package/cli/commands/migrate.js +175 -0
- package/cli/commands/test.js +38 -0
- package/cli/commands/validate.js +354 -0
- package/cli/index.js +113 -0
- package/package.json +95 -0
- package/reference/atoms.md +517 -0
- package/reference/behaviors.md +384 -0
- package/reference/build-tooling.md +275 -0
- package/reference/color-guidelines.md +965 -0
- package/reference/component-lifecycle.md +137 -0
- package/reference/compound-spacing.md +95 -0
- package/reference/decantation-process.md +499 -0
- package/reference/dev-server-routes.md +93 -0
- package/reference/form-system.md +253 -0
- package/reference/i18n.md +336 -0
- package/reference/icons.md +576 -0
- package/reference/llm-primer.md +953 -0
- package/reference/plugins.md +252 -0
- package/reference/registry-consumption.md +76 -0
- package/reference/router.md +217 -0
- package/reference/shells.md +116 -0
- package/reference/spatial-guidelines.md +541 -0
- package/reference/ssr.md +234 -0
- package/reference/state-data.md +215 -0
- package/reference/state-patterns.md +166 -0
- package/reference/state.md +194 -0
- package/reference/style-system.md +110 -0
- package/reference/tokens.md +460 -0
- package/src/app.js +19 -0
- package/src/chart/_animate.js +266 -0
- package/src/chart/_base.js +109 -0
- package/src/chart/_data.js +209 -0
- package/src/chart/_format.js +106 -0
- package/src/chart/_interact.js +364 -0
- package/src/chart/_palette.js +105 -0
- package/src/chart/_renderer.js +52 -0
- package/src/chart/_scene.js +262 -0
- package/src/chart/_shared.js +371 -0
- package/src/chart/index.js +637 -0
- package/src/chart/layouts/_layout-base.js +328 -0
- package/src/chart/layouts/cartesian.js +148 -0
- package/src/chart/layouts/hierarchy.js +562 -0
- package/src/chart/layouts/polar.js +101 -0
- package/src/chart/renderers/canvas.js +179 -0
- package/src/chart/renderers/svg.js +256 -0
- package/src/chart/renderers/webgpu.js +715 -0
- package/src/chart/types/_type-base.js +26 -0
- package/src/chart/types/area.js +134 -0
- package/src/chart/types/bar.js +173 -0
- package/src/chart/types/box-plot.js +125 -0
- package/src/chart/types/bubble.js +63 -0
- package/src/chart/types/candlestick.js +115 -0
- package/src/chart/types/chord.js +85 -0
- package/src/chart/types/combination.js +108 -0
- package/src/chart/types/funnel.js +68 -0
- package/src/chart/types/gauge.js +163 -0
- package/src/chart/types/heatmap.js +98 -0
- package/src/chart/types/histogram.js +71 -0
- package/src/chart/types/line.js +111 -0
- package/src/chart/types/org-chart.js +93 -0
- package/src/chart/types/pie.js +81 -0
- package/src/chart/types/radar.js +96 -0
- package/src/chart/types/radial.js +68 -0
- package/src/chart/types/range-area.js +55 -0
- package/src/chart/types/range-bar.js +61 -0
- package/src/chart/types/sankey.js +73 -0
- package/src/chart/types/scatter.js +66 -0
- package/src/chart/types/sparkline.js +81 -0
- package/src/chart/types/sunburst.js +69 -0
- package/src/chart/types/swimlane.js +88 -0
- package/src/chart/types/treemap.js +62 -0
- package/src/chart/types/waterfall.js +100 -0
- package/src/components/_base.js +1658 -0
- package/src/components/_behaviors.js +1140 -0
- package/src/components/_primitives.js +534 -0
- package/src/components/_qr-encoder.js +539 -0
- package/src/components/accordion.js +207 -0
- package/src/components/affix.js +62 -0
- package/src/components/alert-dialog.js +75 -0
- package/src/components/alert.js +47 -0
- package/src/components/aspect-ratio.js +24 -0
- package/src/components/avatar-group.js +55 -0
- package/src/components/avatar.js +38 -0
- package/src/components/back-top.js +75 -0
- package/src/components/badge.js +74 -0
- package/src/components/banner.js +68 -0
- package/src/components/breadcrumb.js +162 -0
- package/src/components/button.js +115 -0
- package/src/components/calendar.js +131 -0
- package/src/components/card.js +192 -0
- package/src/components/carousel.js +98 -0
- package/src/components/cascader.js +261 -0
- package/src/components/checkbox.js +80 -0
- package/src/components/chip.js +81 -0
- package/src/components/code-block.js +82 -0
- package/src/components/collapsible.js +50 -0
- package/src/components/color-palette.js +438 -0
- package/src/components/color-picker.js +314 -0
- package/src/components/combobox.js +181 -0
- package/src/components/command.js +174 -0
- package/src/components/comment.js +206 -0
- package/src/components/context-menu.js +76 -0
- package/src/components/data-table.js +724 -0
- package/src/components/date-picker.js +217 -0
- package/src/components/date-range-picker.js +244 -0
- package/src/components/datetime-picker.js +271 -0
- package/src/components/descriptions.js +68 -0
- package/src/components/drawer.js +179 -0
- package/src/components/dropdown.js +88 -0
- package/src/components/empty.js +41 -0
- package/src/components/float-button.js +90 -0
- package/src/components/form.js +106 -0
- package/src/components/hover-card.js +49 -0
- package/src/components/icon.js +87 -0
- package/src/components/image.js +97 -0
- package/src/components/index.js +117 -0
- package/src/components/input-group.js +75 -0
- package/src/components/input-number.js +155 -0
- package/src/components/input-otp.js +178 -0
- package/src/components/input.js +91 -0
- package/src/components/kbd.js +36 -0
- package/src/components/label.js +25 -0
- package/src/components/list.js +118 -0
- package/src/components/masked-input.js +236 -0
- package/src/components/mentions.js +165 -0
- package/src/components/menu.js +259 -0
- package/src/components/message.js +80 -0
- package/src/components/modal.js +147 -0
- package/src/components/navigation-menu.js +166 -0
- package/src/components/notification.js +84 -0
- package/src/components/pagination.js +104 -0
- package/src/components/placeholder.js +132 -0
- package/src/components/popconfirm.js +70 -0
- package/src/components/popover.js +58 -0
- package/src/components/progress.js +61 -0
- package/src/components/qrcode.js +251 -0
- package/src/components/radiogroup.js +120 -0
- package/src/components/range-slider.js +176 -0
- package/src/components/rate.js +186 -0
- package/src/components/resizable.js +83 -0
- package/src/components/result.js +57 -0
- package/src/components/scroll-area.js +43 -0
- package/src/components/segmented.js +97 -0
- package/src/components/select.js +165 -0
- package/src/components/separator.js +31 -0
- package/src/components/shell.js +407 -0
- package/src/components/skeleton.js +39 -0
- package/src/components/slider.js +141 -0
- package/src/components/sortable-list.js +176 -0
- package/src/components/space.js +42 -0
- package/src/components/spinner.js +112 -0
- package/src/components/splitter.js +147 -0
- package/src/components/statistic.js +136 -0
- package/src/components/steps.js +99 -0
- package/src/components/switch.js +95 -0
- package/src/components/table.js +44 -0
- package/src/components/tabs.js +216 -0
- package/src/components/tag.js +115 -0
- package/src/components/textarea.js +82 -0
- package/src/components/time-picker.js +153 -0
- package/src/components/time-range-picker.js +170 -0
- package/src/components/timeline.js +226 -0
- package/src/components/toast.js +71 -0
- package/src/components/toggle.js +213 -0
- package/src/components/tooltip.js +57 -0
- package/src/components/tour.js +159 -0
- package/src/components/transfer.js +163 -0
- package/src/components/tree-select.js +274 -0
- package/src/components/tree.js +141 -0
- package/src/components/typography.js +136 -0
- package/src/components/upload.js +118 -0
- package/src/components/visually-hidden.js +20 -0
- package/src/components/watermark.js +124 -0
- package/src/core/index.js +539 -0
- package/src/core/lifecycle.js +69 -0
- package/src/css/atoms.js +651 -0
- package/src/css/components.js +940 -0
- package/src/css/derive.js +1296 -0
- package/src/css/index.js +265 -0
- package/src/css/runtime.js +268 -0
- package/src/css/styles/addons/bioluminescent.js +93 -0
- package/src/css/styles/addons/clay.js +70 -0
- package/src/css/styles/addons/clean.js +57 -0
- package/src/css/styles/addons/command-center.js +143 -0
- package/src/css/styles/addons/dopamine.js +83 -0
- package/src/css/styles/addons/editorial.js +80 -0
- package/src/css/styles/addons/glassmorphism.js +99 -0
- package/src/css/styles/addons/liquid-glass.js +105 -0
- package/src/css/styles/addons/prismatic.js +100 -0
- package/src/css/styles/addons/retro.js +63 -0
- package/src/css/styles/auradecantism.js +96 -0
- package/src/css/theme-registry.js +444 -0
- package/src/data/entity.js +281 -0
- package/src/data/index.js +13 -0
- package/src/data/persist.js +225 -0
- package/src/data/query.js +839 -0
- package/src/data/realtime.js +299 -0
- package/src/data/url.js +177 -0
- package/src/data/worker.js +134 -0
- package/src/explorer/archetypes.js +243 -0
- package/src/explorer/atoms.js +228 -0
- package/src/explorer/charts.js +497 -0
- package/src/explorer/components.js +129 -0
- package/src/explorer/foundations.js +949 -0
- package/src/explorer/icons.js +178 -0
- package/src/explorer/patterns.js +247 -0
- package/src/explorer/recipes.js +194 -0
- package/src/explorer/shared/pattern-examples.js +1337 -0
- package/src/explorer/shared/showcase-renderer.js +958 -0
- package/src/explorer/shared/spec-table.js +41 -0
- package/src/explorer/shared/usage-links.js +87 -0
- package/src/explorer/shell-config.js +10 -0
- package/src/explorer/shells.js +551 -0
- package/src/explorer/styles.js +161 -0
- package/src/explorer/tokens.js +262 -0
- package/src/explorer/tools.js +525 -0
- package/src/form/index.js +804 -0
- package/src/i18n/index.js +251 -0
- package/src/icons/essential.js +479 -0
- package/src/icons/index.js +53 -0
- package/src/plugins/index.js +282 -0
- package/src/registry/archetypes/content-site.json +71 -0
- package/src/registry/archetypes/docs-explorer.json +23 -0
- package/src/registry/archetypes/ecommerce.json +104 -0
- package/src/registry/archetypes/financial-dashboard.json +77 -0
- package/src/registry/archetypes/index.json +41 -0
- package/src/registry/archetypes/portfolio.json +82 -0
- package/src/registry/archetypes/recipe-community.json +159 -0
- package/src/registry/archetypes/saas-dashboard.json +86 -0
- package/src/registry/architect/cross-cutting.json +45 -0
- package/src/registry/architect/domains/ecommerce.json +294 -0
- package/src/registry/architect/domains/financial-services.json +302 -0
- package/src/registry/architect/index.json +26 -0
- package/src/registry/architect/traits.json +379 -0
- package/src/registry/atoms.json +16 -0
- package/src/registry/chart-showcase.json +160 -0
- package/src/registry/chart.json +136 -0
- package/src/registry/components.json +8616 -0
- package/src/registry/core.json +216 -0
- package/src/registry/css.json +319 -0
- package/src/registry/data.json +135 -0
- package/src/registry/foundations.json +11 -0
- package/src/registry/icons.json +463 -0
- package/src/registry/index.json +101 -0
- package/src/registry/patterns/activity-feed.json +37 -0
- package/src/registry/patterns/article-content.json +27 -0
- package/src/registry/patterns/auth-form.json +37 -0
- package/src/registry/patterns/author-card.json +20 -0
- package/src/registry/patterns/card-grid.json +127 -0
- package/src/registry/patterns/category-nav.json +26 -0
- package/src/registry/patterns/chart-grid.json +36 -0
- package/src/registry/patterns/chat-interface.json +37 -0
- package/src/registry/patterns/checklist-card.json +55 -0
- package/src/registry/patterns/comparison-panel.json +27 -0
- package/src/registry/patterns/component-showcase.json +24 -0
- package/src/registry/patterns/contact-form.json +31 -0
- package/src/registry/patterns/cta-section.json +20 -0
- package/src/registry/patterns/data-table.json +37 -0
- package/src/registry/patterns/detail-header.json +83 -0
- package/src/registry/patterns/detail-panel.json +27 -0
- package/src/registry/patterns/explorer-shell.json +22 -0
- package/src/registry/patterns/filter-bar.json +33 -0
- package/src/registry/patterns/filter-sidebar.json +27 -0
- package/src/registry/patterns/form-sections.json +110 -0
- package/src/registry/patterns/goal-tracker.json +27 -0
- package/src/registry/patterns/hero.json +107 -0
- package/src/registry/patterns/index.json +47 -0
- package/src/registry/patterns/kpi-grid.json +36 -0
- package/src/registry/patterns/media-gallery.json +20 -0
- package/src/registry/patterns/order-history.json +20 -0
- package/src/registry/patterns/pagination.json +19 -0
- package/src/registry/patterns/photo-to-recipe.json +36 -0
- package/src/registry/patterns/pipeline-tracker.json +28 -0
- package/src/registry/patterns/post-list.json +27 -0
- package/src/registry/patterns/pricing-table.json +32 -0
- package/src/registry/patterns/scorecard.json +28 -0
- package/src/registry/patterns/search-bar.json +20 -0
- package/src/registry/patterns/specimen-grid.json +19 -0
- package/src/registry/patterns/stat-card.json +55 -0
- package/src/registry/patterns/stats-bar.json +55 -0
- package/src/registry/patterns/steps-card.json +55 -0
- package/src/registry/patterns/table-of-contents.json +19 -0
- package/src/registry/patterns/testimonials.json +21 -0
- package/src/registry/patterns/timeline.json +27 -0
- package/src/registry/patterns/token-inspector.json +21 -0
- package/src/registry/patterns/wizard.json +27 -0
- package/src/registry/recipe-auradecantism.json +69 -0
- package/src/registry/recipe-clean.json +65 -0
- package/src/registry/recipe-command-center.json +78 -0
- package/src/registry/router.json +73 -0
- package/src/registry/schema/README.md +197 -0
- package/src/registry/skeletons.json +259 -0
- package/src/registry/state.json +137 -0
- package/src/registry/tokens.json +40 -0
- package/src/router/hash.js +17 -0
- package/src/router/history.js +18 -0
- package/src/router/index.js +598 -0
- package/src/ssr/index.js +922 -0
- package/src/state/arrays.js +181 -0
- package/src/state/devtools.js +647 -0
- package/src/state/index.js +498 -0
- package/src/state/middleware.js +288 -0
- package/src/state/scheduler.js +206 -0
- package/src/state/store.js +300 -0
- package/src/tags/index.js +19 -0
- package/src/tannins/auth.js +396 -0
- package/src/test/dom.js +352 -0
- package/src/test/index.js +62 -0
- package/src/test/state.js +306 -0
- package/tools/a11y-audit.js +487 -0
- package/tools/analyzer.js +315 -0
- package/tools/audit.js +706 -0
- package/tools/builder.js +1422 -0
- package/tools/css-extract.js +188 -0
- package/tools/dev-server.js +316 -0
- package/tools/dts-gen.js +1260 -0
- package/tools/figma-components.js +329 -0
- package/tools/figma-patterns.js +516 -0
- package/tools/figma-plugin/code.js +453 -0
- package/tools/figma-plugin/manifest.json +14 -0
- package/tools/figma-plugin/ui.html +268 -0
- package/tools/figma-render.js +293 -0
- package/tools/figma-tokens.js +712 -0
- package/tools/figma-upload.js +318 -0
- package/tools/generate.js +738 -0
- package/tools/icons.js +133 -0
- package/tools/init-templates.js +265 -0
- package/tools/install-hooks.sh +5 -0
- package/tools/migrations/0.5.0.js +53 -0
- package/tools/migrations/0.6.0.js +95 -0
- package/tools/minify.js +170 -0
- package/tools/pre-commit +4 -0
- package/tools/registry.js +662 -0
- package/tools/reset-playground.js +61 -0
- package/tools/starter-templates/content-site/app.js +49 -0
- package/tools/starter-templates/content-site/essence.js +19 -0
- package/tools/starter-templates/content-site/pages.js +31 -0
- package/tools/starter-templates/ecommerce/app.js +50 -0
- package/tools/starter-templates/ecommerce/essence.js +19 -0
- package/tools/starter-templates/ecommerce/pages.js +31 -0
- package/tools/starter-templates/landing-page/app.js +38 -0
- package/tools/starter-templates/landing-page/essence.js +18 -0
- package/tools/starter-templates/landing-page/pages.js +21 -0
- package/tools/starter-templates/portfolio/app.js +45 -0
- package/tools/starter-templates/portfolio/essence.js +19 -0
- package/tools/starter-templates/portfolio/pages.js +33 -0
- package/tools/starter-templates/saas-dashboard/app.js +70 -0
- package/tools/starter-templates/saas-dashboard/essence.js +19 -0
- package/tools/starter-templates/saas-dashboard/pages.js +31 -0
- package/tools/verify-pack.js +203 -0
- package/types/chart.d.ts +77 -0
- package/types/components.d.ts +587 -0
- package/types/core.d.ts +89 -0
- package/types/css.d.ts +149 -0
- package/types/data.d.ts +238 -0
- package/types/form.d.ts +164 -0
- package/types/i18n.d.ts +51 -0
- package/types/icons.d.ts +27 -0
- package/types/index.d.ts +13 -0
- package/types/router.d.ts +116 -0
- package/types/ssr.d.ts +102 -0
- package/types/state.d.ts +83 -0
- package/types/tags.d.ts +62 -0
- package/types/tannins.d.ts +63 -0
- package/types/test.d.ts +48 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backward-compatible re-exports from layouts/_layout-base.js.
|
|
3
|
+
* All layout utilities have moved to the layouts/ directory.
|
|
4
|
+
* This file preserves import paths used by tests and external code.
|
|
5
|
+
* @module types/_type-base
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
MARGINS,
|
|
10
|
+
MARGINS_NONE,
|
|
11
|
+
innerDimensions,
|
|
12
|
+
buildXScale,
|
|
13
|
+
buildYScale,
|
|
14
|
+
computeTicks,
|
|
15
|
+
chartColor,
|
|
16
|
+
PALETTE_SIZE
|
|
17
|
+
} from '../layouts/_layout-base.js';
|
|
18
|
+
|
|
19
|
+
// Re-export computeGridLines for tests
|
|
20
|
+
export function computeGridLines(yScale, count = 5) {
|
|
21
|
+
const ticks = yScale.ticks(count);
|
|
22
|
+
return ticks.map(t => ({
|
|
23
|
+
position: yScale(t),
|
|
24
|
+
label: String(t)
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Area chart layout — produces scene graph.
|
|
3
|
+
* Supports: stacked, 100%, range area, markers, negative values.
|
|
4
|
+
* @module types/area
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { scene, group, path, circle, gradient } from '../_scene.js';
|
|
8
|
+
import { areaPathD, smoothAreaPathD, pointsToPathD, smoothPathD } from '../_scene.js';
|
|
9
|
+
import { cartesian } from '../layouts/cartesian.js';
|
|
10
|
+
import { chartColor, buildAnnotations } from '../layouts/_layout-base.js';
|
|
11
|
+
import { resolve, groupBy, downsampleLTTB } from '../_shared.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {Object} spec — resolved chart spec
|
|
15
|
+
* @param {number} width
|
|
16
|
+
* @param {number} height
|
|
17
|
+
* @returns {Object} scene graph
|
|
18
|
+
*/
|
|
19
|
+
export function layoutArea(spec, width, height) {
|
|
20
|
+
const data = resolve(spec.data);
|
|
21
|
+
const coords = cartesian(spec, width, height, { yOpts: { stacked: !!spec.stacked } });
|
|
22
|
+
const { innerW, innerH, margins, xScale, yScale, xType, axisNodes, gridNodes } = coords;
|
|
23
|
+
const yFields = Array.isArray(spec.y) ? spec.y : [spec.y];
|
|
24
|
+
const baseline = yScale(0);
|
|
25
|
+
|
|
26
|
+
let series;
|
|
27
|
+
if (spec.stacked && yFields.length > 1) {
|
|
28
|
+
const prevBaselines = data.map(() => baseline);
|
|
29
|
+
series = yFields.map((f, i) => {
|
|
30
|
+
const points = data.map((d, j) => {
|
|
31
|
+
const xVal = xType === 'band' ? d[spec.x] : (xType === 'time' ? new Date(d[spec.x]) : +d[spec.x]);
|
|
32
|
+
const x = xScale(xVal) + (xType === 'band' ? xScale.bandwidth() / 2 : 0);
|
|
33
|
+
const val = +d[f] || 0;
|
|
34
|
+
const y1 = prevBaselines[j];
|
|
35
|
+
const y0 = yScale(yScale.invert(y1) + val);
|
|
36
|
+
return { x, y0: y0, y1, raw: d };
|
|
37
|
+
});
|
|
38
|
+
for (let j = 0; j < data.length; j++) prevBaselines[j] = points[j].y0;
|
|
39
|
+
return { key: f, color: chartColor(i), points };
|
|
40
|
+
});
|
|
41
|
+
} else if (spec.series) {
|
|
42
|
+
const groups = groupBy(data, spec.series);
|
|
43
|
+
series = [...groups.entries()].map(([key, rows], i) => {
|
|
44
|
+
const sampled = rows.length > innerW * 2 ? downsampleLTTB(rows, spec.x, yFields[0], Math.round(innerW * 2)) : rows;
|
|
45
|
+
return {
|
|
46
|
+
key, color: chartColor(i),
|
|
47
|
+
points: sampled.map(d => {
|
|
48
|
+
const xVal = xType === 'band' ? d[spec.x] : (xType === 'time' ? new Date(d[spec.x]) : +d[spec.x]);
|
|
49
|
+
const x = xScale(xVal) + (xType === 'band' ? xScale.bandwidth() / 2 : 0);
|
|
50
|
+
return { x, y0: yScale(+d[yFields[0]]), y1: baseline, raw: d };
|
|
51
|
+
})
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
series = yFields.map((f, i) => {
|
|
56
|
+
const sampled = data.length > innerW * 2 ? downsampleLTTB(data, spec.x, f, Math.round(innerW * 2)) : data;
|
|
57
|
+
return {
|
|
58
|
+
key: f, color: chartColor(i),
|
|
59
|
+
points: sampled.map(d => {
|
|
60
|
+
const xVal = xType === 'band' ? d[spec.x] : (xType === 'time' ? new Date(d[spec.x]) : +d[spec.x]);
|
|
61
|
+
const x = xScale(xVal) + (xType === 'band' ? xScale.bandwidth() / 2 : 0);
|
|
62
|
+
return { x, y0: yScale(+d[f]), y1: baseline, raw: d };
|
|
63
|
+
})
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Build scene
|
|
69
|
+
const children = [];
|
|
70
|
+
if (spec.grid !== false) children.push(...gridNodes);
|
|
71
|
+
children.push(...axisNodes);
|
|
72
|
+
|
|
73
|
+
// Collect gradient defs
|
|
74
|
+
const gradients = [];
|
|
75
|
+
|
|
76
|
+
for (let si = 0; si < series.length; si++) {
|
|
77
|
+
const s = series[si];
|
|
78
|
+
if (s.points.length < 2) continue;
|
|
79
|
+
|
|
80
|
+
// Gradient fill
|
|
81
|
+
const gradId = `area-grad-${si}`;
|
|
82
|
+
const yMin = Math.min(...s.points.map(p => p.y0));
|
|
83
|
+
const yMax = Math.max(...s.points.map(p => p.y1));
|
|
84
|
+
gradients.push(gradient({
|
|
85
|
+
id: gradId, x1: '0', y1: yMin, x2: '0', y2: yMax,
|
|
86
|
+
stops: [
|
|
87
|
+
{ offset: '0%', color: s.color, opacity: 0.3 },
|
|
88
|
+
{ offset: '100%', color: s.color, opacity: 0.02 }
|
|
89
|
+
]
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
// Fill area — smooth top edge when spec.smooth
|
|
93
|
+
const areaD = spec.smooth ? smoothAreaPathD(s.points) : areaPathD(s.points);
|
|
94
|
+
children.push(path({
|
|
95
|
+
d: areaD, fill: `url(#${gradId})`,
|
|
96
|
+
data: { series: s.key }, key: `area-${s.key}`
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
// Stroke line on top
|
|
100
|
+
const linePts = s.points.map(p => ({ x: p.x, y: p.y0 }));
|
|
101
|
+
const lineD = spec.smooth ? smoothPathD(linePts) : pointsToPathD(linePts);
|
|
102
|
+
children.push(path({
|
|
103
|
+
d: lineD, stroke: s.color, strokeWidth: 2,
|
|
104
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
105
|
+
class: 'd-chart-line',
|
|
106
|
+
data: { series: s.key }, key: `area-line-${s.key}`
|
|
107
|
+
}));
|
|
108
|
+
|
|
109
|
+
// Data point dots (suppress when points > 50)
|
|
110
|
+
if (spec.dots !== false && s.points.length <= 50) {
|
|
111
|
+
for (const p of s.points) {
|
|
112
|
+
children.push(circle({
|
|
113
|
+
cx: p.x, cy: p.y0, r: 3,
|
|
114
|
+
fill: s.color, stroke: 'var(--d-bg)', strokeWidth: 2,
|
|
115
|
+
class: 'd-chart-point',
|
|
116
|
+
data: { series: s.key, value: p.raw[Array.isArray(spec.y) ? s.key : spec.y[0] || spec.y], label: String(p.raw[spec.x]) },
|
|
117
|
+
key: `area-dot-${s.key}-${p.x}`
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Prepend gradients to children so SVG renderer can collect them
|
|
124
|
+
children.unshift(...gradients);
|
|
125
|
+
|
|
126
|
+
// Annotations
|
|
127
|
+
if (spec.annotations) {
|
|
128
|
+
children.push(...buildAnnotations(spec.annotations, xScale, yScale, xType, innerW, innerH));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return scene(width, height, [
|
|
132
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
133
|
+
], { type: 'area', series, xScale, yScale, xType, margins, innerW, innerH, baseline, dataLength: data.length });
|
|
134
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bar chart layout — produces scene graph.
|
|
3
|
+
* Supports: grouped, stacked, horizontal, 100% stacked.
|
|
4
|
+
* @module types/bar
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { scene, group, rect, text } from '../_scene.js';
|
|
8
|
+
import { cartesian } from '../layouts/cartesian.js';
|
|
9
|
+
import { chartColor, buildYScale, MARGINS, innerDimensions, buildAnnotations } from '../layouts/_layout-base.js';
|
|
10
|
+
import { resolve, unique, scaleBand } from '../_shared.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {Object} spec — resolved chart spec
|
|
14
|
+
* @param {number} width
|
|
15
|
+
* @param {number} height
|
|
16
|
+
* @returns {Object} scene graph
|
|
17
|
+
*/
|
|
18
|
+
export function layoutBar(spec, width, height) {
|
|
19
|
+
const data = resolve(spec.data);
|
|
20
|
+
const yFields = Array.isArray(spec.y) ? spec.y : [spec.y];
|
|
21
|
+
const horizontal = !!spec.horizontal;
|
|
22
|
+
|
|
23
|
+
if (horizontal) return layoutHorizontalBar(spec, data, yFields, width, height);
|
|
24
|
+
|
|
25
|
+
const categories = unique(data, spec.x);
|
|
26
|
+
const margins = { ...MARGINS };
|
|
27
|
+
const { innerW, innerH } = innerDimensions(width, height, margins);
|
|
28
|
+
|
|
29
|
+
const xScale = scaleBand(categories, [0, innerW], 0.2);
|
|
30
|
+
const yScale = buildYScale(data, yFields, innerH, { stacked: !!spec.stacked });
|
|
31
|
+
const bandW = xScale.bandwidth();
|
|
32
|
+
|
|
33
|
+
// Build bars
|
|
34
|
+
const bars = buildBars(data, spec, categories, yFields, xScale, yScale, bandW);
|
|
35
|
+
|
|
36
|
+
// Build scene children
|
|
37
|
+
const coords = cartesian(spec, width, height, { bandX: true, yOpts: { stacked: !!spec.stacked } });
|
|
38
|
+
const children = [];
|
|
39
|
+
|
|
40
|
+
if (spec.grid !== false) children.push(...coords.gridNodes);
|
|
41
|
+
children.push(...coords.axisNodes);
|
|
42
|
+
|
|
43
|
+
// Render bars as rects
|
|
44
|
+
const isStacked = spec.stacked && yFields.length > 1;
|
|
45
|
+
for (const bar of bars) {
|
|
46
|
+
const segCount = bar.segments.length;
|
|
47
|
+
for (let si = 0; si < segCount; si++) {
|
|
48
|
+
const seg = bar.segments[si];
|
|
49
|
+
// Stacked: only topmost segment gets rounded corners, middle segments are flat
|
|
50
|
+
const rx = isStacked && si < segCount - 1 ? 0 : Math.min(4, seg.width / 4);
|
|
51
|
+
children.push(rect({
|
|
52
|
+
x: seg.x, y: seg.y, w: Math.max(0, seg.width), h: Math.max(0, seg.height),
|
|
53
|
+
rx, fill: seg.color,
|
|
54
|
+
class: 'd-chart-bar',
|
|
55
|
+
data: { series: seg.field, value: seg.value, label: bar.category },
|
|
56
|
+
key: `bar-${bar.category}-${seg.field}`
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
// Labels
|
|
60
|
+
if (spec.labels) {
|
|
61
|
+
children.push(text({
|
|
62
|
+
x: seg.x + seg.width / 2, y: seg.y - 4,
|
|
63
|
+
content: String(seg.value), anchor: 'middle',
|
|
64
|
+
class: 'd-chart-axis', fontSize: 'var(--d-text-xs)'
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Annotations
|
|
71
|
+
if (spec.annotations) {
|
|
72
|
+
children.push(...buildAnnotations(spec.annotations, xScale, yScale, 'band', innerW, innerH));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const series = yFields.map((f, i) => ({ key: f, color: chartColor(i) }));
|
|
76
|
+
|
|
77
|
+
return scene(width, height, [
|
|
78
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
79
|
+
], { type: 'bar', series, xScale, yScale, xType: 'band', categories, bars, margins, innerW, innerH, dataLength: data.length });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function layoutHorizontalBar(spec, data, yFields, width, height) {
|
|
83
|
+
const coords = cartesian({ ...spec, horizontal: true }, width, height, {
|
|
84
|
+
bandX: true, yOpts: { stacked: !!spec.stacked }
|
|
85
|
+
});
|
|
86
|
+
const { innerW, innerH, margins, xScale, yScale, categories, axisNodes, gridNodes } = coords;
|
|
87
|
+
const bandW = xScale.bandwidth();
|
|
88
|
+
|
|
89
|
+
const children = [];
|
|
90
|
+
if (spec.grid !== false) children.push(...gridNodes);
|
|
91
|
+
children.push(...axisNodes);
|
|
92
|
+
|
|
93
|
+
const bars = [];
|
|
94
|
+
for (const cat of categories) {
|
|
95
|
+
const row = data.find(d => d[spec.x] === cat);
|
|
96
|
+
if (!row) continue;
|
|
97
|
+
const segments = yFields.map((f, i) => {
|
|
98
|
+
const val = +row[f] || 0;
|
|
99
|
+
return {
|
|
100
|
+
field: f, color: chartColor(i),
|
|
101
|
+
x: 0, y: xScale(cat), width: yScale(val), height: bandW,
|
|
102
|
+
value: val, raw: row
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
bars.push({ category: cat, segments });
|
|
106
|
+
for (const seg of segments) {
|
|
107
|
+
children.push(rect({
|
|
108
|
+
x: seg.x, y: seg.y, w: Math.max(0, seg.width), h: Math.max(0, seg.height),
|
|
109
|
+
rx: Math.min(4, seg.height / 4), fill: seg.color,
|
|
110
|
+
class: 'd-chart-bar',
|
|
111
|
+
data: { series: seg.field, value: seg.value, label: cat },
|
|
112
|
+
key: `bar-h-${cat}-${seg.field}`
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const series = yFields.map((f, i) => ({ key: f, color: chartColor(i) }));
|
|
118
|
+
|
|
119
|
+
return scene(width, height, [
|
|
120
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
121
|
+
], { type: 'bar', series, bars, categories, margins, innerW, innerH, dataLength: data.length, horizontal: true });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function buildBars(data, spec, categories, yFields, xScale, yScale, bandW) {
|
|
125
|
+
if (spec.stacked && yFields.length > 1) {
|
|
126
|
+
return categories.map(cat => {
|
|
127
|
+
const row = data.find(d => d[spec.x] === cat);
|
|
128
|
+
if (!row) return null;
|
|
129
|
+
let cumY = 0;
|
|
130
|
+
const segments = yFields.map((f, i) => {
|
|
131
|
+
const val = +row[f] || 0;
|
|
132
|
+
const y0 = cumY;
|
|
133
|
+
cumY += val;
|
|
134
|
+
return {
|
|
135
|
+
field: f, color: chartColor(i),
|
|
136
|
+
x: xScale(cat), y: yScale(cumY), width: bandW,
|
|
137
|
+
height: yScale(y0) - yScale(cumY), value: val, raw: row
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
return { category: cat, segments };
|
|
141
|
+
}).filter(Boolean);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (yFields.length > 1) {
|
|
145
|
+
const groupBandW = bandW / yFields.length;
|
|
146
|
+
return categories.map(cat => {
|
|
147
|
+
const row = data.find(d => d[spec.x] === cat);
|
|
148
|
+
if (!row) return null;
|
|
149
|
+
const segments = yFields.map((f, i) => {
|
|
150
|
+
const val = +row[f] || 0;
|
|
151
|
+
return {
|
|
152
|
+
field: f, color: chartColor(i),
|
|
153
|
+
x: xScale(cat) + i * groupBandW, y: yScale(val),
|
|
154
|
+
width: groupBandW * 0.85, height: yScale(0) - yScale(val),
|
|
155
|
+
value: val, raw: row
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
return { category: cat, segments };
|
|
159
|
+
}).filter(Boolean);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return data.map(d => {
|
|
163
|
+
const val = +d[yFields[0]] || 0;
|
|
164
|
+
return {
|
|
165
|
+
category: d[spec.x],
|
|
166
|
+
segments: [{
|
|
167
|
+
field: yFields[0], color: chartColor(0),
|
|
168
|
+
x: xScale(d[spec.x]), y: yScale(val), width: bandW,
|
|
169
|
+
height: yScale(0) - yScale(val), value: val, raw: d
|
|
170
|
+
}]
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Box Plot chart layout.
|
|
3
|
+
* Supports: multi-series, horizontal, whisker types (min-max, 1.5IQR).
|
|
4
|
+
* @module types/box-plot
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { scene, group, rect, line, circle, text } from '../_scene.js';
|
|
8
|
+
import { cartesian } from '../layouts/cartesian.js';
|
|
9
|
+
import { chartColor, MARGINS, innerDimensions, buildGridNodes, buildYAxisNodes } from '../layouts/_layout-base.js';
|
|
10
|
+
import { resolve, unique, scaleBand, scaleLinear, extent } from '../_shared.js';
|
|
11
|
+
|
|
12
|
+
export function layoutBoxPlot(spec, width, height) {
|
|
13
|
+
const data = resolve(spec.data);
|
|
14
|
+
const xField = spec.x;
|
|
15
|
+
const yField = Array.isArray(spec.y) ? spec.y[0] : spec.y;
|
|
16
|
+
const whiskerType = spec.whiskerType || '1.5IQR';
|
|
17
|
+
const categories = unique(data, xField);
|
|
18
|
+
|
|
19
|
+
const margins = { ...MARGINS };
|
|
20
|
+
const { innerW, innerH } = innerDimensions(width, height, margins);
|
|
21
|
+
|
|
22
|
+
const xScale = scaleBand(categories, [0, innerW], 0.3);
|
|
23
|
+
const bandW = xScale.bandwidth();
|
|
24
|
+
|
|
25
|
+
// Compute box stats per category
|
|
26
|
+
const boxes = categories.map((cat, ci) => {
|
|
27
|
+
const values = data.filter(d => d[xField] === cat).map(d => +d[yField]).sort((a, b) => a - b);
|
|
28
|
+
return { category: cat, color: chartColor(ci), ...computeBoxStats(values, whiskerType) };
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Y scale from overall range
|
|
32
|
+
let yMin = Infinity, yMax = -Infinity;
|
|
33
|
+
for (const b of boxes) {
|
|
34
|
+
if (b.whiskerLow < yMin) yMin = b.whiskerLow;
|
|
35
|
+
if (b.whiskerHigh > yMax) yMax = b.whiskerHigh;
|
|
36
|
+
for (const o of b.outliers) {
|
|
37
|
+
if (o < yMin) yMin = o;
|
|
38
|
+
if (o > yMax) yMax = o;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const pad = (yMax - yMin) * 0.1 || 1;
|
|
42
|
+
const yScale = scaleLinear([yMin - pad, yMax + pad], [innerH, 0]);
|
|
43
|
+
|
|
44
|
+
const children = [];
|
|
45
|
+
if (spec.grid !== false) children.push(...buildGridNodes(yScale, innerW));
|
|
46
|
+
|
|
47
|
+
// X axis labels
|
|
48
|
+
children.push(line({ x1: 0, y1: innerH, x2: innerW, y2: innerH, stroke: 'var(--d-border)', class: 'd-chart-axis' }));
|
|
49
|
+
for (const cat of categories) {
|
|
50
|
+
const x = xScale(cat) + bandW / 2;
|
|
51
|
+
children.push(text({ x, y: innerH + 18, content: String(cat), anchor: 'middle', class: 'd-chart-axis' }));
|
|
52
|
+
}
|
|
53
|
+
children.push(...buildYAxisNodes(yScale, innerH, spec));
|
|
54
|
+
|
|
55
|
+
// Draw boxes
|
|
56
|
+
for (const b of boxes) {
|
|
57
|
+
const cx = xScale(b.category) + bandW / 2;
|
|
58
|
+
const boxW = bandW * 0.6;
|
|
59
|
+
const x0 = cx - boxW / 2;
|
|
60
|
+
|
|
61
|
+
// Whisker lines
|
|
62
|
+
children.push(line({ x1: cx, y1: yScale(b.whiskerHigh), x2: cx, y2: yScale(b.q3), stroke: b.color, strokeWidth: 1.5, strokeDash: '3,2' }));
|
|
63
|
+
children.push(line({ x1: cx, y1: yScale(b.q1), x2: cx, y2: yScale(b.whiskerLow), stroke: b.color, strokeWidth: 1.5, strokeDash: '3,2' }));
|
|
64
|
+
|
|
65
|
+
// Whisker caps
|
|
66
|
+
const capW = boxW * 0.4;
|
|
67
|
+
children.push(line({ x1: cx - capW, y1: yScale(b.whiskerHigh), x2: cx + capW, y2: yScale(b.whiskerHigh), stroke: b.color, strokeWidth: 1.5 }));
|
|
68
|
+
children.push(line({ x1: cx - capW, y1: yScale(b.whiskerLow), x2: cx + capW, y2: yScale(b.whiskerLow), stroke: b.color, strokeWidth: 1.5 }));
|
|
69
|
+
|
|
70
|
+
// Box (Q1 to Q3)
|
|
71
|
+
children.push(rect({
|
|
72
|
+
x: x0, y: yScale(b.q3), w: boxW, h: yScale(b.q1) - yScale(b.q3),
|
|
73
|
+
fill: b.color, opacity: 0.3, stroke: b.color, strokeWidth: 1.5,
|
|
74
|
+
class: 'd-chart-bar',
|
|
75
|
+
data: { label: b.category, value: `Q1:${b.q1.toFixed(1)} Med:${b.median.toFixed(1)} Q3:${b.q3.toFixed(1)}`, series: b.category },
|
|
76
|
+
key: `box-${b.category}`
|
|
77
|
+
}));
|
|
78
|
+
|
|
79
|
+
// Median line
|
|
80
|
+
children.push(line({ x1: x0, y1: yScale(b.median), x2: x0 + boxW, y2: yScale(b.median), stroke: b.color, strokeWidth: 2 }));
|
|
81
|
+
|
|
82
|
+
// Outliers
|
|
83
|
+
for (const o of b.outliers) {
|
|
84
|
+
children.push(circle({ cx, cy: yScale(o), r: 3, fill: 'none', stroke: b.color, strokeWidth: 1.5, class: 'd-chart-point' }));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return scene(width, height, [
|
|
89
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
90
|
+
], { type: 'box-plot', boxes, margins, innerW, innerH, dataLength: data.length });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function computeBoxStats(sorted, whiskerType) {
|
|
94
|
+
const n = sorted.length;
|
|
95
|
+
if (n === 0) return { q1: 0, median: 0, q3: 0, whiskerLow: 0, whiskerHigh: 0, outliers: [] };
|
|
96
|
+
|
|
97
|
+
const median = quantile(sorted, 0.5);
|
|
98
|
+
const q1 = quantile(sorted, 0.25);
|
|
99
|
+
const q3 = quantile(sorted, 0.75);
|
|
100
|
+
const iqr = q3 - q1;
|
|
101
|
+
|
|
102
|
+
let whiskerLow, whiskerHigh, outliers = [];
|
|
103
|
+
|
|
104
|
+
if (whiskerType === 'min-max') {
|
|
105
|
+
whiskerLow = sorted[0];
|
|
106
|
+
whiskerHigh = sorted[n - 1];
|
|
107
|
+
} else {
|
|
108
|
+
// 1.5 IQR
|
|
109
|
+
const lowerFence = q1 - 1.5 * iqr;
|
|
110
|
+
const upperFence = q3 + 1.5 * iqr;
|
|
111
|
+
whiskerLow = sorted.find(v => v >= lowerFence) ?? sorted[0];
|
|
112
|
+
whiskerHigh = [...sorted].reverse().find(v => v <= upperFence) ?? sorted[n - 1];
|
|
113
|
+
outliers = sorted.filter(v => v < lowerFence || v > upperFence);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { q1, median, q3, whiskerLow, whiskerHigh, outliers };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function quantile(sorted, p) {
|
|
120
|
+
const idx = (sorted.length - 1) * p;
|
|
121
|
+
const lo = Math.floor(idx);
|
|
122
|
+
const hi = Math.ceil(idx);
|
|
123
|
+
if (lo === hi) return sorted[lo];
|
|
124
|
+
return sorted[lo] + (sorted[hi] - sorted[lo]) * (idx - lo);
|
|
125
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bubble chart layout — scatter with size encoding.
|
|
3
|
+
* @module types/bubble
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { scene, group, circle, text } from '../_scene.js';
|
|
7
|
+
import { cartesian } from '../layouts/cartesian.js';
|
|
8
|
+
import { chartColor } from '../layouts/_layout-base.js';
|
|
9
|
+
import { resolve, groupBy, extent } from '../_shared.js';
|
|
10
|
+
|
|
11
|
+
export function layoutBubble(spec, width, height) {
|
|
12
|
+
const data = resolve(spec.data);
|
|
13
|
+
const coords = cartesian(spec, width, height);
|
|
14
|
+
const { innerW, innerH, margins, xScale, yScale, xType, axisNodes, gridNodes } = coords;
|
|
15
|
+
const yField = Array.isArray(spec.y) ? spec.y[0] : spec.y;
|
|
16
|
+
const sizeField = spec.size || spec.z || yField;
|
|
17
|
+
const [sizeMin, sizeMax] = extent(data, sizeField);
|
|
18
|
+
const minR = 4, maxR = Math.min(innerW, innerH) / 12;
|
|
19
|
+
|
|
20
|
+
function bubbleR(v) {
|
|
21
|
+
const t = sizeMax > sizeMin ? (v - sizeMin) / (sizeMax - sizeMin) : 0.5;
|
|
22
|
+
return minR + Math.sqrt(t) * (maxR - minR);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let series;
|
|
26
|
+
if (spec.series) {
|
|
27
|
+
const groups = groupBy(data, spec.series);
|
|
28
|
+
series = [...groups.entries()].map(([key, rows], i) => ({
|
|
29
|
+
key, color: chartColor(i),
|
|
30
|
+
points: rows.map(d => ({ ...mapPt(d, spec, xScale, yScale, xType, yField), r: bubbleR(+d[sizeField]) }))
|
|
31
|
+
}));
|
|
32
|
+
} else {
|
|
33
|
+
series = [{ key: yField, color: chartColor(0),
|
|
34
|
+
points: data.map(d => ({ ...mapPt(d, spec, xScale, yScale, xType, yField), r: bubbleR(+d[sizeField]) }))
|
|
35
|
+
}];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const children = [];
|
|
39
|
+
if (spec.grid !== false) children.push(...gridNodes);
|
|
40
|
+
children.push(...axisNodes);
|
|
41
|
+
|
|
42
|
+
for (const s of series) {
|
|
43
|
+
for (let i = 0; i < s.points.length; i++) {
|
|
44
|
+
const p = s.points[i];
|
|
45
|
+
children.push(circle({
|
|
46
|
+
cx: p.x, cy: p.y, r: p.r,
|
|
47
|
+
fill: s.color, opacity: 0.7, class: 'd-chart-point',
|
|
48
|
+
data: { series: s.key, value: p.raw[yField], label: String(p.raw[spec.x]) },
|
|
49
|
+
key: `bubble-${s.key}-${i}`
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return scene(width, height, [
|
|
55
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
56
|
+
], { type: 'bubble', series, margins, innerW, innerH, dataLength: data.length });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function mapPt(d, spec, xScale, yScale, xType, yField) {
|
|
60
|
+
const xVal = xType === 'band' ? d[spec.x] : (xType === 'time' ? new Date(d[spec.x]) : +d[spec.x]);
|
|
61
|
+
const x = xScale(xVal) + (xType === 'band' ? xScale.bandwidth() / 2 : 0);
|
|
62
|
+
return { x, y: yScale(+d[yField]), raw: d };
|
|
63
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Candlestick / OHLC chart layout.
|
|
3
|
+
* Supports: hollow variant, volume overlay.
|
|
4
|
+
* @module types/candlestick
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { scene, group, rect, line, text } from '../_scene.js';
|
|
8
|
+
import { MARGINS, innerDimensions, buildGridNodes, buildYAxisNodes, chartColor } from '../layouts/_layout-base.js';
|
|
9
|
+
import { resolve, scaleBand, scaleLinear, unique, extent } from '../_shared.js';
|
|
10
|
+
|
|
11
|
+
export function layoutCandlestick(spec, width, height) {
|
|
12
|
+
const data = resolve(spec.data);
|
|
13
|
+
const xField = spec.x;
|
|
14
|
+
const openField = spec.open || 'open';
|
|
15
|
+
const highField = spec.high || 'high';
|
|
16
|
+
const lowField = spec.low || 'low';
|
|
17
|
+
const closeField = spec.close || 'close';
|
|
18
|
+
const volumeField = spec.volume;
|
|
19
|
+
|
|
20
|
+
const margins = { ...MARGINS };
|
|
21
|
+
if (volumeField) margins.bottom += 60; // extra space for volume
|
|
22
|
+
const { innerW, innerH: rawInnerH } = innerDimensions(width, height, margins);
|
|
23
|
+
const volumeH = volumeField ? 50 : 0;
|
|
24
|
+
const innerH = rawInnerH - volumeH;
|
|
25
|
+
|
|
26
|
+
const categories = data.map(d => d[xField]);
|
|
27
|
+
const xScale = scaleBand(categories, [0, innerW], 0.1);
|
|
28
|
+
const bandW = xScale.bandwidth();
|
|
29
|
+
|
|
30
|
+
// Y scale from high/low range
|
|
31
|
+
let yMin = Infinity, yMax = -Infinity;
|
|
32
|
+
for (const d of data) {
|
|
33
|
+
const lo = +d[lowField], hi = +d[highField];
|
|
34
|
+
if (lo < yMin) yMin = lo;
|
|
35
|
+
if (hi > yMax) yMax = hi;
|
|
36
|
+
}
|
|
37
|
+
const pad = (yMax - yMin) * 0.05;
|
|
38
|
+
const yScale = scaleLinear([yMin - pad, yMax + pad], [innerH, 0]);
|
|
39
|
+
|
|
40
|
+
const children = [];
|
|
41
|
+
if (spec.grid !== false) children.push(...buildGridNodes(yScale, innerW));
|
|
42
|
+
|
|
43
|
+
// X axis
|
|
44
|
+
children.push(line({ x1: 0, y1: innerH, x2: innerW, y2: innerH, stroke: 'var(--d-border)', class: 'd-chart-axis' }));
|
|
45
|
+
// Show every Nth label to avoid overlap
|
|
46
|
+
const labelInterval = Math.max(1, Math.floor(categories.length / 8));
|
|
47
|
+
for (let i = 0; i < categories.length; i++) {
|
|
48
|
+
if (i % labelInterval !== 0) continue;
|
|
49
|
+
const x = xScale(categories[i]) + bandW / 2;
|
|
50
|
+
children.push(text({ x, y: innerH + 18, content: String(categories[i]), anchor: 'middle', class: 'd-chart-axis' }));
|
|
51
|
+
}
|
|
52
|
+
children.push(...buildYAxisNodes(yScale, innerH, spec));
|
|
53
|
+
|
|
54
|
+
// Candles
|
|
55
|
+
const upColor = 'var(--d-success)';
|
|
56
|
+
const downColor = 'var(--d-error)';
|
|
57
|
+
|
|
58
|
+
for (let i = 0; i < data.length; i++) {
|
|
59
|
+
const d = data[i];
|
|
60
|
+
const open = +d[openField], close = +d[closeField];
|
|
61
|
+
const high = +d[highField], low = +d[lowField];
|
|
62
|
+
const isUp = close >= open;
|
|
63
|
+
const color = isUp ? upColor : downColor;
|
|
64
|
+
const cx = xScale(d[xField]) + bandW / 2;
|
|
65
|
+
const bodyW = bandW * 0.6;
|
|
66
|
+
|
|
67
|
+
// Wick (high to low)
|
|
68
|
+
children.push(line({ x1: cx, y1: yScale(high), x2: cx, y2: yScale(low), stroke: color, strokeWidth: 1 }));
|
|
69
|
+
|
|
70
|
+
// Body
|
|
71
|
+
const bodyTop = yScale(Math.max(open, close));
|
|
72
|
+
const bodyBottom = yScale(Math.min(open, close));
|
|
73
|
+
const bodyH = Math.max(1, bodyBottom - bodyTop);
|
|
74
|
+
|
|
75
|
+
if (spec.hollow && isUp) {
|
|
76
|
+
children.push(rect({
|
|
77
|
+
x: cx - bodyW / 2, y: bodyTop, w: bodyW, h: bodyH,
|
|
78
|
+
fill: 'none', stroke: color, strokeWidth: 1.5,
|
|
79
|
+
class: 'd-chart-bar',
|
|
80
|
+
data: { label: String(d[xField]), value: `O:${open} H:${high} L:${low} C:${close}`, series: 'candle' },
|
|
81
|
+
key: `candle-${i}`
|
|
82
|
+
}));
|
|
83
|
+
} else {
|
|
84
|
+
children.push(rect({
|
|
85
|
+
x: cx - bodyW / 2, y: bodyTop, w: bodyW, h: bodyH,
|
|
86
|
+
fill: color, class: 'd-chart-bar',
|
|
87
|
+
data: { label: String(d[xField]), value: `O:${open} H:${high} L:${low} C:${close}`, series: 'candle' },
|
|
88
|
+
key: `candle-${i}`
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Volume overlay
|
|
94
|
+
if (volumeField) {
|
|
95
|
+
const volMax = Math.max(...data.map(d => +d[volumeField] || 0), 1);
|
|
96
|
+
const volScale = scaleLinear([0, volMax], [0, volumeH]);
|
|
97
|
+
const volY0 = innerH + 30;
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < data.length; i++) {
|
|
100
|
+
const d = data[i];
|
|
101
|
+
const vol = +d[volumeField] || 0;
|
|
102
|
+
const isUp = +d[closeField] >= +d[openField];
|
|
103
|
+
const h = volScale(vol);
|
|
104
|
+
children.push(rect({
|
|
105
|
+
x: xScale(d[xField]), y: volY0 + volumeH - h, w: bandW, h,
|
|
106
|
+
fill: isUp ? upColor : downColor, opacity: 0.3,
|
|
107
|
+
key: `vol-${i}`
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return scene(width, height, [
|
|
113
|
+
group({ transform: `translate(${margins.left},${margins.top})` }, children)
|
|
114
|
+
], { type: 'candlestick', margins, innerW, innerH, dataLength: data.length });
|
|
115
|
+
}
|