Debian Codegraph @

js/core/barrenderer.ts

raw
1 import { CellRenderer, TextRenderer, GraphicsContext } from '@lumino/datagrid';
2
3 /**
4 * A cell renderer which renders data values as bars.
5 */
6 export class BarRenderer extends TextRenderer {
7 /**
8 * Construct a new bar renderer.
9 *
10 * @param options - The options for initializing the renderer.
11 */
12 constructor(options: BarRenderer.IOptions = {}) {
13 super(options);
14 this.barColor = options.barColor || '#4682b4';
15 this.barValue = options.barValue || 0;
16 this.orientation = options.orientation || 'horizontal';
17 this.barVerticalAlignment = options.barVerticalAlignment || 'bottom';
18 this.barHorizontalAlignment = options.barHorizontalAlignment || 'left';
19 this.showText = options.showText || true;
20 }
21
22 /**
23 * The CSS color for drawing the bar.
24 */
25 readonly barColor: CellRenderer.ConfigOption<string>;
26
27 /**
28 * The value of the bar, between 0. and 1.
29 */
30 readonly barValue: CellRenderer.ConfigOption<number>;
31
32 /**
33 * The orientation of the bar, can be horizontal or vertical.
34 */
35 readonly orientation: CellRenderer.ConfigOption<BarRenderer.Orientation>;
36
37 /**
38 * The horizontal alignment of the bar, default is 'bottom'.
39 */
40 readonly barVerticalAlignment: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>;
41
42 /**
43 * The vertical alignment of the bar, default is 'left'.
44 */
45 readonly barHorizontalAlignment: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>;
46
47 /**
48 * Whether to draw the text on the bar or not, default is true.
49 */
50 readonly showText: CellRenderer.ConfigOption<boolean>;
51
52 /**
53 * Paint the content for a cell.
54 *
55 * @param gc - The graphics context to use for drawing.
56 *
57 * @param config - The configuration data for the cell.
58 */
59 paint(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
60 const showText = CellRenderer.resolveOption(this.showText, config);
61
62 this.drawBackground(gc, config);
63 this.drawBar(gc, config);
64 if (showText) this.drawText(gc, config);
65 }
66
67 /**
68 * Prepare the graphics context for drawing a column of cells.
69 *
70 * @param gc - The graphics context to prepare.
71 *
72 * @param row - The index of the first row to be rendered.
73 *
74 * @param col - The index of the column to be rendered.
75 *
76 * @param field - The field descriptor for the column, or `null`.
77 */
78 prepare(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
79 // Look up the default state from the renderer.
80 const { font, textColor, barColor, backgroundColor, horizontalAlignment } =
81 this;
82
83 // Set up the default font.
84 if (font && typeof font === 'string') {
85 gc.font = font;
86 }
87
88 // Set up the default fill style.
89 if (backgroundColor && typeof backgroundColor === 'string') {
90 gc.fillStyle = backgroundColor;
91 } else if (barColor && typeof barColor === 'string') {
92 gc.fillStyle = barColor;
93 } else if (textColor && typeof textColor === 'string') {
94 gc.fillStyle = textColor;
95 }
96
97 // Set up the default text alignment.
98 if (typeof horizontalAlignment === 'string') {
99 gc.textAlign = horizontalAlignment;
100 } else {
101 gc.textAlign = 'left';
102 }
103
104 // Set up the default text baseline.
105 gc.textBaseline = 'bottom';
106 }
107
108 /**
109 * Draw the bar.
110 *
111 * @param gc - The graphics context to use for drawing.
112 *
113 * @param config - The configuration data for the cell.
114 */
115 drawBar(gc: GraphicsContext, config: CellRenderer.CellConfig): void {
116 const barColor = CellRenderer.resolveOption(this.barColor, config);
117 let barValue = CellRenderer.resolveOption(this.barValue, config);
118 const vAlign = CellRenderer.resolveOption(
119 this.barVerticalAlignment,
120 config,
121 );
122 const hAlign = CellRenderer.resolveOption(
123 this.barHorizontalAlignment,
124 config,
125 );
126 const orientation = CellRenderer.resolveOption(this.orientation, config);
127
128 // Bail if there is no color to draw.
129 if (!barColor) {
130 return;
131 }
132
133 // Be careful not to draw outside of the cell
134 if (barValue > 1) {
135 barValue = 1;
136 }
137 if (barValue < 0) {
138 barValue = 0;
139 }
140
141 let x: number = config.x;
142 let y: number = config.y;
143
144 // Draw the bar in the cell.
145 gc.fillStyle = barColor;
146 if (orientation === 'horizontal') {
147 const rect_width = barValue * config.width;
148
149 if (hAlign === 'center') x += (config.width - rect_width) / 2;
150 if (hAlign === 'right') x += config.width - rect_width;
151
152 gc.fillRect(x, y, rect_width, config.height);
153 } else {
154 const rect_height = barValue * config.height;
155
156 if (vAlign === 'center') {
157 y += (config.height - rect_height) / 2;
158 } else if (vAlign === 'bottom') {
159 y += config.height - rect_height;
160 }
161
162 gc.fillRect(x, y, config.width, rect_height);
163 }
164 }
165 }
166
167 /**
168 * The namespace for the `BarRenderer` class statics.
169 */
170 export namespace BarRenderer {
171 /**
172 * A type alias for the supported orientation modes.
173 */
174 export type Orientation = 'horizontal' | 'vertical';
175
176 /**
177 * An options object for initializing a bar renderer.
178 */
179 export interface IOptions extends TextRenderer.IOptions {
180 /**
181 * The background color for the cells.
182 *
183 * The default is `''`.
184 */
185 barColor?: CellRenderer.ConfigOption<string>;
186
187 /**
188 * The value of the bar, between 0. and 1..
189 *
190 * The default is `0.`.
191 */
192 barValue?: CellRenderer.ConfigOption<number>;
193
194 /**
195 * The orientation of the bar, can be horizontal or vertical.
196 *
197 * The default is `horizontal`.
198 */
199 orientation?: CellRenderer.ConfigOption<BarRenderer.Orientation>;
200
201 /**
202 * The horizontal alignment of the bar, can be bottom, center or top.
203 *
204 * The default is `bottom`.
205 */
206 barVerticalAlignment?: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>;
207
208 /**
209 * The vertical alignment of the bar, can be left, center or right.
210 *
211 * The default is `left`.
212 */
213 barHorizontalAlignment?: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>;
214
215 /**
216 * Whether to draw the text on the bar or not.
217 *
218 * The default is true.
219 */
220 showText?: CellRenderer.ConfigOption<boolean>;
221 }
222 }