I would like to overlay completed trades on the SfCartesianChart using a candle series by drawing a line between the entry price and close price. I have looked into trendlines and markers but cannot see how I would draw a simple trade line between 2 points on the chart.
Any suggestions?
Many Thanks
Alistair
Hi Alistair,
Greetings from Syncfusion.
We have achieved your requirement ‘drawing a line between the
open price and close price’ with the help of the onCreateRenderer
callback in the chart to create a customizable
series. This allows you to create a custom rendering in series.
We have prepared a sample based on your requirement and attached a sample below
for your reference.
Screenshot:
For more details, please refer this ug:
https://help.syncfusion.com/flutter/cartesian-charts/callbacks#oncreaterenderer
Note: We have rendered the black line to connect the open and close value. Please change the sample based on your requirement.
Regards,
Lavanya A.
Hi Lavanya,
Thanks for getting back to me. I think you may have mis-understood the requirement though - I would like to draw a line from the open and close of a trade, not the open and close of a candle. I need to just plot the 2 points on the same chart as the candle series and join them with a line. Ideally I would be able to pop up a tooltip at the start point and end point to show the trade details.
I have updated your demo screenshot to illustrate.
I think the onCreateRenderer looks like it will support this - maybe it is should be LineSeries on top of the CandleSeries? Is that possible?
Many Thanks
Alistair
Hi Arconomy,
We are checking for a feasible solution to achieve your requirement and we need time on this. We will update the further status in one business day on 30 January 2023. We appreciate your patience until then.
Regards,
Yuvaraj.
Hi Yuvaraj,
Thanks for looking into this. Let me know if I can provide any support.
Many thanks
Alistair
Hi Arconomy,
We are still looking for a feasible solution to achieve your requirement and we need one more business day. We will update the further detail on 31 January 2023. We appreciate your patience until then.
Regards,
Yuvaraj.
Thanks for the update Yuvaraj.
Looking forward to hearing from you tomorrow.
Many thanks
Alistair
Hi Arconomy,
We have achieved your requirement with the help of a custom painter widget. We have placed the chart widget inside a Stack, and used the SchedulerBinding to generate a list of CustomPaint widgets. Each CustomPaint object has a LinePainter object as its painter. We have stored the line trade points in the tradePoints property, and the LinePainter takes start and end offsets as inputs, which are calculated by converting the trade point data into pixel values using the pointToPixel method in the ChartSeriesController, accessed from the onRendererCreated callback. We have shared a sample below for your reference.
Screenshot:
Note:
We have used simple data points for rendering the line above the candle. Please change the tradePoints values based on your requirement.
Regards,
Yuvaraj.
Hi Yuvaraj,
Well done! Thanks for spending time on this. I will try and integrate your solution today and let you know how I get on.
Many Thanks
Alistair
Hi Yuvaraj,
I have integrated your solution into my chart and made some updates to suport redrawing the trade lines when the chart pans and zooms. I still have a few issues that I am struggling with though;
1) Preventing the CustomPaint widgets being repainted and added to the Stack if they are outside the visible chart area (see screenshots). Is there a way of getting the minimum and maximum visble points on the X axis and Y axis?
2) Do you know whether I could apply the TooltipBehavior to the CustomPaint Widget to show the trade details (e.g. Datetime, entry and close prices)?
Many Thanks
Alistair
Hi Arconomy,
1) Preventing the CustomPaint widgets being repainted and added to the Stack if they are outside the visible chart area (see screenshots). Is there a way of getting the minimum and maximum visble points on the X axis and Y axis?
We suggest using the technical indicator feature instead of CustomPainter to avoid rendering a line outside the chart area. We have generated a list of technical indicators based on the trade points and assigned it to the indicators property in the chart. We have created a sample for this and shared it below for your reference. You can modify it as per your requirement.
UG, https://help.syncfusion.com/flutter/cartesian-charts/technical-indicators
2) Do you know whether I could apply the TooltipBehavior to the CustomPaint Widget to show the trade details (e.g. Datetime, entry and close prices)?
Now, we are using the technical indicator instead of CustomPainter, so by default, the technical indicator has trackball support. You can customize the trackball tooltip using the builder property and show the trackball only at the start and end value of the trade line. We have shared a sample below for your reference.
Screenshot:
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Regards,
Yuvaraj.
Hi Yuvaraj,
I integrated your solution today - although I could not get the TrackBall to work together with the tooltipbehaviour - the ToolTipbehaviour seems to override and the default indicator tooltip shown instead of the using the trackballbehaviour builder.
I would be ok with just using the ToolTipBehaviour if there were a way to;
1) update the tooltip for the indicator to use custom widgets
2) update the indicator points to show a small circle (or square or triangle) at the entry point and close point.
Is this possible?
Many Thanks
Alistair
Hi Arconomy,
We are validating your query at our end, and we will update further details within one business day on February 6, 2023. We appreciate your patience until then.
Regards,
Yuvaraj.
Hi Arconomy,
Query #1: update the tooltip for the indicator to use custom widgets.
You can achieve your requirement to show the custom widget as a tooltip with the help of the builder property in the TooltipBehavior, here you can return the custom widget and it will be rendered instead of the tooltip. Please refer to the following UG documentation.
https://help.syncfusion.com/flutter/cartesian-charts/tooltip#tooltip-template
Query #2: update the indicator points to show a small circle (or square or triangle) at the entry point and close point.
Yes, you can render a circle shape at the entry and close point of the indicator with the help of ScatterSeries. Here we have generated the scatter series for the trade points value and added this scatter series to the existing series list. You can also change the shape of the scatter series with the help of the shape property in the markerSettings. We have shared the sample below for your requirement.
Screenshot:
Regards,
Yuvaraj.
To overlay completed trades on a SfCartesianChart using a candle series, you can use a LineSeries to represent the trade lines. The LineSeries allows you to plot lines between two points in the chart. You can specify the start and end points of the trade lines using the X and Y values.
In your case, the start point would be the entry price and the end point would be the close price. To create the LineSeries, you will need to set the X and Y values for each trade line. You can then add the LineSeries to the chart, and it will be displayed as a series of lines over the top of your candle series. Make sure to set the color, thickness, and any other styling options you want for the trade lines.
Thanks for the suggestion.
Hi Yuvaraj,
I trying to configure/customise the SfCartesianChart to support custom tooltips for multiple Series on the same chart. The solution above works when using the default tooltips only.
The aim is show price data (CandleSeries), position data (ScatterSeries + EmaIndicator) and order data (FastLineSeries?) on the charts and each line series will require a different custom tooltip. I was hoping that the toolTipBehaviour builder method would enable me to do this by switching on the data type in the callback.
The custom tooltips works for the CandleSeries (price data) but not for the ScatterSeries (trade data). The error below is thrown at runtime. I have updated and attached the sample app to recreate the problem. Run the app and hover over a trade data point to trigger the error.
The following IndexError was thrown while dispatching a pointer event:
RangeError (index): Index out of range: no indices are valid: 0
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw_
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 581:7 _get]
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 367:37 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 431:10 callMethod
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 435:5 dsend
packages/syncfusion_flutter_charts/src/common/user_interaction/tooltip_rendering_details.dart 445:36 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/linked_hash_map.dart 21:13 forEach
packages/syncfusion_flutter_charts/src/common/user_interaction/tooltip_rendering_details.dart 382:32 showTemplateTooltip
packages/syncfusion_flutter_charts/src/chart/base/chart_base.dart 3908:38 [_performMouseHover]
packages/syncfusion_flutter_charts/src/chart/base/chart_base.dart 2340:39 <fn>
packages/flutter/src/rendering/proxy_box.dart 3266:14 handleEvent
packages/flutter/src/gestures/binding.dart 440:14 dispatchEvent
packages/flutter/src/rendering/binding.dart 336:11 dispatchEvent
packages/flutter/src/gestures/binding.dart 395:7 [_handlePointerEventImmediately]
Please can you assist.
Many Thanks
Alistair
Hi Arconomy,
The technical indicator data source is calculated in the source based on the period property, but we only used two data points for the indicator data source in the sample, so an exception is thrown.
However, you can resolve this issue by using the LineSeries instead of indicators, as per the below code snippet.
List<ChartSeries<ChartSampleData, DateTime>> getSeries() { final List<ChartSeries<ChartSampleData, DateTime>> series = []; series.add(CandleSeries( …. ));
series.addAll(List<LineSeries<ChartSampleData, DateTime>>.generate( tradePoints.length, (index) => LineSeries( color: Colors.black, dataSource: tradePoints[index], xValueMapper: (ChartSampleData data, _) => data.x, yValueMapper: (ChartSampleData data, _) => data.high)));
series.addAll(List<ScatterSeries<ChartSampleData, DateTime>>.generate( tradePoints.length, (index) => ScatterSeries( …. ))); return series; } }
|
We have noticed one more issue in your sample, you have checked that the data is ChartSampleData to modify the tooltip builder for candle series and other series. But that condition is always satisfied and shows candle builder text only. You can resolve that issue by checking that the series is CandleSeries, as per the below code snippet.
builder: (dynamic data, dynamic point, dynamic series, int pointIndex, int seriesIndex) { if (series is CandleSeries) { return const Text( 'Custom tooltip for Candle data', style: TextStyle(color: Colors.white), ); } else { return const Text( 'Custom tooltip for Trade data', style: TextStyle(color: Colors.white), ); } }, |
And we have modified your sample and shared it below for your reference.
Regards,
Lavanya A.
Hi Lavanya - thanks for your quick response.
The line series works much better - however I think we now have an issue now where the width of the Candles seems to be overridden by the width of the Line Series? I'll do some more investigating and get back to you.
Many Thanks
Alistair
Hi Arconomy,
Thanks, we will wait for your further update.
Regards,
Yuvaraj.
Hi Yuvaraj,
Thanks for all your support so far - you guys are doing a great job and have some great widgets.
I have investigated the issue further and it seems to be an issue with the ScatterSeries affecting the CandleSeries. The width of the candles changes when the ScatterSeries is added to the chart.
I have updated the demo attached to illustrate the problem. Click on the 'Show ScatterSeries button' to see the issue.
This demo is closer to the real world data.
I also have a few other questions;
1) Is there a way to add a tooltip to the LineSeries so that when use mouse over the line the tooltip would appear rather than just the points of the line? When you zoom in the end of the line is not visible so the tootip does not show.
2) Is there a way of getting and setting the zoom level and pan state programmatically so that if the chart data is refreshed I can rebuild the chart with the same zoom and pan state.
Many Thanks for all your help
Alistair
Hi Arconomy,
Query #1: The width of the candles changes when the ScatterSeries is added to the chart.
We have checked the reported issue that candle series width gets decrease when adding scatter series to the cartesian chart, and gets reproduced at our end. Currently, we are investigating the root cause in our source we will update you the further details tomorrow.
Query #2: Is there a way to add a tooltip to the LineSeries so that when use mouse over the line the tooltip would appear rather than just the points of the line? When you zoom in the end of the line is not visible so the tootip does not show.
The tooltip will appear only when the user taps or hovers the mouse close to the data point area. If the mouse is not in that region, the tooltip won't appear. Currently, we don't have support for activating the tooltip when the mouse hovers over the line series segment.
Query #3: Is there a way of getting and setting the zoom level and pan state programmatically so that if the chart data is refreshed I can rebuild the chart with the same zoom and pan state.
Yes, you can update the zoom level and pan the chart using the ZoomPanBehavior public method, we have shared the related UG documentation below for your reference.
UG, https://help.syncfusion.com/flutter/cartesian-charts/methods#methods-in-zoompanbehavior
Regards,
Yuvaraj.
Hi Arconomy,
We are still checking for a feasible workaround to achieve your requirement, we will update the further details in one business day on 6 March 2023. We appreciate your patience until then.
Regards,
Yuvaraj.
Thanka for the update. Looking forward to hearing from you once you have a fix.
Many thanks
Alistair
Hi Arconomy,
We are able to replicate the reported issue regarding the candle series width getting decreased while adding a scatter series to the chart at our end and have logged a bug report for it in our feedback portal. We will fix and include the changes in our 2023 volume 1 main release which is expected to be rolled out on 21 March 2023. We will update you here once the release is rolled out and we appreciate your patience until then. You can also track the status of the bug with the feedback below.
Feedback, https://www.syncfusion.com/feedback/41822
Regards,
Yuvaraj.
Hi Arconomy,
Thanks for your patience. The reported issue regarding the candle series width getting decreased while adding a scatter series to the chart has been fixed and rolled out in our weekly patch release. To avoid this please upgrade the chart package to the latest version below.
Version: https://pub.dev/packages/syncfusion_flutter_charts/versions/21.1.35
Regards,
Yuvaraj.
Thanks for the update Yuvaraj. I will update to latest.
Many thanks
Most Welcome. Kindly get back to us if you have further queries. We are always happy to assist you.