From d4254a953afd86e33305f16c101dc554421780d5 Mon Sep 17 00:00:00 2001 From: "Graciliano M. P." Date: Wed, 4 Nov 2026 11:25:19 -0300 Subject: [PATCH 1/2] Export the ChartJS onClick callback with Function.toJS so it works on dart2wasm; bump to 3.0.2 The onClick closure flowed into JS as a plain Dart function via jsify, which dart2wasm rejects. Also fix renderScatterChart passing onClick into the JS 'timed' slot, wire onClick for bar charts, test the handler for every ChartJS chart type, and run the Chrome tests on dart2wasm in CI. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/dart.yml | 2 + CHANGELOG.md | 14 ++++ lib/chartjs-2.9.4/chart_engine_wrapper.js | 4 +- lib/src/chart_engine_base.dart | 2 +- lib/src/chart_engine_chartjs.dart | 17 +++-- lib/src/chart_engine_series.dart | 2 + pubspec.yaml | 2 +- test/chart_engine_integration_test.dart | 85 +++++++++++++++++++++++ 8 files changed, 118 insertions(+), 10 deletions(-) diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index 12ca268..7fa0a80 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -27,6 +27,8 @@ jobs: run: dart run dependency_validator - name: Run tests (VM + Chrome) run: dart test --platform vm,chrome + - name: Run tests (Chrome, dart2wasm) + run: dart test --platform chrome --compiler dart2wasm # - name: dartdoc # run: dartdoc --no-generate-docs - name: dart pub publish --dry-run diff --git a/CHANGELOG.md b/CHANGELOG.md index 204f4e9..897b4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## 3.0.2 + +- `ChartEngineChartJS`: + - Fix `ChartOptions.onClick` under `dart2wasm`: the callback is now exported + to JS with `Function.toJS` (`dart:js_interop`) instead of flowing into JS + as a plain Dart closure, which `dart2wasm` rejects. + - `renderScatterChart`: fix the argument order passed to the JS wrapper. + With an `onClick` set, the callback landed in the `timed` slot, turning + the X axis into a time scale and failing to render. + - `renderBarChart`/`renderHorizontalBarChart`: wire `onClick`, which was + silently ignored by the JS wrapper. +- Tests: invoke the `onClick` handler of every ChartJS chart type. +- CI: run the Chrome tests on `dart2wasm` too. + ## 3.0.1 - Apply the `dart format` style of the new SDK floor. diff --git a/lib/chartjs-2.9.4/chart_engine_wrapper.js b/lib/chartjs-2.9.4/chart_engine_wrapper.js index 423365e..a0f8849 100644 --- a/lib/chartjs-2.9.4/chart_engine_wrapper.js +++ b/lib/chartjs-2.9.4/chart_engine_wrapper.js @@ -467,7 +467,7 @@ return chart ; }; - exports.renderBar = function (horizontal, canvas, title, xTitle, yTitle, xLabels, xAxisMinMax, yAxisMinMax, series, colors) { + exports.renderBar = function (horizontal, canvas, title, xTitle, yTitle, xLabels, xAxisMinMax, yAxisMinMax, series, colors, onClickCallback) { const ctx = canvas.getContext('2d'); const datasets = [] ; @@ -487,7 +487,7 @@ const type = horizontal ? 'horizontalBar' : 'bar' ; - const config = this.configure(type, title, xTitle, yTitle, xLabels, xAxisMinMax, yAxisMinMax, datasets) ; + const config = this.configure(type, title, xTitle, yTitle, xLabels, xAxisMinMax, yAxisMinMax, datasets, null, null,null,null,null, null, onClickCallback) ; const chart = new Chart(ctx, config); diff --git a/lib/src/chart_engine_base.dart b/lib/src/chart_engine_base.dart index 8fe82e8..c7e493a 100644 --- a/lib/src/chart_engine_base.dart +++ b/lib/src/chart_engine_base.dart @@ -12,7 +12,7 @@ final String CHART_ENGINE_PACKAGE_PATH = 'packages/chart_engine'; abstract class ChartEngine { /// `chart_engine` package version. // ignore: non_constant_identifier_names - static final String VERSION = '3.0.1'; + static final String VERSION = '3.0.2'; String get version; diff --git a/lib/src/chart_engine_chartjs.dart b/lib/src/chart_engine_chartjs.dart index d91b0fc..661664d 100644 --- a/lib/src/chart_engine_chartjs.dart +++ b/lib/src/chart_engine_chartjs.dart @@ -187,16 +187,20 @@ class ChartEngineChartJS extends ChartEngine { return minMax?.toJS; } - static Function? _onClick(ChartData chartData) { + /// Exports [ChartOptions.onClick] to JS with `Function.toJS`. + /// + /// A plain Dart closure can't flow into JS when compiled to Wasm + /// (`jsify` rejects it), so it must be exported explicitly. + static JSFunction? _onClick(ChartData chartData) { var onClick = chartData.options.onClick; if (onClick == null) return null; - return (Object? activeItems, Object? xItems, Object? yItems) { + return ((JSAny? activeItems, JSAny? xItems, JSAny? yItems) { onClick( - activeItems.objectDartify() as List?, - xItems.objectDartify() as List?, - yItems.objectDartify() as List?, + activeItems.dartify() as List?, + xItems.dartify() as List?, + yItems.dartify() as List?, ); - }; + }).toJS; } static JSObject? _verticalLinesConfig(ChartData chartData) { @@ -434,6 +438,7 @@ class ChartEngineChartJS extends ChartEngine { seriesPairs.toJSDeep, _verticalLinesConfig(chartSeries), colors.toJSDeep, + false, _onClick(chartSeries), ]; diff --git a/lib/src/chart_engine_series.dart b/lib/src/chart_engine_series.dart index 1550f6e..53abe79 100644 --- a/lib/src/chart_engine_series.dart +++ b/lib/src/chart_engine_series.dart @@ -1057,6 +1057,8 @@ abstract class ChartOptions { String verticalLinesDefaultColor = '#ff0000'; /// Callback for `click` events. + /// + /// Currently only called by the ChartJS engine. void Function(List? activeItems, List? xItems, List? yItems)? onClick; /// Copies this instance. diff --git a/pubspec.yaml b/pubspec.yaml index 0d1b50f..8900014 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: chart_engine description: Chart generator with interchangeable chart engines, like ChartJS and ApexCharts. -version: 3.0.1 +version: 3.0.2 homepage: https://github.com/Colossus-Services/chart_engine environment: diff --git a/test/chart_engine_integration_test.dart b/test/chart_engine_integration_test.dart index 0bc3d03..b13f976 100644 --- a/test/chart_engine_integration_test.dart +++ b/test/chart_engine_integration_test.dart @@ -1,6 +1,8 @@ @TestOn('browser') library; +import 'dart:js_interop_unsafe'; + import 'package:chart_engine/chart_engine_all.dart'; import 'package:test/test.dart'; import 'package:web_utils/web_utils.dart'; @@ -313,6 +315,89 @@ void main() { }); }); + group('ChartEngineChartJS onClick', () { + late ChartEngineChartJS engine; + + setUpAll(() async { + engine = ChartEngineChartJS(); + expect(await engine.loadFinancial(), isTrue); + }); + + /// Simulates a click by calling the `onClick` handler that the wrapper + /// set in the chart options, passing the first element of the first + /// dataset as the clicked item. + void click(RenderedChart chart) { + var chartObj = chart.chartJSObject!; + var options = chartObj['options'] as JSObject; + var handler = options['onClick'] as JSFunction?; + expect(handler, isNotNull, reason: 'No `onClick` in chart options'); + + var meta = chartObj.callMethod('getDatasetMeta'.toJS, 0.toJS); + var elements = meta['data'] as JSArray; + var items = [elements.toDart.first].toJS; + var evt = {'offsetX': 100, 'offsetY': 100}.toJSDeep; + + handler!.callAsFunction(chartObj, evt, items); + } + + final renders = { + 'line': (o, d) => engine.renderLineChart(o, d as ChartSeries), + 'time-series': (o, d) => + engine.renderTimeSeriesChart(o, d as ChartTimeSeries), + 'bar-vertical': (o, d) => engine.renderBarChart(o, d as ChartSeries), + 'bar-horizontal': (o, d) => + engine.renderHorizontalBarChart(o, d as ChartSeries), + 'gauge': (o, d) => engine.renderGaugeChart(o, d as ChartSet), + 'scatter': (o, d) => engine.renderScatterChart(o, d as ChartSeriesPair), + 'scatter-time-series': (o, d) => + engine.renderScatterTimedChart(o, d as ChartTimeSeries), + 'financial-ohlc': (o, d) => + engine.renderFinancialChart(o, d as ChartTimeSeries), + }; + + final data = { + 'line': _lineSeries, + 'time-series': _timeSeries, + 'bar-vertical': _lineSeries, + 'bar-horizontal': _lineSeries, + 'gauge': _set, + 'scatter': _pairSeries, + 'scatter-time-series': _timeSeries, + 'financial-ohlc': _financialSeries, + }; + + for (var MapEntry(key: type, value: render) in renders.entries) { + test('calls the Dart callback: $type', () { + var calls = <(List?, List?, List?)>[]; + var chartData = data[type]!(); + chartData.options.onClick = (a, x, y) => calls.add((a, x, y)); + + var chart = render(_newOutput(), chartData); + expect(chart.type, equals(type)); + + click(chart); + + expect(calls, hasLength(1)); + var (activeItems, xItems, yItems) = calls.single; + + expect(activeItems, hasLength(1)); + var item = activeItems!.single as Map; + expect(item['datasetIndex'], equals(0)); + expect(item['index'], equals(0)); + expect(item.containsKey('value'), isTrue); + + expect(xItems, isA()); + expect(yItems, isA()); + }); + } + + test('no onClick: no handler in options', () { + var chart = engine.renderLineChart(_newOutput(), _lineSeries()); + var options = chart.chartJSObject!['options'] as JSObject; + expect(options['onClick'].isDefinedAndNotNull, isFalse); + }); + }); + group('ChartEngineApexCharts', () { test('financial chart is not supported', () async { var engine = ChartEngineApexCharts(); From 5695136949f02b8c73f457cd655befeaef662727 Mon Sep 17 00:00:00 2001 From: "Graciliano M. P." Date: Wed, 4 Nov 2026 11:26:25 -0300 Subject: [PATCH 2/2] Merge the onClick test's parallel data/render maps into one map of records Co-Authored-By: Claude Opus 5.5 --- test/chart_engine_integration_test.dart | 67 ++++++++++++++----------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/test/chart_engine_integration_test.dart b/test/chart_engine_integration_test.dart index b13f976..5687cdd 100644 --- a/test/chart_engine_integration_test.dart +++ b/test/chart_engine_integration_test.dart @@ -340,36 +340,47 @@ void main() { handler!.callAsFunction(chartObj, evt, items); } - final renders = { - 'line': (o, d) => engine.renderLineChart(o, d as ChartSeries), - 'time-series': (o, d) => - engine.renderTimeSeriesChart(o, d as ChartTimeSeries), - 'bar-vertical': (o, d) => engine.renderBarChart(o, d as ChartSeries), - 'bar-horizontal': (o, d) => - engine.renderHorizontalBarChart(o, d as ChartSeries), - 'gauge': (o, d) => engine.renderGaugeChart(o, d as ChartSet), - 'scatter': (o, d) => engine.renderScatterChart(o, d as ChartSeriesPair), - 'scatter-time-series': (o, d) => - engine.renderScatterTimedChart(o, d as ChartTimeSeries), - 'financial-ohlc': (o, d) => - engine.renderFinancialChart(o, d as ChartTimeSeries), - }; - - final data = { - 'line': _lineSeries, - 'time-series': _timeSeries, - 'bar-vertical': _lineSeries, - 'bar-horizontal': _lineSeries, - 'gauge': _set, - 'scatter': _pairSeries, - 'scatter-time-series': _timeSeries, - 'financial-ohlc': _financialSeries, - }; - - for (var MapEntry(key: type, value: render) in renders.entries) { + // Chart type -> (data factory, render function): + final cases = + < + String, + (ChartData Function(), RenderedChart Function(HTMLElement, ChartData)) + >{ + 'line': ( + _lineSeries, + (o, d) => engine.renderLineChart(o, d as ChartSeries), + ), + 'time-series': ( + _timeSeries, + (o, d) => engine.renderTimeSeriesChart(o, d as ChartTimeSeries), + ), + 'bar-vertical': ( + _lineSeries, + (o, d) => engine.renderBarChart(o, d as ChartSeries), + ), + 'bar-horizontal': ( + _lineSeries, + (o, d) => engine.renderHorizontalBarChart(o, d as ChartSeries), + ), + 'gauge': (_set, (o, d) => engine.renderGaugeChart(o, d as ChartSet)), + 'scatter': ( + _pairSeries, + (o, d) => engine.renderScatterChart(o, d as ChartSeriesPair), + ), + 'scatter-time-series': ( + _timeSeries, + (o, d) => engine.renderScatterTimedChart(o, d as ChartTimeSeries), + ), + 'financial-ohlc': ( + _financialSeries, + (o, d) => engine.renderFinancialChart(o, d as ChartTimeSeries), + ), + }; + + for (var MapEntry(key: type, value: (newData, render)) in cases.entries) { test('calls the Dart callback: $type', () { var calls = <(List?, List?, List?)>[]; - var chartData = data[type]!(); + var chartData = newData(); chartData.options.onClick = (a, x, y) => calls.add((a, x, y)); var chart = render(_newOutput(), chartData);