@inglorious/charts 1.0.1

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.
Files changed (49) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +554 -0
  3. package/package.json +64 -0
  4. package/src/base.css +86 -0
  5. package/src/cartesian/area.js +392 -0
  6. package/src/cartesian/area.test.js +366 -0
  7. package/src/cartesian/bar.js +445 -0
  8. package/src/cartesian/bar.test.js +346 -0
  9. package/src/cartesian/line.js +823 -0
  10. package/src/cartesian/line.test.js +177 -0
  11. package/src/chart.test.js +444 -0
  12. package/src/component/brush.js +264 -0
  13. package/src/component/empty-state.js +33 -0
  14. package/src/component/empty-state.test.js +81 -0
  15. package/src/component/grid.js +123 -0
  16. package/src/component/grid.test.js +123 -0
  17. package/src/component/legend.js +76 -0
  18. package/src/component/legend.test.js +103 -0
  19. package/src/component/tooltip.js +65 -0
  20. package/src/component/tooltip.test.js +96 -0
  21. package/src/component/x-axis.js +212 -0
  22. package/src/component/x-axis.test.js +148 -0
  23. package/src/component/y-axis.js +77 -0
  24. package/src/component/y-axis.test.js +107 -0
  25. package/src/handlers.js +150 -0
  26. package/src/index.js +264 -0
  27. package/src/polar/donut.js +181 -0
  28. package/src/polar/donut.test.js +152 -0
  29. package/src/polar/pie.js +758 -0
  30. package/src/polar/pie.test.js +268 -0
  31. package/src/shape/curve.js +55 -0
  32. package/src/shape/dot.js +104 -0
  33. package/src/shape/rectangle.js +46 -0
  34. package/src/shape/sector.js +58 -0
  35. package/src/template.js +25 -0
  36. package/src/theme.css +90 -0
  37. package/src/utils/cartesian-layout.js +164 -0
  38. package/src/utils/chart-utils.js +30 -0
  39. package/src/utils/colors.js +77 -0
  40. package/src/utils/data-utils.js +155 -0
  41. package/src/utils/data-utils.test.js +210 -0
  42. package/src/utils/extract-data-keys.js +22 -0
  43. package/src/utils/padding.js +16 -0
  44. package/src/utils/paths.js +279 -0
  45. package/src/utils/process-declarative-child.js +46 -0
  46. package/src/utils/scales.js +250 -0
  47. package/src/utils/shared-context.js +166 -0
  48. package/src/utils/shared-context.test.js +237 -0
  49. package/src/utils/tooltip-handlers.js +129 -0
@@ -0,0 +1,152 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { render } from "@inglorious/web/test"
5
+ import { beforeEach, describe, expect, it, vi } from "vitest"
6
+
7
+ import { donut } from "./donut.js"
8
+
9
+ describe("donut", () => {
10
+ let entity
11
+ let api
12
+
13
+ beforeEach(() => {
14
+ entity = {
15
+ id: "test-donut",
16
+ type: "donut",
17
+ data: [
18
+ { label: "Category A", value: 20 },
19
+ { label: "Category B", value: 35 },
20
+ { label: "Category C", value: 15 },
21
+ ],
22
+ width: 400,
23
+ height: 400,
24
+ colors: ["#3b82f6", "#ef4444", "#10b981"],
25
+ showTooltip: true,
26
+ showLabel: true,
27
+ }
28
+
29
+ api = {
30
+ notify: vi.fn(),
31
+ }
32
+ })
33
+
34
+ describe("render()", () => {
35
+ it("should render donut chart with data", () => {
36
+ const result = donut.render(entity, api)
37
+ const container = document.createElement("div")
38
+ render(result, container)
39
+
40
+ const svg = container.querySelector("svg")
41
+ expect(svg).toBeTruthy()
42
+ })
43
+
44
+ it("should handle empty data gracefully", () => {
45
+ entity.data = []
46
+
47
+ const result = donut.render(entity, api)
48
+ const container = document.createElement("div")
49
+ render(result, container)
50
+
51
+ const svg = container.querySelector("svg")
52
+ expect(svg).toBeTruthy()
53
+ })
54
+
55
+ it("should render center text when configured", () => {
56
+ entity.centerText = "Total Sales"
57
+
58
+ const result = donut.render(entity, api)
59
+ const container = document.createElement("div")
60
+ render(result, container)
61
+
62
+ expect(container.textContent).toContain("Total Sales")
63
+ })
64
+
65
+ it("should notify tooltipShow and tooltipHide on slice hover", () => {
66
+ const result = donut.render(entity, api)
67
+ const container = document.createElement("div")
68
+ render(result, container)
69
+
70
+ const slice = container.querySelector(".iw-chart-pie-slice")
71
+ expect(slice).toBeTruthy()
72
+
73
+ slice.dispatchEvent(
74
+ new MouseEvent("mouseenter", {
75
+ bubbles: true,
76
+ clientX: 100,
77
+ clientY: 120,
78
+ }),
79
+ )
80
+
81
+ expect(api.notify).toHaveBeenCalledWith(
82
+ `#${entity.id}:tooltipShow`,
83
+ expect.objectContaining({
84
+ label: expect.any(String),
85
+ value: expect.any(Number),
86
+ color: expect.any(String),
87
+ x: expect.any(Number),
88
+ y: expect.any(Number),
89
+ }),
90
+ )
91
+
92
+ slice.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true }))
93
+ expect(api.notify).toHaveBeenCalledWith(`#${entity.id}:tooltipHide`)
94
+ })
95
+
96
+ it("should not notify tooltipShow when tooltip is disabled", () => {
97
+ entity.showTooltip = false
98
+
99
+ const result = donut.render(entity, api)
100
+ const container = document.createElement("div")
101
+ render(result, container)
102
+
103
+ const slice = container.querySelector(".iw-chart-pie-slice")
104
+ expect(slice).toBeTruthy()
105
+
106
+ slice.dispatchEvent(
107
+ new MouseEvent("mouseenter", {
108
+ bubbles: true,
109
+ clientX: 90,
110
+ clientY: 110,
111
+ }),
112
+ )
113
+
114
+ expect(api.notify).not.toHaveBeenCalledWith(
115
+ `#${entity.id}:tooltipShow`,
116
+ expect.any(Object),
117
+ )
118
+ })
119
+
120
+ it("should notify tooltipMove on svg mouse move when tooltip is visible", () => {
121
+ entity.tooltip = { label: "Category A", value: 20, color: "#3b82f6" }
122
+
123
+ const result = donut.render(entity, api)
124
+ const container = document.createElement("div")
125
+ render(result, container)
126
+
127
+ const svg = container.querySelector("svg")
128
+ expect(svg).toBeTruthy()
129
+
130
+ svg.dispatchEvent(
131
+ new MouseEvent("mousemove", {
132
+ bubbles: true,
133
+ clientX: 100,
134
+ clientY: 120,
135
+ }),
136
+ )
137
+
138
+ expect(api.notify).toHaveBeenCalledWith(`#${entity.id}:tooltipMove`, {
139
+ x: 115,
140
+ y: 105,
141
+ })
142
+ })
143
+ })
144
+
145
+ describe("renderTooltip()", () => {
146
+ it("should return a tooltip composition function", () => {
147
+ const tooltipFn = donut.renderTooltip(entity, { config: {} }, api)
148
+ expect(typeof tooltipFn).toBe("function")
149
+ expect(tooltipFn.isTooltip).toBe(true)
150
+ })
151
+ })
152
+ })