Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/chartjs-2.9.4/chart_engine_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [] ;
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/chart_engine_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 11 additions & 6 deletions lib/src/chart_engine_chartjs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -434,6 +438,7 @@ class ChartEngineChartJS extends ChartEngine {
seriesPairs.toJSDeep,
_verticalLinesConfig(chartSeries),
colors.toJSDeep,
false,
_onClick(chartSeries),
];

Expand Down
2 changes: 2 additions & 0 deletions lib/src/chart_engine_series.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
96 changes: 96 additions & 0 deletions test/chart_engine_integration_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -313,6 +315,100 @@ 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<JSObject>('getDatasetMeta'.toJS, 0.toJS);
var elements = meta['data'] as JSArray<JSAny?>;
var items = [elements.toDart.first].toJS;
var evt = {'offsetX': 100, 'offsetY': 100}.toJSDeep;

handler!.callAsFunction(chartObj, evt, items);
}

// 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 = newData();
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<List>());
expect(yItems, isA<List>());
});
}

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();
Expand Down
Loading