1+ import type { ApexOptions } from "apexcharts" ;
12import ApexCharts from "apexcharts" ;
2- import { align_series_for , xaxis_type_for } from "./chart_series.js" ;
3- import { add_init_fn } from "./init.js" ;
4-
5- /**
6- * @typedef { import("./chart_series.js").ChartSeries } ChartSeries
7- * @typedef { import( "./chart_series.js").Series } Series
8- */
3+ import {
4+ align_series_for ,
5+ type ChartSeries ,
6+ type Series ,
7+ xaxis_type_for ,
8+ } from "./chart_series.ts" ;
9+ import { add_init_fn } from "./init.ts" ;
910
1011const sqlpage_chart = ( ( ) => {
1112 function sqlpage_chart ( ) {
12- /** @type {NodeListOf<HTMLElement> } */
13- const charts = document . querySelectorAll ( "[data-pre-init=chart]" ) ;
13+ const charts = document . querySelectorAll < HTMLElement > (
14+ "[data-pre-init=chart]" ,
15+ ) ;
1416 for ( const c of charts ) {
1517 try {
1618 build_sqlpage_chart ( c ) ;
@@ -50,24 +52,21 @@ const sqlpage_chart = (() => {
5052
5153 const referenceColor = colorNames [ isDarkTheme ? "gray-lt" : "gray" ] ;
5254
53- /** @typedef { { [property:string]: string| number| null} } ReferenceLine */
55+ type ReferenceLine = { [ property : string ] : string | number | null } ;
5456
55- /** @param {unknown } name @returns {string|undefined } */
56- const named_color = ( name ) =>
57+ const named_color = ( name : unknown ) : string | undefined =>
5758 typeof name === "string" ? colorNames [ name ] : undefined ;
5859
59- /** @param { string| number| null } name */
60- const reference_color = ( name ) => named_color ( name ) || referenceColor ;
60+ const reference_color = ( name : string | number | null ) =>
61+ named_color ( name ) || referenceColor ;
6162
62- /**
63- * @param {ReferenceLine[] } rows - the rows that carry an xline or a yline
64- * @param {"x"|"y" } column - the column the reference is written in
65- * @param {"x"|"y" } axis - the apexcharts axis that column is drawn on
66- * @param {(value: any) => any } to_axis_value - puts a SQL value on the axis
67- * @returns {object[] } apexcharts axis annotations
68- */
69- function reference_lines ( rows , column , axis , to_axis_value ) {
70- const on_axis = ( value ) => {
63+ function reference_lines (
64+ rows : ReferenceLine [ ] ,
65+ column : "x" | "y" ,
66+ axis : "x" | "y" ,
67+ to_axis_value : ( value : string | number ) => unknown ,
68+ ) : object [ ] {
69+ const on_axis = ( value : string | number | null ) => {
7170 if ( value == null ) return null ;
7271 const placed = to_axis_value ( value ) ;
7372 return Number . isNaN ( placed ) ? null : placed ;
@@ -94,13 +93,10 @@ const sqlpage_chart = (() => {
9493 } ) ;
9594 }
9695
97- /** @param {HTMLElement } c */
98- function build_sqlpage_chart ( c ) {
96+ function build_sqlpage_chart ( c : HTMLElement ) {
9997 const [ data_element ] = c . getElementsByTagName ( "data" ) ;
10098 const data = JSON . parse ( data_element . textContent ) ;
101- const chartContainer = /** @type {HTMLElement } */ (
102- c . querySelector ( ".chart" )
103- ) ;
99+ const chartContainer = c . querySelector ( ".chart" ) as HTMLElement ;
104100 chartContainer . innerHTML = "" ;
105101 const is_timeseries = ! ! data . time ;
106102 const chart_type =
@@ -109,11 +105,12 @@ const sqlpage_chart = (() => {
109105 ! ! data . stacked && STACKABLE_CHART_TYPES . includes ( chart_type ) ;
110106 const points = data . points . filter ( Array . isArray ) ;
111107 const reference_rows = data . points . filter ( ( row ) => ! Array . isArray ( row ) ) ;
112- /** @type { Series } */
113- const series_map = new Map ( ) ;
108+ const series_map : Series = new Map ( ) ;
114109 for ( const [ name , old_x , old_y , color , z ] of points ) {
115- /** @type {ChartSeries } */
116- const point_series = series_map . get ( name ) ?? { name, data : [ ] } ;
110+ const point_series : ChartSeries = series_map . get ( name ) ?? {
111+ name,
112+ data : [ ] ,
113+ } ;
117114 series_map . set ( name , point_series ) ;
118115 let x = old_x ;
119116 let y = old_y ;
@@ -145,7 +142,7 @@ const sqlpage_chart = (() => {
145142 ! ! data . horizontal ,
146143 ) ;
147144
148- let labels ;
145+ let labels : ( number | string | Date ) [ ] | undefined ;
149146 if ( chart_type === "pie" ) {
150147 labels = points . map ( ( [ name , x , _y ] ) => x || name ) ;
151148 series = points . map ( ( [ _name , _x , y ] ) => Number . parseFloat ( y ) ) ;
@@ -297,14 +294,12 @@ const sqlpage_chart = (() => {
297294 colors,
298295 series,
299296 } ;
300- if ( labels ) options . labels = labels ;
297+ if ( labels ) ( options as { labels ?: unknown } ) . labels = labels ;
301298 // Numeric axes count intervals; category and time axes use tickAmount as a
302299 // target for label density.
303- if ( data . xticks ) options . xaxis . tickAmount = data . xticks ;
304- const chart = new ApexCharts (
305- chartContainer ,
306- /** @type {import("apexcharts").ApexOptions } */ ( options ) ,
307- ) ;
300+ if ( data . xticks )
301+ ( options . xaxis as { tickAmount ?: number } ) . tickAmount = data . xticks ;
302+ const chart = new ApexCharts ( chartContainer , options as ApexOptions ) ;
308303 chart . render ( ) . catch ( console . error ) ;
309304 if ( window . charts ) window . charts . push ( chart ) ;
310305 else window . charts = [ chart ] ;
0 commit comments