Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## newVersion
* **FEATURE** Add `extraLinesData` support to [CandlestickChart](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/candlestick_chart.md). You can now draw horizontal and vertical lines on candlestick charts, similar to LineChart. Useful for showing support/resistance levels, moving averages, or other reference lines.
* **BUGFIX** (by @imaNNeo) Consider the `enabled` property in [LineTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/line_chart.md#linetouchdata-read-about-touch-handling), [BarTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/bar_chart.md#bartouchdata-read-about-touch-handling), [PieTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/pie_chart.md#pietouchdata-read-about-touch-handling), [ScatterTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/scatter_chart.md#scattertouchdata-read-about-touch-handling), [RadarTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/radar_chart.md#radartouchdata-read-about-touch-handling) and [CandlestickTouchData](https://github.com/imaNNeo/fl_chart/blob/main/repo_files/documentations/candlestick_chart.md#candlesticktouchdata-read-about-touch-handling), #1676

## 1.1.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ class CandlestickChartSample1State extends State<CandlestickChartSample1> {
getDrawingHorizontalLine: (_) => _gridLine,
getDrawingVerticalLine: (_) => _gridLine,
),
extraLinesData: ExtraLinesData(
horizontalLines: [
HorizontalLine(
y: _monthMedian,
color: AppColors.contentColorYellow,
strokeWidth: 2,
dashArray: [10, 5],
label: HorizontalLineLabel(
show: true,
alignment: Alignment.topRight,
padding:
const EdgeInsets.only(bottom: 8, right: 8),
style: const TextStyle(
color: AppColors.contentColorYellow,
fontSize: 12,
fontWeight: FontWeight.bold,
),
labelResolver: (line) =>
'Median: \$${line.y.toStringAsFixed(0)}',
),
),
],
),
titlesData: FlTitlesData(
show: true,
rightTitles: const AxisTitles(
Expand Down Expand Up @@ -258,6 +281,22 @@ class CandlestickChartSample1State extends State<CandlestickChartSample1> {

bool get _canGoPrevious => _currentMonthIndex > 0;

double get _monthMedian {
if (_btcMonthlyData == null) return 0;
final monthData = _btcMonthlyData![_currentMonthIndex];
if (monthData.isEmpty) return 0;

// Calculate median of closing prices
final closingPrices = monthData.map((e) => e.close).toList()..sort();
final middle = closingPrices.length ~/ 2;

if (closingPrices.length % 2 == 0) {
return (closingPrices[middle - 1] + closingPrices[middle]) / 2;
} else {
return closingPrices[middle];
}
}

void _previousMonth() {
if (!_canGoPrevious) {
return;
Expand Down
11 changes: 11 additions & 0 deletions lib/src/chart/candlestick_chart/candlestick_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
/// on top of each [CandlestickChartData.candleSpots] using [showingTooltipIndicators],
/// just put spot indices you want to show it on top of them.
///
/// [CandlestickChart] draws some horizontal or vertical lines on above or below of everything,
/// they are useful in some scenarios, for example you can show average line, you can fill
/// [extraLinesData] property to have your extra lines.
///
/// [clipData] forces the [CandlestickChart] to draw lines inside the chart bounding box.
CandlestickChartData({
List<CandlestickSpot>? candlestickSpots,
FlCandlestickPainter? candlestickPainter,
FlTitlesData? titlesData,
ExtraLinesData? extraLinesData,
CandlestickTouchData? candlestickTouchData,
List<int>? showingTooltipIndicators,
FlGridData? gridData,
Expand All @@ -57,6 +62,7 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
super(
gridData: gridData ?? const FlGridData(),
titlesData: titlesData ?? const FlTitlesData(),
extraLinesData: extraLinesData ?? const ExtraLinesData(),
clipData: clipData ?? const FlClipData.none(),
minX: minX ??
CandlestickChartHelper.calculateMaxAxisValues(
Expand Down Expand Up @@ -121,6 +127,8 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
t,
),
titlesData: FlTitlesData.lerp(a.titlesData, b.titlesData, t),
extraLinesData:
ExtraLinesData.lerp(a.extraLinesData, b.extraLinesData, t),
candlestickTouchData: b.candlestickTouchData,
showingTooltipIndicators: lerpIntList(
a.showingTooltipIndicators,
Expand Down Expand Up @@ -156,6 +164,7 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
List<CandlestickSpot>? candlestickSpots,
FlCandlestickPainter? candlestickPainter,
FlTitlesData? titlesData,
ExtraLinesData? extraLinesData,
CandlestickTouchData? candlestickTouchData,
List<int>? showingTooltipIndicators,
FlGridData? gridData,
Expand All @@ -176,6 +185,7 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
candlestickSpots: candlestickSpots ?? this.candlestickSpots,
candlestickPainter: candlestickPainter ?? this.candlestickPainter,
titlesData: titlesData ?? this.titlesData,
extraLinesData: extraLinesData ?? this.extraLinesData,
candlestickTouchData: candlestickTouchData ?? this.candlestickTouchData,
showingTooltipIndicators:
showingTooltipIndicators ?? this.showingTooltipIndicators,
Expand Down Expand Up @@ -204,6 +214,7 @@ class CandlestickChartData extends AxisChartData with EquatableMixin {
showingTooltipIndicators,
gridData,
titlesData,
extraLinesData,
minX,
maxX,
baselineX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ class CandlestickChartPainter extends AxisChartPainter<CandlestickChartData> {
..clipRect(Offset.zero & canvasWrapper.size);
}
super.paint(context, canvasWrapper, holder);

if (!holder.data.extraLinesData.extraLinesOnTop) {
super.drawExtraLines(context, canvasWrapper, holder);
}

drawAxisSpotIndicator(context, canvasWrapper, holder);
drawCandlesticks(context, canvasWrapper, holder);

if (holder.data.extraLinesData.extraLinesOnTop) {
super.drawExtraLines(context, canvasWrapper, holder);
}

if (holder.chartVirtualRect != null) {
canvasWrapper.restore();
}
Expand Down
32 changes: 32 additions & 0 deletions test/chart/candlestick_chart/candlestick_chart_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,38 @@ void main() {
),
false,
);

expect(
candleStickChartData1 ==
candleStickChartData1Clone.copyWith(
extraLinesData: ExtraLinesData(
horizontalLines: [
HorizontalLine(y: 10),
],
),
),
false,
);

expect(
candleStickChartData1 ==
candleStickChartData1Clone.copyWith(
extraLinesData: ExtraLinesData(
verticalLines: [
VerticalLine(x: 5),
],
),
),
false,
);

expect(
candleStickChartData1 ==
candleStickChartData1Clone.copyWith(
extraLinesData: const ExtraLinesData(),
),
true,
);
});

test('CandlestickSpot equality test', () {
Expand Down